美文网首页
Android-RecyclerView实现上拉加载更多及下拉刷

Android-RecyclerView实现上拉加载更多及下拉刷

作者: hao_developer | 来源:发表于2021-08-26 10:58 被阅读0次
    image.png
    image.png
    image.png

    activity_main.xml

     <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/sr_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
     
            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_home"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
        </android.support.v4.widget.SwipeRefreshLayout>
    

    item的布局 item_rv.xml 只有一个简单的TextView

    item底部item_foot.xml

      <ImageView
            android:id="@+id/iv_foot"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/icon"/>
        <TextView
            android:id="@+id/tv_foot"
            android:layout_toRightOf="@id/iv_foot"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center"/>
    

    看下Activity,下拉刷新主要是通过 SwipeRefreshLayout嵌套RecyclerView,实现OnRefreshListener方法,再重新设置数据给Recyclerview达到刷新数据的目的,这里我通过模拟数据达到刷新的效果,注释都有写。上拉加载更多则是通过监听Recyclerview的滑动(OnScrollListener),当其滑动到底部时对其原有数据进行增加达到加载更多数据的目的。

      private RecyclerView recyclerView;
        private SwipeRefreshLayout swipeRefreshLayout;
        private List<String> mList;
        private RecyclerAdapter adapter;
        private int reTimes = 0;//更新次数
     
        @Override
     
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            getData();
            initRecyclerView();
        }
     
        private void initView() {
            recyclerView = findViewById(R.id.rv_home);
            swipeRefreshLayout = findViewById(R.id.sr_refresh);
            swipeRefreshLayout.setOnRefreshListener(this);
            mList = new ArrayList<>();
        }
     
        /**
         * 初始化RecyclerView
         */
        private void initRecyclerView() {
            adapter = new RecyclerAdapter(mList, this);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            recyclerView.setItemAnimator(new DefaultItemAnimator());
            recyclerView.setAdapter(adapter);
            recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    //滑动到底部
                    if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                        //recyclerview滑动到底部,更新数据
                        //加载更多数据
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (reTimes < 2) {
                                    getMoreData();
                                    //告诉他有更多数据
                                    adapter.hasMore(true);
                                    adapter.notifyDataSetChanged();
                                } else {
                                    //没有数据了
                                    adapter.hasMore(false);
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        }, 3000);
                    }
                    else {//滑上去了
                        adapter.hasMore(true);
    //                    adapter.notifyDataSetChanged();
                    }
                }
            });
     
        }
     
        //刷新事件
        @Override
        public void onRefresh() {
            //显示刷新动画效果
            swipeRefreshLayout.setRefreshing(true);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //清空数据
                    adapter.reData();
                    //重新获取数据
                    getData();
                    //更新的数据
                    upData();
                    //重新设置有更多数据
                    adapter.hasMore(true);
                    reTimes = 0;
                    adapter.notifyDataSetChanged();
                    //隐藏刷新效果
                    swipeRefreshLayout.setRefreshing(false);
                }
            }, 3000);
        }
     
        //获取数据
        public void getData() {
            for (int i = 0; i < 20; i++) {
                mList.add(i + "");
            }
        }
     
        //加载更多数据
        public void getMoreData() {
            reTimes++;
            List<String> moreList = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                moreList.add("加载的数据" + i);
            }
            mList.addAll(moreList);
        }
     
        //更新数据
        private void upData() {
            for (int i = 0; i < 5; i++) {
                mList.add(0, "更新的数据" + i);
            }
        }
    

    设配器,Recyclerview的item有2种,一种是正常的Item,另一种则是底部加载更多的Item(Foot),我们通过getItemViewType来判断返回的是哪种Item。

        private List<String> mDatas;//数据
        private int TYPE_ITEM = 0;//正常的Item
        private int TYPE_FOOT = 1;//尾部刷新
     
        private Context mContext;
     
        private boolean hasMore = true;
     
        public RecyclerAdapter(List<String> mDatas, Context mContext) {
            this.mDatas = mDatas;
            this.mContext = mContext;
        }
     
        //返回不同布局
        @Override
        public int getItemViewType(int position) {
            if (position == getItemCount() - 1) {
                return TYPE_FOOT;
            }
            return TYPE_ITEM;
        }
     
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_ITEM) {
                return new ItemHolder(LayoutInflater.from(mContext).inflate(R.layout.item_rv, parent, false));
            } else {
                return new FootHolder(LayoutInflater.from(mContext).inflate(R.layout.item_foot, parent, false));
            }
        }
     
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ItemHolder) {
                ((ItemHolder) holder).tv_item.setText("测试" + mDatas.get(position));
            } else {
                if (hasMore){
                    ((FootHolder) holder).tv_foot.setText("正在加载更多。。");
                    ((FootHolder) holder).iv_foot.setVisibility(View.VISIBLE);
                }else {
                    ((FootHolder) holder).tv_foot.setText("没有更多数据。。");
                    ((FootHolder) holder).iv_foot.setVisibility(View.GONE);
                }
            }
        }
     
        //多出尾部刷新的item
        @Override
        public int getItemCount() {
            return mDatas.size() + 1;
        }
     
        //item的holder
        class ItemHolder extends RecyclerView.ViewHolder {
            private TextView tv_item;
     
            public ItemHolder(View itemView) {
                super(itemView);
                tv_item = itemView.findViewById(R.id.tv_item);
            }
        }
     
        class FootHolder extends RecyclerView.ViewHolder {
            private TextView tv_foot;
            private ImageView iv_foot;
     
            public FootHolder(View itemView) {
                super(itemView);
                tv_foot = itemView.findViewById(R.id.tv_foot);
                iv_foot = itemView.findViewById(R.id.iv_foot);
            }
        }
     
        //是否有更多数据
        public void hasMore(boolean hamore) {
            this.hasMore = hamore;
        }
     
        //这里清空数据,避免下拉刷新后,还显示上拉加载的数据
        public void reData(){
            mDatas.clear();
        }
    

    相关文章

      网友评论

          本文标题:Android-RecyclerView实现上拉加载更多及下拉刷

          本文链接:https://www.haomeiwen.com/subject/btreiltx.html