RecyclerView实现上拉加载,下拉刷新,和侧滑删除

步骤:
1、首先实现下拉加载,基本没什么好说的,Google封装
2、上拉刷新,这面稍微麻烦一点,不过逻辑清楚了就好多了。
(1)、先写adapter,由于实现上拉加载底部和普通的数据滚动所以用两个变量来区分加载的ViewHolder。注意:重新getItemViewType,和getItemCount方法
(2)通过实现RecyclerView的addOnScrollListener方法来监听滚动条,查看是否到了最后一个item。

一、下拉刷新(PS:最容易实现)
layout SwipeRefreshLayout+RecyclerView实现的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/id_swiperefreshlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/id_delivery_address"
            android:divider="#ffff0000"
            android:dividerHeight="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

activitiy 中获取SwipeRefreshLayout, 调用setOnRefreshListener方法设置上拉监听

 mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                            @Override
                            public void onRefresh() {
                                //TODO 自己的逻辑方法
                                refreshData (REFRESH_DATA_TYPE);
                                //true和false代表开启和取消下拉的动画圆圈
                                mSwipeRefreshLayout.setRefreshing(false);
                            }
                        });

二、上拉加载(PS:一开始感觉很费劲,但是看了大大的代码还是很好的,逻辑清楚)
传送门: http://blog.csdn.net/huyongl1989/article/details/51789012
先上item代码,当然=-=天然性色盲,审美不全

<?xml version="1.0" encoding="utf-8"?>
<com.mcxtzhang.swipemenulib.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="3dp"
    android:clickable="true"
    app:ios="false"
    app:leftSwipe="true"
    app:swipeEnable="true"
    android:paddingTop="3dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:id="@+id/address_show_msg"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_weight="8"
            android:orientation="vertical">

            <TextView
                android:id="@+id/id_da_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="3dp"
                android:text="位置"
                android:textColor="@color/orangered"
                android:textSize="16dp"/>

            <TextView
                android:id="@+id/id_da_address"
                android:layout_width="match_parent"
                android:layout_height="44dp"
                android:text="地址"
                android:textColor="@color/black"
                android:textSize="18dp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/id_da_consignee"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.3"
                    android:text="姓名"
                    android:textSize="16dp" />

                <TextView
                    android:id="@+id/id_mobile_phone"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="电话号码"
                    android:textSize="16dp" />
            </LinearLayout>
        </LinearLayout>

        <ImageView
            android:id="@+id/id_edit_address"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:src="@drawable/edit"
            android:paddingRight="10dp"/>
    </LinearLayout>

    <!-- 以下都是侧滑菜单的内容依序排列 -->

    <Button
        android:id="@+id/id_btn_delete"
        android:layout_width="60dp"
        android:layout_height="match_parent"
        android:background="@color/red"
        android:text="删除"
        android:textColor="@android:color/white"/>
</com.mcxtzhang.swipemenulib.SwipeMenuLayout>

根据上面的步骤:
1、创建一个adapter,定义好需求的变量

        private static final int VIEW_TYPE_FOOTER = 0;//上拉加载
        private static final int VIEW_TYPE_ITEM = 1;//普通滚动
        private LoadStatus loadStatus = LoadStatus.LOAD_ING;//实现是否是加载中,用来中断
        private Context mContext;//这个基本没用到,不过习惯了
        private List<DataBean> dataBean;//数据集
        private LayoutInflater mInfalter;//用来加载布局
        //可以通过adapter设置变量来中断底部加载动画
        public void setLoadStatus(LoadStatus LoadStatus) {
            this.loadStatus = LoadStatus;
        }

2、重写getItemCount和getItemViewType方法

        @Override
        public int getItemCount() {
            //由于加载了底部item,所以判断item数量的时候加1条,
            return AssertUtil.isNotEmpty(deliveryAddressList) ? deliveryAddressList.size() + 1 : 0;
        }

        @Override
        public int getItemViewType(int position) {
            //判断是最后一条开启上拉加载
            if (position + 1 == getItemCount()) {
                return VIEW_TYPE_FOOTER;
            }
            return VIEW_TYPE_ITEM;
        }

3、初始化布局,根据viewType

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == VIEW_TYPE_FOOTER) {
                return new FooterViewHolder(
                    mInfalter.inflate(R.layout.footer_layout, parent, false));
            }
            else if(viewType == VIEW_TYPE_ITEM) {
                return new DeliverAddressVH(mInfalter.inflate(
                    R.layout.delivery_address_item, parent,false));
            }
            else {
                return null;
            }
        }

4、绑定数据,底部绑定的时候根据loadStatus来判断是否中断加载的动画

@Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            switch (getItemViewType(position)) {
                case VIEW_TYPE_ITEM:
                    onBindItemViewHolder(holder, position);
                    break;
                case VIEW_TYPE_FOOTER:
                    onBindFooterViewHolder(holder, position, loadStatus);
                    break;
                default:
                    break;
            }
        }

5、根据recyclerView的addOnScrollListener方法来监听滚动,从而实现上拉

deliveryAddressRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
    //状态改变时调用,设置上拉时一般判断是否松手。RecyclerView.SCROLL_STATE_IDLE用滚动停止来监听
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
         super.onScrollStateChanged(recyclerView, newState);
         //设置状态为加载中
         deliverAddressAdapter.setLoadStatus(LoadStatus.LOAD_ING);
         if ((newState == RecyclerView.SCROLL_STATE_IDLE) && 
                 (lastVisibleItem + 1 >= deliverAddressAdapter.getItemCount())) {
             //自动加载不设置上拉特效
             new Handler().postDelayed(new Runnable() {
                 @Override
                 public void run() {
                     refreshData (LOADMORE_DATA_TYPE);
                 }
             }, 1500);
         }
     }
    //该方法在滚动时就会改变
     @Override
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
         super.onScrolled(recyclerView, dx, dy);
         //查看最后一个可见item的位置
         lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
     }
  });

6、应该是第一步来说的~~习惯三连,设置adapter,设置recyclerView,设置layoutManager

deliverAddressAdapter = new 
            DeliverAddressAdapter(DeliveryAddressListActivity.this, deliveryAddress);
deliveryAddressRv.setAdapter(deliverAddressAdapter);
deliveryAddressRv.setLayoutManager(mLayoutManager);

上一下底部代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:orientation="horizontal"
    android:id="@+id/id_loading_layout">

    <ProgressBar
        android:layout_marginRight="6dp"
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/loading" />


</LinearLayout>

三、实现侧滑删除
根据前辈的gitHub指点,当然学习阶段直接抄不太好。。。。
传送门:https://github.com/mcxtzhang/SwipeDelMenuLayout

1、监听侧滑

private onSwipeListener mOnSwipeListener;
public void setOnDelListener(onSwipeListener mOnDelListener) {
     this.mOnSwipeListener = mOnDelListener;
 }

2、同时写入回调方法

    //TODO 左滑删除回调
    public interface onSwipeListener {
        void onDel(int pos);
    }

3、设置一个删除按钮绑定删除方法

deliverAddressVH.id_btn_delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
          if (null != mOnSwipeListener) {
              mOnSwipeListener.onDel(deliverAddressVH.getAdapterPosition());
          }
      }
  });

4、adapter设置删除监听

//TODO 设置左滑删除
deliverAddressAdapter.setOnDelListener(new onSwipeListener() {
      @Override
      public void onDel(int pos) {
          if (pos >= 0 && pos < deliveryAddress.size()) {
              Toast.makeText(DeliveryAddressListActivity.this,
                           "删除:" + pos, Toast.LENGTH_SHORT).show();
              deliveryAddress.remove(pos);
              deliverAddressAdapter.notifyItemRemoved(pos);//推荐用这个有动画效果
          }
      }
  });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值