admin 管理员组

文章数量: 887021

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

  • 引入CustomViewPager,引入的原因参考:ViewPager系列:解决ViewPager内部,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/apk/res/android"
    xmlns:app="http://schemas.android/apk/res-auto"
    xmlns:tools="http://schemas.android/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 Fragment