Android Fragment + ViewPater实现点击和滑动切换

效果图:

图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();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王睿丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值