ViewPager+Fragment实现页面滑动切换

一、创建activity(WxActivity)

二、修改WxActivity对应的布局文件activity_wx

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.administrator.myapplication.WxActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/wx_vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/content_lin"></android.support.v4.view.ViewPager>

    <LinearLayout
        android:id="@+id/content_lin"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">


        <Button
            android:id="@+id/contact_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="联系人" />

        <Button
            android:id="@+id/friend_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="朋友圈" />

        <Button
            android:id="@+id/news_btn"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="消息" />

    </LinearLayout>

</RelativeLayout>

三、创建fragment(ContactFragment,FriendFragment,NewsFragment)修改其对应的布局文件fragment_contact,fragment_news,fragment_friend

<FrameLayout 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="com.example.administrator.myapplication.fragment.ContactFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="30sp"
        android:gravity="center"
        android:text="联系人" />

</FrameLayout>

四、创建MyPageAdapter适配器

public class MyPageAdapter extends FragmentPagerAdapter{
    //创建fragmentList集合
    private List<Fragment> fragmentList;
    //构造方法实现传值
    public MyPageAdapter(FragmentManager fm,List<Fragment> fragmentList) {
        super(fm);
        this.fragmentList = fragmentList;
    }
    //返回fragmentList对象
    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }
    //返回fragmentList对象个数
    @Override
    public int getCount() {
        return fragmentList.size();
    }
}

五、修改WxActivity

public class WxActivity extends AppCompatActivity implements View.OnClickListener {
    //创建button对象
    private Button contactBtn;
    private Button friendBtn;
    private Button newsBtn;
    //创建viewpager对象
    private ViewPager wxVp;
    //创建fragmentList集合,并进行实力化
    List<Fragment> fragmentList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wx);
        //绑定ID
        bangId();
        //实例化fragment对象
        ContactFragment contactFragment = new ContactFragment();
        FriendFragment friendFragment = new FriendFragment();
        NewsFragment newsFragment = new NewsFragment();
        //将fragment对象通过add()方法放入fragmentList集合中
        fragmentList.add(contactFragment);
        fragmentList.add(friendFragment);
        fragmentList.add(newsFragment);
        //创建MyPageAdapter页面适配器
        MyPageAdapter adapter = new MyPageAdapter(getSupportFragmentManager(), fragmentList);
        //绑定适配器
        wxVp.setAdapter(adapter);


    }

    private void bangId() {
        contactBtn = findViewById(R.id.contact_btn);
        friendBtn = findViewById(R.id.friend_btn);
        newsBtn = findViewById(R.id.news_btn);
        wxVp = findViewById(R.id.wx_vp);

        contactBtn.setOnClickListener(this);
        friendBtn.setOnClickListener(this);
        newsBtn.setOnClickListener(this);
        wxVp.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.contact_btn:
                //通过setCurrentItem()方法实现点击跳转;
                wxVp.setCurrentItem(0);
                break;
            case R.id.friend_btn:
                wxVp.setCurrentItem(1);
                break;
            case R.id.news_btn:
                wxVp.setCurrentItem(2);
                break;
            default:
                break;
        }

    }
}

六、效果展现

这里写图片描述
这里写图片描述
这里写图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

intoSunshine

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

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

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

打赏作者

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

抵扣说明:

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

余额充值