ViewPager系列:ViewPager+Fragment实现滑动下一页效果

Posted zhangjin1120

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ViewPager系列:ViewPager+Fragment实现滑动下一页效果相关的知识,希望对你有一定的参考价值。

公有四个Fragment,内容不同,布局也会不同。上效果图:

public class CustomViewPager extends ViewPager {

    public CustomViewPager(Context context)
    {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    int mCurrentPagePosition;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            boolean wrapHeight = MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST;
            if (wrapHeight) {
                View child = getChildAt(mCurrentPagePosition);
                if (child != null) {
                    child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                    int h = child.getMeasuredHeight();

                    heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void reMeasureCurrentPage(int position) {
        mCurrentPagePosition = position;
        requestLayout();
    }
}
  • activity_main.xml代码中写入ViewPager:
<?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=".MainActivity">

   <com.zj.viewpagerdemo1.CustomViewPager
       android:id="@+id/vp_word"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>

</RelativeLayout>
  • MainActivity.java中初始化ViewPager,并且设置Adapter、滑动监听:
public class MainActivity extends AppCompatActivity {
    CustomViewPager vpWord;
    TestFragment fragment1,fragment2,fragment3,fragment4;
    TestFragment[] fragments = new TestFragment[4];
    String[] nameList = new String[]{"张学友","刘德华","黎明","郭富城"};
    VPFragmentAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vpWord = findViewById(R.id.vp_word);
        fragment1 = new TestFragment();
        fragment2 = new TestFragment();
        fragment3 = new TestFragment();
        fragment4= new TestFragment();

        fragments[0] = fragment1;
        fragments[1] = fragment2;
        fragments[2] = fragment3;
        fragments[3] = fragment4;

        adapter = new VPFragmentAdapter(getSupportFragmentManager(), fragments, nameList);
        vpWord.setOffscreenPageLimit(fragments.length); //解决报空问题
        vpWord.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vpWord.reMeasureCurrentPage(vpWord.getCurrentItem());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vpWord.setAdapter(adapter);

    }
}
  • TestFragment的核心作用,就是根据不同的name值,匹配不同的布局。
public class TestFragment extends Fragment {
    String name;

    public TestFragment() {
    }

    public void setName(String name) {
        this.name = name;
    }

    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (name.equals("张学友")) {
            rootView = inflater.inflate(R.layout.fragment_test, container, false);
        } else {
            rootView = inflater.inflate(R.layout.fragment_test2, container, false);
        }
        TextView tv = rootView.findViewById(R.id.tv);
        tv.setText(name);
        return rootView;
    }
}
  • VPFragmentAdapter的核心任务,是把不同的name,赋值给fragment,也就是fragments[i].setName(mTitle[i]);
public class VPFragmentAdapter extends FragmentPagerAdapter {
    private String[] mTitle;
    private TestFragment[] fragments;

    public VPFragmentAdapter(FragmentManager fm, TestFragment[] fragments, String[] pageNames) {
        super(fm);
        this.fragments = fragments;
        this.mTitle = pageNames;
    }

    @Override
    public TestFragment getItem(int i) {
       fragments[i].setName(mTitle[i]);
        return fragments[i];
    }


    @Override
    public int getCount() {
        return fragments.length;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitle[position];
    }
}
  • 布局代码就太简单了,不写了。

以上是关于ViewPager系列:ViewPager+Fragment实现滑动下一页效果的主要内容,如果未能解决你的问题,请参考以下文章

ViewPager系列:ViewPager+Fragment实现滑动下一页效果

ViewPager系列文章(一)- ViewPager源码分析及加载页面原理图

ViewPager系列:代码控制直接跳转到某一个页面

ViewPager系列之 仿魅族应用的广告BannerView(转)

ViewPager系列之 仿魅族应用的广告BannerView

如何以编程方式从片段内部设置ViewPager选项卡?