recycleview的基础Adapter

Posted layfork

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了recycleview的基础Adapter相关的知识,希望对你有一定的参考价值。

.封装了一个基础的adapter.,用于recycleview的快捷使用
有BaseAdapter,BaseViewHolder,PAdapter,MainActivity

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    public List<T> mDatas;

    public LayoutInflater mLayoutInflater;
    public Context mContext;
    public int mLayout;



    public BaseAdapter(Context context,List<T> datas,int layout){
        this.mDatas=datas;
        this.mContext=context;
        this.mLayout=layout;
        mLayoutInflater =LayoutInflater.from(context);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=  mLayoutInflater.inflate(mLayout,null,false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        T t =getItem(position);

        bindData(holder,t);
    }

    public T getItem(int position){
        return mDatas.get(position);
    }

    @Override
    public int getItemCount() {
        if(mDatas!=null && mDatas.size()>0){
            return mDatas.size();
        }
        return 0;
    }


    public List<T> getmData() {
        return mDatas;
    }
    public void clearData(){
        if(mDatas.size()>0 && mDatas!=null){
            int presize= mDatas.size();
            mDatas.clear();
            notifyItemRangeRemoved(0,presize);
        }
    }



    public void addData(int position,List<T> datas){
        if (datas!=null && datas.size()>0){
            mDatas.addAll(datas);
            notifyItemRangeInserted(position,mDatas.size());
        }
    }


    public abstract void bindData(BaseViewHolder viewHolder,T t);
}
public class BaseViewHolder extends RecyclerView.ViewHolder {
    public SparseArray<View> views;


    public BaseViewHolder(View itemView) {
        super(itemView);
        views = new SparseArray<>();

    }


    public TextView  getTextView(int id){
        return findView(id);
    }

    public ImageView getImageView(int id){
        return findView(id);
    }

    public Button getButton(int id){
        return findView(id);
    }


    private <T extends View> T findView(int id){
        View view =views.get(id);
        if(view==null){
             view = itemView.findViewById(id);
             views.put(id,view);
        }
        return (T) view;
    }



}
public class PAdapter extends BaseAdapter<PayBean.Data> {

    public PAdapter(Context context, List<PayBean.Data> datas) {
        super(context, datas, R.layout.view_rv_item);
    }

    @Override
    public void bindData(BaseViewHolder viewHolder, final PayBean.Data data) {
        viewHolder.getTextView(R.id.tv_id).setText(data.getId());
        viewHolder.getTextView(R.id.tv_openid).setText(data.getOpenid());


        TextView tv_id= viewHolder.getTextView(R.id.tv_id);
        TextView tv_openid= viewHolder.getTextView(R.id.tv_openid);
        tv_openid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MyApplication.getContext(),data.getOpenid(),Toast.LENGTH_SHORT).show();
            }
        });
    }
}
public class MainActivity extends AppCompatActivity {
    private OkHttpHelper httpHelper  =OkHttpHelper.getInstance();
    private SwipeRefreshLayout swipeRefreshLayout;
    @ViewInject(R.id.tv)
    private TextView tv;

    private RecyclerView mRecyclerView;
    private ArrayList<String> mDatas=new ArrayList<>();
    private MaterialRefreshLayout materialRefreshLayout;

    private static final int STATE_NORMAL=0;
    private static final int STATE_REFRESH=1;
    private static final int STATE_MORE=2;
    private int state = STATE_NORMAL;
    private List<PayBean.Data> mData;

    private int page=1;
    private int pagesize=20;
    private int totalpages=0;
    private PAdapter payAdapter;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = findViewById(R.id.recycleview);
        materialRefreshLayout =findViewById(R.id.refresh);


        initRefreshLayout();
        getData();


    }


    private void initRefreshLayout(){
        materialRefreshLayout.setLoadMore(true);
        materialRefreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                refreshData();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                if(page<=totalpages){
                    loadMoreData();
                }else{
                    Toast.makeText(MyApplication.getContext(),"没有更多内容了",Toast.LENGTH_SHORT).show();
                    materialRefreshLayout.finishRefreshLoadMore();
                }
            }
        });
    }

    private void loadMoreData(){
        page=++page;
        
        state=STATE_MORE;
        getData();
    }

    private void refreshData(){
        page=1;
        state=STATE_REFRESH;
        getData();
    }

    private void getData(){
        String url=Api.BASE_URL+"&page="+page+"&pagesize="+pagesize;
        httpHelper.get(url, new BaseHttpCallback() {
            @Override
            public void onRequestBefore(Request request) {

            }

            @Override
            public void onSuccess(Response response, String string) {
                PayBean payBean =JSON.parseObject(string,PayBean.class);

                page= payBean.getPageindex();
                totalpages =payBean.getTotalpages();
                mData = payBean.getData();

                showData();

            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }

            @Override
            public void onFailure(Exception e) {

            }
        });
    }

    private void showData(){

        switch (state){
            case STATE_NORMAL:
                payAdapter = new PAdapter(getApplicationContext(),mData);

                mRecyclerView.setAdapter(payAdapter);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(MyApplication.getContext()));
                /*mRecyclerView.setAdapter(new BaseAdapter<PayBean.Data>(MyApplication.getContext(),mData,R.layout.view_rv_item) {

                    @Override
                    public void bindData(BaseViewHolder viewHolder, final PayBean.Data data) {
                        viewHolder.getTextView(R.id.tv_id).setText(data.getId());
                        viewHolder.getTextView(R.id.tv_openid).setText(data.getOpenid());

                        viewHolder.getTextView(R.id.tv_openid).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Toast.makeText(MyApplication.getContext(),data.getOpenid(),Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });*/
                //mRecyclerView.setLayoutManager(new LinearLayoutManager(MyApplication.getContext()));
                break;
            case STATE_REFRESH:
                payAdapter.clearData();
                payAdapter.addData(0,mData);
                mRecyclerView.scrollToPosition(0);
                materialRefreshLayout.finishRefresh();
                break;
            case STATE_MORE:
                payAdapter.addData(payAdapter.getmData().size(),mData);
                mRecyclerView.scrollToPosition(payAdapter.getmData().size());
                materialRefreshLayout.finishRefreshLoadMore();
                break;
            default:
                break;
        }

    }

}

2个布局文件

activity_main和view_rv_item

main
 <com.cjj.MaterialRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:wave_height_type="normal"
        app:isLoadMore="true"
        app:overlay="true"
        app:wave_show="true"
        >
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycleview"
            android:scrollbars="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </com.cjj.MaterialRefreshLayout>
    <LinearLayout
        android:id="@+id/item_layout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView
            android:id="@+id/tv_openid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

 

以上是关于recycleview的基础Adapter的主要内容,如果未能解决你的问题,请参考以下文章

[Android] Android RecycleView和ListView 自定义Adapter封装类

HeaderRecycleAdapter--通用的带头部RecycleView.Adapter

android 简单实现 recycleview,adapter 展示item,点击展示详情

一个Adapter+recycleview实现多种布局,区分布局中

Android RecycleView自定义布局的使用

RecycleView刷新数据时保持焦点不变