效果图:
图1:
图2:
图3:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ly_main"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="9">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#03b8ff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/rb_room"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="选择房间"
android:button="@null"
android:textSize="20dp"
android:gravity="center"
android:textColor="#ed0909"/>
<RadioButton
android:id="@+id/rb_dining"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="餐厅"
android:button="@null"
android:textSize="20dp"
android:gravity="center"
android:textColor="#fff"/>
<RadioButton
android:id="@+id/rb_kursaal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="游乐场"
android:button="@null"
android:textSize="20dp"
android:gravity="center"
android:textColor="#fff"/>
</LinearLayout>
</RadioGroup>
</LinearLayout>
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RadioButton rbMessage,rbSay,rbLinkman;
private FragmentManager manager;
private FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); // 初始化控件
// 创建布局管理器对象
manager = getSupportFragmentManager();
transaction = manager.beginTransaction(); // 获得事务对象,并启动事务
transaction.add(R.id.ly_main,new RoomFragment()); // 默认打开联系人界面,第一个参数是这个 Fragment 要依赖的布局,第二个是对应的 Fragment
transaction.commit(); // 提交事务
}
private void initView() {
rbMessage = findViewById(R.id.rb_room);
rbLinkman = findViewById(R.id.rb_dining);
rbSay = findViewById(R.id.rb_kursaal);
rbSay.setOnClickListener(this);
rbLinkman.setOnClickListener(this);
rbMessage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
transaction = manager.beginTransaction(); // 因为要切换界面,所以要重新获取一次事务对象
switch (v.getId()){
case R.id.rb_room:
/**
* 利用replace方法,达到滑动切换界面的目的
* 第一个参数是: 这个 Fragment 要放置的布局界面
* 第二个参数是: 对应的 Fragment
*/
transaction.replace(R.id.ly_main,new RoomFragment());
break;
case R.id.rb_dining:
transaction.replace(R.id.ly_main,new DiningFragment());
break;
case R.id.rb_kursaal:
transaction.replace(R.id.ly_main,new KursaalFragment());
break;
}
transaction.commit(); // 重新提交事务
}
}
fragment_dining.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".DiningFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="餐厅"
android:layout_centerInParent="true"
android:textSize="40dp"/>
</RelativeLayout>
DiningFragment.xml:
public class DiningFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dining, container, false);
}
}
fragment_kursaal.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".KursaalFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游乐场"
android:layout_centerInParent="true"
android:textSize="40dp"/>
</RelativeLayout>
KursaalFragment.java:
ublic class KursaalFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_kursaal, container, false);
}
}
创建五个子Fragment,分别为:OneFragment,TwoFragment,ThreeFragment,LuxuryFragment,VipFragment,它们的代码都是一模一样的,所以这里我就只列举一个就好:
public class OneFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
这个子Fragment所对应的布局也基本都是一样的,这里我列举一个即可:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".KursaalFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单人间"
android:layout_centerInParent="true"
android:textSize="40dp"/>
</RelativeLayout>
最核心的! 实现 Fragment + ViewPater 的代码来了:
FragAdapter.java:
public class FragAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList;
public FragAdapter(FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
this.fragmentList = fragmentList;
}
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
}