ViewPager滑动切换Fragment代码

老长时间没有编辑过Vp滑动切换Fragment的代码了

这段时间有时间赶紧复习了一下,贴在网上备用---->

 

 

这是布局

<?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:orientation="vertical"
    tools:context=".view.activity.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/one"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/oneFragment" />
        <TextView
            android:id="@+id/two"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/twoFragment" />
        <TextView
            android:id="@+id/three"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/threeFragment" />
    </LinearLayout>
</LinearLayout>

 

 

这是activity的代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ViewPager mVp;
    private TextView mOne;
    private TextView mTwo;
    private TextView mThree;
    private ArrayList<Fragment> fragments;
    private ArrayList<TextView> textViews;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mVp = findViewById(R.id.vp);
        mOne = findViewById(R.id.one);
        mTwo = findViewById(R.id.two);
        mThree = findViewById(R.id.three);
        //装载fragment
        fragments = new ArrayList<>();
        fragments.add(new OneFragment());
        fragments.add(new TwoFragment());
        fragments.add(new ThreeFragment());
        //装载textview
        textViews = new ArrayList<>();
        textViews.add(mOne);
        textViews.add(mTwo);
        textViews.add(mThree);

        mOne.setOnClickListener(this);
        mTwo.setOnClickListener(this);
        mThree.setOnClickListener(this);

        //Vp默认展示第一页   textview也设置一下设置对应样式
        textViews.get(0).setTextColor(Color.RED);

        //Vp设置适配器
        mVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        //Vp设置页面改变监听
        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                for (TextView text :
                        textViews) {
                    text.setTextColor(Color.BLACK);
                }
                //一定在最后一行!!!   否则样式不会被应用!
                textViews.get(i).setTextColor(Color.RED);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    /**
     * textview点击切换fragment
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.one:
                mVp.setCurrentItem(0);
                break;
            case R.id.two:
                mVp.setCurrentItem(1);
                break;
            case R.id.three:
                mVp.setCurrentItem(2);
                break;
        }
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值