自定义RecyclerView上拉加载更多和刷新

1:自定义recyclerView

public class XListView extends SwipeRefreshLayout implements IXListViewListener, IXListViewListener.OnItemClick {

    private RecyclerView recyclerView;

    private IXListViewListener ixListViewListener;//刷新及加载更多回掉

    private XListViewAdapter adapter;

    private boolean isLoadMore = true;//是否还能加载更多

    private static final String TAG = "XListView";

    private OnItemClick onItemClick;

    public XListView(Context context) {
        super(context);
        this.init(context);
    }

    public XListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    private void init(Context context) {
        this.recyclerView = new RecyclerView(context);
        final LinearLayoutManagerItem layoutManager = new LinearLayoutManagerItem(context);
        this.recyclerView.setLayoutManager(layoutManager);
        this.setColorSchemeColors(Color.RED, Color.GREEN, Color.YELLOW);
        this.recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {

            private int lastVisibleItem;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState != RecyclerView.SCROLL_STATE_IDLE || layoutManager == null || !isLoadMore || isRefreshing())
                    return;
                int state = adapter.getStatus();
                int visibleItemCount = layoutManager.getChildCount();//显示数量
                int totalItemCount = adapter.getXlistViewItemCount() + 1;//总数
                if (visibleItemCount > 0 && visibleItemCount <= totalItemCount && lastVisibleItem >= totalItemCount - 1 && XListViewAdapter.CLICK_MORE == state) {
                    onLoadMore();//加载更多
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = -1;
                if (layoutManager == null && !isLoadMore) {
                    return;
                }
                if (dy > 20) {
                    lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                }
            }
        });
        this.addView(this.recyclerView);
    }


    public void setLoadMore(boolean loadMore) {
        this.isLoadMore = loadMore;
    }

    /**
     * 加载更多完毕
     */
    public final void stopLoadMore(boolean isData) {
        if (this.adapter == null)
            return;
        if (isData)
            this.adapter.changeMoreStatus(XListViewAdapter.CLICK_MORE);
        else
            this.adapter.changeMoreStatus(XListViewAdapter.NO_DATA);
        this.setLoadMore(isData);

    }

    public final void setXListViewAdapter(XListView.XListViewAdapter adapter) {
        this.recyclerView.setAdapter(adapter);
        this.adapter = adapter;
        if (this.adapter != null)
            this.adapter.setIxListViewListener(this);
    }

    @Override
    public final void onRefresh() {
        if (ixListViewListener != null && !isLoadData())
            this.ixListViewListener.onRefresh();
        else
            this.setRefreshing(false);
    }

    @Override
    public final void onLoadMore() {
        if (this.ixListViewListener != null && !isRefreshing()) {
            this.ixListViewListener.onLoadMore();
            if (this.adapter != null)
                this.adapter.changeMoreStatus(XListViewAdapter.LOADING_MORE);
        }
    }

    @Override
    public final void onItemClick(RecyclerView parent, View view, int position, long id) {
        if (this.onItemClick != null)
            this.onItemClick.onItemClick(parent, view, position, id);
    }


    public final void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    public final void setOnRefreshListener(IXListViewListener ixListViewListener) {
        this.ixListViewListener = ixListViewListener;
        super.setOnRefreshListener(this);
        if (this.adapter != null)
            this.adapter.setIxListViewListener(this);

    }

    private boolean isLoadData() {
        return XListViewAdapter.LOADING_MORE == this.adapter.getStatus();
    }

    public class LinearLayoutManagerItem extends LinearLayoutManager {


        public LinearLayoutManagerItem(Context context) {
            super(context);
        }

        @Override
        public RecyclerView.LayoutParams generateDefaultLayoutParams() {
            return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }


    /**
     * 适配器,需要继承
     *
     * @param <VH>
     */
    public static abstract class XListViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter implements OnClickListener {

        private static final int TYPE_FOOTER = 10000;  //顶部FootView

        public static final int LOADING_MORE = 1;//正在加载中

        public static final int CLICK_MORE = 3;//点击加载更多数据

        public static final int NO_DATA = 2;//没有更多数据

        private int status = CLICK_MORE;//上拉加载更多状态-默认为0


        private XListView xListView;

        public Context getContext() {
            return xListView.getContext();
        }

        @Override
        public final int getItemViewType(int position) {
            int itemCount = getXlistViewItemCount();
            if (position == itemCount && xListView.isLoadMore) {
                return TYPE_FOOTER;
            } else {
                return getXlistItemViewType(position);
            }
        }

        @Override
        public final int getItemCount() {
            int count = getXlistViewItemCount();
            if (count <= 0)
                return count;
            else
                return count + (xListView.isLoadMore ? 1 : 0);
        }


        @Override
        public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == TYPE_FOOTER) {//显示底部布局
                View layout = LayoutInflater.from(getContext()).inflate(R.layout.footer_layout, null);
                FootViewHolder footViewHolder = new FootViewHolder(layout);
                footViewHolder.textView = (TextView) layout.findViewById(R.id.footer);
                layout.setOnClickListener(this);
                footViewHolder.progressBar = (ProgressBar) layout.findViewById(R.id.progressBar);
                return footViewHolder;
            }
            RecyclerView.ViewHolder holder = onXlistViewCreateViewHolder(parent, viewType);
            holder.itemView.setOnClickListener(this);
            return holder;
        }

        @Override
        public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            String tag = holder.getClass().getName();
            if (holder instanceof FootViewHolder) {
                FootViewHolder footViewHolder = (FootViewHolder) holder;
                String text = "上拉加载更多...";
                int visibility = View.GONE;
                switch (status) {
                    case LOADING_MORE:
                        text = "正在加载更多数据...";
                        visibility = View.VISIBLE;
                        break;
                    case NO_DATA:
                        text = "没有更多数据";
                        break;
                    case CLICK_MORE:
                        text = "点击加载更多";
                        break;
                }
                footViewHolder.progressBar.setVisibility(visibility);
                footViewHolder.textView.setText(text);
            } else {
                tag = TAG;
                onXlistViewBindViewHolder(holder, position);
            }
            holder.itemView.setTag(position + tag);
        }

        public abstract RecyclerView.ViewHolder onXlistViewCreateViewHolder(ViewGroup parent, int viewType);

        public abstract void onXlistViewBindViewHolder(RecyclerView.ViewHolder holder, int position);

        public abstract int getXlistViewItemCount();

        public int getXlistItemViewType(int position) {
            return super.getItemViewType(position);
        }

        public final void changeMoreStatus(int status) {
            this.status = status;
            this.notifyDataSetChanged();
        }

        public final int getStatus() {
            return status;
        }

        @Override
        public final void onClick(View v) {
            if (v == null)
                return;
            String tag = v.getTag().toString();
            if (TextUtils.isEmpty(tag))
                return;
            if (CLICK_MORE == status && xListView != null && tag.contains(FootViewHolder.class.getName())) {
                this.xListView.onLoadMore();//加载更多
                return;
            } else if (tag.contains(TAG)) {
                int position = -1;
                try {
                    position = Integer.parseInt(tag.replaceAll(TAG, "").trim());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (position < 0)
                    return;
                this.xListView.onItemClick(xListView.recyclerView, v, position, v.getId());
            }

        }

        public final void setIxListViewListener(XListView ixListViewListener) {
            this.xListView = ixListViewListener;
        }
    }

    /**
     * 底部加载显示view
     */
    static class FootViewHolder extends RecyclerView.ViewHolder {

        TextView textView;

        ProgressBar progressBar;

        public FootViewHolder(View itemView) {
            super(itemView);
        }
    }

}




2:刷新和家长更多,条目接口

public interface IXListViewListener extends SwipeRefreshLayout.OnRefreshListener {

    void onLoadMore();

    interface OnItemClick {
        void onItemClick(RecyclerView parent, View view, int position, long id);
    }

}


3:footer  item  (footer_layout.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="10dp">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="18dp"
        android:layout_height="16dp" />

    <TextView
        android:id="@+id/footer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="测试"
        android:textSize="14sp" />

</LinearLayout>


4:下载地址  http://download.csdn.net/detail/xingwei08246/9777153

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值