ViewPager + PagerAdapte的使用

之前写过一篇TabLayout  + ViewPager + Fragment  文章。  TabLayout + ViewPager + Fragment

近期在做题库页面,用到ViewPager 和 PagerAdapter,所以想要简单的记录一下。  答题功能

 

主要技术点

1.ViewPager常用API

2.PagerAdapter重写方法介绍

3.VIewPager + PagerAdapter使用介绍

3.ViewPager滑动速度自定义

 

 

ViewPager常用API

可参考ViewPager

1.setAdapter(PagerAdapter adapter)// 设置适配器函数,和ListView同理
2.setCurrentItem(int item) // 设置当前要显示的页面
3.setOffscreenPageLimit(int limit)// 设置默认缓存页面数量
4.addOnPageChangeListener(ViewPager.OnPageChangeListener listener) //设置ViewPager事件监听   (重写三个方法onPageScrolled、onPageSelected、onPageScrollStateChanged)

 

PagerAdapter使用介绍

  

/*
*获取pager的个数
*/
 @Override
    public int getCount() {
        return mList.size();
    }
/*
*移除一个给定索引的View
*/
@Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
/*
*判断是否是当前View
*/
  @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

/*
*给container添加View
*/

 @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
       ...
        return scrollView;
    }

 

3.VIewPager + PagerAdapter使用介绍

1)Activity的XML里面添加控件View

2)Activity中PagerAdater设置给ViewPager

    public ArrayList mQuestionList = new ArrayList<>();


 @Override
    public void initData(Bundle savedInstanceState) {
        for (int i = 0; i < 10; i++) {
            ArrayList metas = new ArrayList<>();
            metas.add("宪法");
            metas.add("法律");
            metas.add("行政法规");
            metas.add("行政规章");
            HomeworkQuestionBean homeworkQuestionBean1 = new HomeworkQuestionBean();
            homeworkQuestionBean1.type = HomeworkQuestionBean.HomeworkQuestionTypeBean.single_choice;
            homeworkQuestionBean1.setMetas(metas);
            homeworkQuestionBean1.setStem("1、下列发的形式中,由全国人民代表大会及其常务委员会经一定立法程序制定颁布," +
                    "调整国家、社会和公民生活中基本社会关系的是()。");
            mQuestionList.add(homeworkQuestionBean1);
        }

 mAdapter = new QuestionPagerAdapter(getContext(), results);
        viewPager.setAdapter(mAdapter);

}

 

Adapter

public class QuestionPagerAdapter extends PagerAdapter {


    protected LayoutInflater inflater;
    protected Context mContext;
    protected ArrayList mList;


    public QuestionPagerAdapter(Context context, ArrayList list) {
        mList = list;
        mContext = context;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        HomeworkQuestionBean questionBean = mList.get(position);

        View view = switchQuestionWidget(questionBean, position + 1, mList.size());
        ScrollView scrollView = new ScrollView(mContext);
        scrollView.setFillViewport(true);
        scrollView.setVerticalScrollBarEnabled(false);
        scrollView.addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        container.addView(scrollView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return scrollView;
    }


    private View switchQuestionWidget(HomeworkQuestionBean question, int index, int totalNum) {
        BaseHomeworkQuestionWidget mWidget;
        HomeworkQuestionBean.HomeworkQuestionTypeBean typeBean = question.getType();
        int layoutId = 0;
        switch (typeBean) {
            case choice:
            case uncertain_choice:
                layoutId = R.layout.item_pager_homework_question_choice;
                break;
            case single_choice:
                layoutId = R.layout.item_pager_homework_question_singlechoice;
                break;
            case essay:
                layoutId = R.layout.item_pager_homework_question_essay;
                break;
            case determine://判断题
                layoutId = R.layout.item_pager_homework_question_determine;
                break;
            default:
                break;
        }
        mWidget = (BaseHomeworkQuestionWidget) LayoutInflater.from(mContext).inflate(layoutId, null);
        mWidget.setData(question, index, totalNum);
        return mWidget;
    }


}

 

PS:感觉这篇文章写得很LOW,就当是记录了。

 

4.ViewPager滑动速度自定义

public class ExamViewPager extends ViewPager {
    public ESExamViewPager(@NonNull Context context) {
        super(context);
        initView();
    }

    public ExamViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }


    private void initView() {
        try {
            Class clazz = Class.forName("android.support.v4.view.ViewPager");
            Field f = clazz.getDeclaredField("mScroller");
            FixedViewPagerSpeedScroller fixedSpeedScroller =
                    new FixedViewPagerSpeedScroller(getContext(), new LinearOutSlowInInterpolator());
            fixedSpeedScroller.setmDuration(300);
            f.setAccessible(true);
            f.set(this, fixedSpeedScroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

OK。

 

 

你可能感兴趣的:(ViewPager + PagerAdapte的使用)