使用ViewPager和RecyclerView实现水平分页功能

/**
 * ght (c) 2007-2017 xxx Inc. All rights reserved.
 *
 * @author lzz
 *         Created 2017/5/4 10:07
 * @license http://www.xxx.net
 * @link http://www.xxx.net
 * @description 推荐商品的ViewPager适配器;带有水平分页功能的RecyclerView
 *
 * **********************
 *   ##     ##      ##  *
 *                      *
 *   ##     ##      ##  *
 * **********************
 */
public class GoodsPageAdapter extends PagerAdapter {

    private List<View> indicators = new ArrayList<>();//水平分页的指示器
    private ArrayList<GoodsVo> datas = new ArrayList<>();//RecyclerView数据集合
    private Context context;
    private LinearLayout llIndicators;//水平分页的容器

    public void setDatas(ArrayList<GoodsVo> datas) {
        this.datas = datas;
    }

    public List<View> getIndicators() {
        return indicators;
    }

    public GoodsPageAdapter(Context context, LinearLayout llIndicators) {
        this.context = context;
        this.llIndicators = llIndicators;
    }


    /**
     * 计算水平分页的数量
     *
     * @return
     */
    @Override
    public int getCount() {
        int count = datas.size() % 6;
        int divide = datas.size() / 6;
        return count == 0 ? divide : divide + 1;
    }

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

    @Override
    public RecyclerView instantiateItem(ViewGroup container, int position) {

        RecyclerView recyclerView = new RecyclerView(context);
        //2行显示。列数量通过item的宽度来控制,显示3列
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 2, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(gridLayoutManager);
        GoodsDetailsHotGridAdapter hotGridAdapter = new GoodsDetailsHotGridAdapter(context);
        recyclerView.setAdapter(hotGridAdapter);

        List<GoodsVo> list = new ArrayList<GoodsVo>();
        //每页最多显示6个,小于数据集总数,且小于下一页开始的位置索引
        for (int i = position * 6; i < (position + 1) * 6 && i < datas.size(); i++) {
            list.add(datas.get(i));
        }

        //初始化指示器。position == 0只初始化一次;且有多页;
        for (int i = 0; position == 0 && getCount() != 1 && i < getCount(); i++) {
            View indicator = new View(context);
            Drawable drawable = context.getResources().getDrawable(R.drawable.indicator_selector);
            indicator.setBackground(drawable);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(24, 24);
            params.setMarginEnd(10);
            llIndicators.addView(indicator, params);
            indicators.add(indicator);
        }

        hotGridAdapter.setDatas(list);
        hotGridAdapter.notifyDataSetChanged();
        container.addView(recyclerView);
        return recyclerView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeViewAt(position);
    }
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值