您的位置:首页 > 其它

Fragment页面的切换

2016-04-16 12:03 393 查看
在android日常开发中,经常会用到点击底部导航栏,然后多个Fragment互相切换的功能。之前做毕设的时候用到了这个功能,今天有空整理了一下,并且做了androidStudio的demo,供大家一起讨论学习。闲话不多说,直接上图,大约就是这个样子(欢迎转载,请说明来处)Demo下载地址:http://download.csdn.net/detail/sinat_27681957/9492952



主文件用来控制Ftagment的切换:

public class MainActivity extends AppCompatActivity {
private Fragment[] mFragments;
private RadioGroup bottomRg;
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
init();
}

private void init(){
mFragments = new Fragment[3];
fragmentManager = getSupportFragmentManager();
mFragments[0] = fragmentManager.findFragmentById(R.id.fragment_main);
mFragments[1] = fragmentManager.findFragmentById(R.id.fragment_bbs);
mFragments[2] = fragmentManager.findFragmentById(R.id.fragment_user);
fragmentTransaction = fragmentManager.beginTransaction()
.hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);
fragmentTransaction.show(mFragments[0]).commit();
setFragmentIndicator();
}

//切换fragment
private void setFragmentIndicator() {

bottomRg = (RadioGroup) findViewById(R.id.bottomRg);
bottomRg.setOnCheckedChangeListener(new RadioGroup.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();
Log.e("1", "1");
break;

case R.id.rbTwo:
fragmentTransaction.show(mFragments[1]).commit();
Log.e("2", "2");
break;

case R.id.rbThree:
fragmentTransaction.show(mFragments[2]).commit();
Log.e("3", "3");
break;

default:
break;
}
}
});
}
}

主布局文件里面放置3个fragment:

<?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" >

<!-- 上边主页面 -->

<fragment
android:id="@+id/fragment_main"
android:name="com.lei.doctorofhand.fragment.MainFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />

<fragment
android:id="@+id/fragment_bbs"
android:name="com.lei.doctorofhand.fragment.HomeFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />

<fragment
android:id="@+id/fragment_user"
android:name="com.lei.doctorofhand.fragment.UserFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="20" />

<!-- 分割线 -->
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/bgcolor"/>

<!-- 底部菜单页面 -->
<RadioGroup
android:id="@+id/bottomRg"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:background="@color/footer_bg_normal"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/rbOne"
style="@style/rg_btn_style"
android:checked="true"
android:text="首页"
android:textColor="@color/black"
android:drawableTop="@drawable/rb_one_btn_selector"/>

<RadioButton
android:id="@+id/rbTwo"
style="@style/rg_btn_style"
android:text="房间"
android:textColor="@color/black"
android:drawableTop="@drawable/rb_two_btn_selector"/>

<RadioButton
android:id="@+id/rbThree"
style="@style/rg_btn_style"
android:text="设置"
android:textColor="@color/black"
android:drawableTop="@drawable/rb_three_btn_selector"/>
</RadioGroup>

</LinearLayout>

MainFragment:

public class MainFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_main,container,false);
return view;
}

HomeFragment:

public class HomeFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_home, container, false);
return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initView();
}
private void initView() {

}
}

UserFragment:

public class UserFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.fragment_user, container, false);
}

}

fragment_main.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:background="@color/bgwhite"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="首页"/>

</LinearLayout>

Fragment_home.xml:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/bgwhite"
android:orientation="vertical"
tools:context="com.askdoctor.ui.FragmentBBS" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="房间"/>
</LinearLayout>

Fragment_user.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgwhite"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="用户"/>

</RelativeLayout>


好了差不多了,Demo下载地址:http://download.csdn.net/detail/sinat_27681957/9492952
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: