RecycleView实现上滑下拉加载新的数据 并将某一条数据置顶

本次demo写的较为简单,主要是使用了RecycleView自带的addOnScrollListener方法
代码如下:
MainActivity中:


public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecommendView;
    private ArrayList<VideoEntity> videoList = new ArrayList<VideoEntity>();
    private RecommendAdapter recommendAdapter = new RecommendAdapter(MainActivity.this, videoList);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //如果是横屏
            setContentView(R.layout.activity_main_horizontal);

        } else {
            setContentView(R.layout.activity_main_vertical);
            for (int i = 0; i < 20; i++) {
                VideoEntity videoEntity = new VideoEntity(i + "", i + "hhhhhhhhhh");
                videoList.add(videoEntity);
            }
            mRecommendView = findViewById(R.id.recommend_list);
            LinearLayoutManager linearLayoutManagerNew = new LinearLayoutManager(MainActivity.this);
            mRecommendView.setLayoutManager(linearLayoutManagerNew);
            mRecommendView.setAdapter(recommendAdapter);
            initRvCommendNew(linearLayoutManagerNew);

        }

    }

    protected void initRvCommendNew(LinearLayoutManager linearLayoutManagerNew) {
        mRecommendView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //获取当前页面第一个item的posion值
                int visibleFirst = linearLayoutManagerNew.findFirstVisibleItemPosition();
                //获取当前页面最后一个item的posion值
                int visibleLast = linearLayoutManagerNew.findLastVisibleItemPosition();
                if (visibleFirst <= 0) {
                    recommendAdapter.loadPreData();
                }else if(visibleLast >= recommendAdapter.getItemCount() -1) {
                    recommendAdapter.loadNextData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
}

在Adapter中要写好上滑和下拉时数据的处理更新


public class RecommendAdapter extends RecyclerView.Adapter<RecommendHolder> {
    private Context mContext;
    private ArrayList<VideoEntity> videoList;

    public RecommendAdapter(Context mContext, ArrayList<VideoEntity> videoList) {
        this.mContext = mContext;
        this.videoList = videoList;
    }

    @Override
    public RecommendHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_recommend, null);
        RecommendHolder myViewHoder = new RecommendHolder(view);
        return myViewHoder;

    }

    @Override
    public void onBindViewHolder(RecommendHolder holder, int position) {
        if (videoList.get(position) != null) {
            holder.bindData(videoList.get(position));
        }


    }

    @Override
    public int getItemCount() {
        return videoList.size();
    }


    public void loadNextData() {
        ArrayList<VideoEntity> nextVideoList = new ArrayList<VideoEntity>();
        for (int i = 0; i < 5; i++) {
            VideoEntity vEntity = new VideoEntity(i+"",i+"xxxxxxxxx");
            videoList.add(vEntity);
        }
        notifyItemInserted(this.getItemCount());

    }
    public void loadPreData() {
        ArrayList<VideoEntity> nextVideoList = new ArrayList<VideoEntity>();
        for (int i = 0; i < 5; i++) {
            VideoEntity vEntity = new VideoEntity(i+"",i+"xxxxxxxxx");
            videoList.add(0,vEntity);
        }
        notifyItemChanged(0);

    }
}

最后 该写法在下拉刷新的时候会出现bug 应该是刷新时机有一些问题。

置顶


protected LinearLayoutManager linearLayoutManagerNew;

linearLayoutManagerNew = new LinearLayoutManager(rootView.getContext());
linearLayoutManagerNew.scrollToPosition(3);// 列表滚动到第三个item

通过LinearLayoutManager的scrollToPosition方法 让列表滚动到第几个,由此实现置顶

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值