RecyclerView下拉刷新,上拉加载

RecyclerView - BaseRecyclerAdapt

2016-12-20  本文已影响390人  RickGe

本文未投入专题

BaseRecyclerAdapter继承自RecyclerView.Adapter,该类可作为RecyclerView的需要的Adapter的基类。

01 BaseRecyclerAdapter.java

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter {
    protected Context mContext;
    protected LayoutInflater mLayoutInflater;
    protected ArrayList<T> mItems;

    public static final int VIEW_TYPE_NORMAL = 0;
    public static final int VIEW_TYPE_FOOTER = -1;

    public static final int STATE_HIDE = 1;
    public static final int STATE_LOADING = 2;
    public static final int STATE_NO_MORE = 3;
    public static final int STATE_LOAD_ERROR = 4;

    private int mState;

    public BaseRecyclerAdapter(Context context) {
        this.mContext = context;
        mItems = new ArrayList<>();
        this.mLayoutInflater = LayoutInflater.from(mContext);
        mState = STATE_HIDE;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_FOOTER) {
            return new FooterViewHolder(mLayoutInflater.inflate(R.layout.recycler_footer_view, parent, false));
        } else {
            RecyclerView.ViewHolder holder = onCreateNormalViewHolder(parent, viewType);
            if(holder != null){
                holder.itemView.setTag(holder);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) v.getTag();
                        Toast.makeText(mContext, "Position: " + holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
            return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder.getItemViewType() == VIEW_TYPE_FOOTER) {
            onBindFooterViewHolder(holder);
        } else {
            onBindNormalViewHolder(holder, mItems.get(position), position);
        }
    }

    protected abstract RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType);
    
    protected abstract void onBindNormalViewHolder(RecyclerView.ViewHolder holder, T item, int position);

    private void onBindFooterViewHolder(RecyclerView.ViewHolder holder) {
        FooterViewHolder fvh = (FooterViewHolder) holder;
        fvh.itemView.setVisibility(View.VISIBLE);
        switch (mState) {
            case STATE_LOADING:
                fvh.tv_footer.setText(mContext.getResources().getString(R.string.state_loading));
                fvh.pb_footer.setVisibility(View.VISIBLE);
                break;
            case STATE_NO_MORE:
                fvh.tv_footer.setText(mContext.getResources().getString(R.string.state_no_more));
                fvh.pb_footer.setVisibility(View.GONE);
                break;
            case STATE_LOAD_ERROR:
                fvh.tv_footer.setText(mContext.getResources().getString(R.string.state_load_error));
                fvh.pb_footer.setVisibility(View.GONE);
                break;
            case STATE_HIDE:
                fvh.itemView.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mItems.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return VIEW_TYPE_FOOTER;
        } else {
            return VIEW_TYPE_NORMAL;
        }
    }

    public static class FooterViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.pb_footer) ProgressBar pb_footer;
        @BindView(R.id.tv_footer) TextView tv_footer;

        public FooterViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public void setState(int state) {
        if (this.mState != state) {
            this.mState = state;
            updateItem(getItemCount() - 1);
        }
    }

    public void updateItem(final int position) {
        if (getItemCount() > position) {

            //notifyItemChanged(position);

            // Cannot call this method in a scroll callback.
            // Scroll callbacks might be run during a measure & layout pass where you cannot change the RecyclerView data.
            // Any method call that might change the structure of the RecyclerView or the adapter contents should be postponed to the next frame.


            Handler handler = new Handler();
            Runnable r = new Runnable() {
                public void run() {
                    notifyItemChanged(position);
                }
            };
            handler.post(r);
        }
    }

    public void addAll(final List<T> items) {
        if (items != null) {
            int positionStart = this.mItems.size();
            this.mItems.addAll(items);
            notifyItemRangeInserted(positionStart, items.size());
        }
    }

    public void clear() {
        this.mItems.clear();
        notifyDataSetChanged();
    }

}

02 recycler_footer_view.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="46dp"
    android:gravity="center"
    android:minHeight="46dp"
    android:orientation="horizontal">
    <ProgressBar
        android:id="@+id/pb_footer"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp" />
    <TextView
        android:id="@+id/tv_footer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="正在加载中..." />
</LinearLayout>

03 strings.xml

<resources>
    <string name="state_loading">正在加载…</string>
    <string name="state_no_more">没有更多数据</string>
    <string name="state_load_error">加载失败</string>
</resources>

04 一个继承了BaseRecyclerAdapter的class

public class TweetAdapter extends BaseRecyclerAdapter<Tweet>{

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

    @Override
    protected RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(mLayoutInflater.inflate(R.layout.item_list_tweet, parent, false));
    }

    @Override
    protected void onBindNormalViewHolder(RecyclerView.ViewHolder holder, Tweet item, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;

        Glide.with(mContext)
                .load(item.getAuthor().getPortrait())
                .asBitmap()
                .placeholder(R.mipmap.widget_face)
                .error(R.mipmap.widget_face)
                .into(viewHolder.mViewPortrait);

        viewHolder.mViewName.setText(item.getAuthor().getName());
        viewHolder.mViewTime.setText(StringUtil.formatSomeAgo(item.getPubDate()));
        viewHolder.mViewPlatform.setText(getAppClientName(item.getAppClient()));        
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.iv_tweet_face) CircleImageView mViewPortrait;
        @BindView(R.id.tv_tweet_name) TextView mViewName;
        @BindView(R.id.tv_tweet_time) TextView mViewTime;
        @BindView(R.id.tv_tweet_platform) TextView mViewPlatform;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private String getAppClientName(int appClient){
        String appClientName = "";
        switch (appClient){
            case 3 :
                appClientName = "Android";
                break;
            case 4 :
                appClientName = "iPhone";
                break;
        }
        return appClientName;
    }
}

05 参考来源

程序代码参考自开源中国App

上一篇 下一篇

猜你喜欢

热点阅读