先看 BaseViewHolder类
import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.View; public class BaseViewHolder extends RecyclerView.ViewHolder { private SparseArray<View> views; public BaseViewHolder(@NonNull View itemView) { super(itemView); this.views = new SparseArray<>(); } //通过id获取view ,SparseArray中是否已经有这个view,没有则添加 public <T extends View> T getView(int viewId) { View view = views.get(viewId); if (view == null) { view = itemView.findViewById(viewId); views.put(viewId, view); } return (T) view; } public View getRootView() { return itemView; } }
2.BaseAdapter
import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; //提供一个泛型的List数据集合 public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> { private List<T> mData; protected Context context; private LayoutInflater layoutInflater; private static final int ERROR_CODE = 7011; protected OnItemClickListener onItemClickListner;//单击事件 public BaseAdapter(Context context, List<T> list){ this.mData = list; this.context = context; layoutInflater =LayoutInflater.from(context); } //根据itemviewtype类型返回布局类型 @NonNull @Override public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { if (getItemViewType(i) == ERROR_CODE){ BaseViewHolder holder = new BaseViewHolder(layoutInflater.inflate(noDataLayoutId(),viewGroup,false)); return holder; } else { BaseViewHolder holder = new BaseViewHolder(layoutInflater.inflate(getLayoutId(),viewGroup,false)); return holder; } } @Override public void onBindViewHolder(@NonNull BaseViewHolder baseViewHolder, int i) { if (getItemViewType(i) == ERROR_CODE){ noData(baseViewHolder,i); } else { bindData(baseViewHolder,mData.get(i),i); } } @Override public int getItemCount() { return mData != null ? mData.size() : 1; } @Override public int getItemViewType(int position) { //list为null或者list为空则显示无数据布局 if (mData == null || mData.isEmpty()){ return ERROR_CODE; } return super.getItemViewType(position); } //抽象方法,默认数据加载布局 protected abstract int getLayoutId(int position); //无数据布局 protected abstract int noDataLayoutId(); //操作有数据的泛型方法 protected abstract void bindData( BaseViewHolder holder, T t,final int position); //操作无数据的泛型方法 protected abstract void noData(BaseViewHolder holder,int position); //泛型item点击事件回调接口 T可以是lsit数据类型 protected interface OnItemClickListener<T> { void onItemClickListener(View v,T t, int position); } //点击事件回调方法 protected void setOnItemClickListener(OnItemClickListener onItemClickListner) { this.onItemClickListner = onItemClickListner; } }
3.使用
1.HomeAdapter
import android.content.Context; import android.view.View; import android.widget.TextView; import java.util.List; public class HomeAdapter extends BaseAdapter<String> { public HomeAdapter(Context context, List<String> list) { super(context, list); } @Override protected int getLayoutId() { return R.layout.home_item_layout; } @Override protected int noDataLayoutId() { return R.layout.no_data_item_layout; } @Override protected void bindData(BaseViewHolder holder, final String s, final int position) { final TextView tv_content = holder.getView(R.id.tv_content); tv_content.setText(s); tv_content.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (onItemClickListner != null){ onItemClickListner.onItemClickListener(tv_content,s,position); } } }); } @Override protected void noData(BaseViewHolder holder, int position) { } }
2.Activity
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private RecyclerView recycler_view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recycler_view = findViewById(R.id.recycler_view); recycler_view.setLayoutManager(new LinearLayoutManager(this)); List<String> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { list.add("i="+i); } HomeAdapter adapter = new HomeAdapter(this,list); adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<String>() { @Override public void onItemClickListener(View v, String o, int position) { Toast.makeText(MainActivity.this, o, Toast.LENGTH_SHORT).show(); } }); recycler_view.setAdapter(adapter); } @Override public void onClick(View v) { } }
3.布局
home_item_layout布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_content" android:layout_width="match_parent" android:layout_height="100px" android:text="2334324" android:gravity="center"/> </LinearLayout>
no_data_item_layout布局:
<?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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="暂无数据" android:gravity="center"/> </LinearLayout>
Activity布局:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </android.support.constraint.ConstraintLayout>
PS:简单封装,不喜勿喷。谢谢