RadioGroup + Fragment實作
01 效果圖
該實作方式隻能通過切換RadioButton來切換頁面,并不能通過滑動來實作。

MainInterface.png
02 layout
4個fragment的相似布局檔案
contact_fragment.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact fragment"
android:textSize="30sp"/>
主布局檔案
activity_main.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
android:id="@+id/rg_control"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:gravity="center">
android:id="@+id/rb_talk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="30dp"
android:button="@drawable/radio_talk_selector"
android:checked="true" />
android:id="@+id/rb_contact"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:button="@drawable/radio_contact_selector"/>
android:id="@+id/rb_discovery"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:button="@drawable/radio_discovery_selector"/>
android:id="@+id/rb_personal"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:button="@drawable/radio_personal_selector"/>
03 Activity
4個Fragment的相似Java代碼
public class ContactFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 這裡注意inflate的第三個參數要設定為false
View view = inflater.inflate(R.layout.contact_fragment, container, false);
return view;
}
}
MainActivity.java
public class MainActivity extends Activity implements OnCheckedChangeListener {
// 用于對Fragment進行管理
private FragmentManager fragmentManager;
private TalkFragment talkFragment;
private ContactFragment contactFragment;
private DiscoveryFragment discoveryFragment;
private PersonalFragment personalFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
initUI();
}
private void initUI() {
RadioGroup rg_control = (RadioGroup) findViewById(R.id.rg_control);
rg_control.setOnCheckedChangeListener(this);
// 設定預設的Fragment
setDefaultFragment();
}
private void setDefaultFragment() {
FragmentTransaction transaction = fragmentManager.beginTransaction();
talkFragment = new TalkFragment();
transaction.add(R.id.fl_content, talkFragment);
transaction.commit();
}
// Radio切換的事件處理
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId) {
case R.id.rb_talk:
setTabSelection(0);
break;
case R.id.rb_contact:
setTabSelection(1);
break;
case R.id.rb_discovery:
setTabSelection(2);
break;
case R.id.rb_personal:
setTabSelection(3);
break;
}
}
private void setTabSelection(int index) {
// 開啟一個Fragment事務
FragmentTransaction transaction = fragmentManager.beginTransaction();
// 先隐藏掉所有的Fragment
hideFragments(transaction);
switch (index) {
case 0:
if (talkFragment == null) {
// 如果TalkFragment為空,則建立一個并添加到界面上
talkFragment = new TalkFragment();
transaction.add(R.id.fl_content, talkFragment);
} else {
// 如果TalkFragment不為空,則直接将它顯示出來
transaction.show(talkFragment);
}
break;
case 1:
if (contactFragment == null) {
contactFragment = new ContactFragment();
transaction.add(R.id.fl_content, contactFragment);
} else {
transaction.show(contactFragment);
}
break;
case 2:
if (discoveryFragment == null) {
discoveryFragment = new DiscoveryFragment();
transaction.add(R.id.fl_content, discoveryFragment);
} else {
transaction.show(discoveryFragment);
}
break;
case 3:
if (personalFragment == null) {
personalFragment = new PersonalFragment();
transaction.add(R.id.fl_content, personalFragment);
} else {
transaction.show(personalFragment);
}
break;
}
transaction.commit();
}
private void hideFragments(FragmentTransaction transaction) {
if (talkFragment != null) {
transaction.hide(talkFragment);
}
if (contactFragment != null) {
transaction.hide(contactFragment);
}
if (discoveryFragment != null) {
transaction.hide(discoveryFragment);
}
if (personalFragment != null) {
transaction.hide(personalFragment);
}
}
}
04 橫豎屏切換
如果參考這個Demo後,在橫豎屏切換時發現重疊問題,那麼在AndroidManifest.xml檔案中做如下設定。
橫豎屏切換時,activity不重新建立。
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden|screenSize">