新浪微博をまねてbuttonをクリックしてfragmentを切り替えます
5024 ワード
キーコード:
メインインタフェースのレイアウト:
package com.itfanr.fragmenttab;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.os.Build;
public class MainActivity extends Activity {
private Fragment[] mFragments;
private RadioGroup bottomRg;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
private RadioButton rbOne, rbTwo, rbThree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFragments = new Fragment[3];
fragmentManager = this.getFragmentManager();
mFragments[0] = fragmentManager.findFragmentById(R.id.chat_fragment);
mFragments[1] = fragmentManager.findFragmentById(R.id.find_fragment);
mFragments[2] = fragmentManager
.findFragmentById(R.id.mine_fragment);
fragmentTransaction = fragmentManager.beginTransaction()
.hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);
fragmentTransaction.show(mFragments[0]).commit();
setFragmentIndicator() ;
}
private void setFragmentIndicator() {
this.bottomRg = (RadioGroup) findViewById(R.id.bottomRg);
this.rbOne = (RadioButton) findViewById(R.id.rbOne);
this.rbTwo = (RadioButton) findViewById(R.id.rbTwo);
this.rbThree = (RadioButton) findViewById(R.id.rbThree);
bottomRg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
fragmentTransaction = fragmentManager.beginTransaction()
.hide(mFragments[0]).hide(mFragments[1])
.hide(mFragments[2]);
switch (checkedId) {
case R.id.rbOne:
fragmentTransaction.show(mFragments[0]).commit();
break;
case R.id.rbTwo:
fragmentTransaction.show(mFragments[1]).commit();
break;
case R.id.rbThree:
fragmentTransaction.show(mFragments[2]).commit();
break;
default:
break;
}
}
});
}
}
メインインタフェースのレイアウト:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<fragment
android:id="@+id/chat_fragment"
android:name="com.itfanr.fragmenttab.ChatFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10" />
<fragment
android:id="@+id/find_fragment"
android:name="com.itfanr.fragmenttab.FindFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10" />
<fragment
android:id="@+id/mine_fragment"
android:name="com.itfanr.fragmenttab.MineFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="10" />
<RadioGroup
android:id="@+id/bottomRg"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_weight="0.5"
android:orientation="horizontal"
android:gravity="center">
<RadioButton
android:id="@+id/rbOne"
android:background="@drawable/ofm_collect_icon"
android:checked="true"
android:text=" " />
<RadioButton
android:id="@+id/rbTwo"
android:background="@drawable/ofm_collect_icon"
android:text=" " />
<RadioButton
android:id="@+id/rbThree"
android:background="@drawable/ofm_collect_icon"
android:text=" " />
</RadioGroup>
</LinearLayout>