RadioGroup + Fragment實(shí)現(xiàn)
- 01 效果圖
該實(shí)現(xiàn)方式只能通過切換RadioButton來切換頁面腐螟,并不能通過滑動(dòng)來實(shí)現(xiàn)。
- 02 layout
4個(gè)fragment的相似布局文件
contact_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact fragment"
android:textSize="30sp"/>
</LinearLayout>
主布局文件
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- 將該FrameLayout作為Fragment的容器 -->
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<RadioGroup
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">
<RadioButton
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" />
<RadioButton
android:id="@+id/rb_contact"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:button="@drawable/radio_contact_selector"/>
<RadioButton
android:id="@+id/rb_discovery"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:button="@drawable/radio_discovery_selector"/>
<RadioButton
android:id="@+id/rb_personal"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:button="@drawable/radio_personal_selector"/>
</RadioGroup>
</LinearLayout>
- 03 Activity
4個(gè)Fragment的相似Java代碼
public class ContactFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 這里注意inflate的第三個(gè)參數(shù)要設(shè)置為false
View view = inflater.inflate(R.layout.contact_fragment, container, false);
return view;
}
}
MainActivity.java
public class MainActivity extends Activity implements OnCheckedChangeListener {
// 用于對(duì)Fragment進(jìn)行管理
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);
// 設(shè)置默認(rèn)的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) {
// 開啟一個(gè)Fragment事務(wù)
FragmentTransaction transaction = fragmentManager.beginTransaction();
// 先隱藏掉所有的Fragment
hideFragments(transaction);
switch (index) {
case 0:
if (talkFragment == null) {
// 如果TalkFragment為空,則創(chuàng)建一個(gè)并添加到界面上
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();
}
/**
* 將所有的Fragment都置為隱藏狀態(tài)报账。
*
* @param transaction
* 用于對(duì)Fragment執(zhí)行操作的事務(wù)
*/
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 橫豎屏切換
如果參考這個(gè)Demo后当宴,在橫豎屏切換時(shí)發(fā)現(xiàn)重疊問題纵势,那么在AndroidManifest.xml文件中做如下設(shè)置踱阿。
橫豎屏切換時(shí),activity不重新創(chuàng)建吨悍。
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden|screenSize">