RecyclerView的Adapter萬能適配包
阿新 • • 發佈:2019-01-26
在Android L版本中,引入了appcompat新特性(RecyclerView,Toolbar,CardView,palette)
其中RecyclerView用來取代ListView和GridView
專案目錄:
從這裡看出,以後Recyclerview使用Adapter萬能適配,減少寫Adapter類(少了SimpleAdapter)
一.一個簡單的RecyclerView的demo(一般寫法)
1.新建Android Studio專案,在新建一個module叫simple,在simple的build.gradle加入
compile 'com.jesse.recyclerview:recyclerview:23.1.1'
引入recyclerview包
2.在activity_main.xml引入recyclerview佈局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.jesse.myapplication.MainActivity"> <!-- 引入RecyclerView佈局--> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
3.新建item_data.xml佈局,作為recyclerview的item佈局
<?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:id="@+id/tv_name" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
4.新建Data.java作為RecyclerView的資料Bean類
public class Data { private String name; private String title; public Data(String name, String title) { this.name = name; this.title = title; } public String getName() { return name; } public String getTitle() { return title; } }
5.新建SimpleAdapter.java作為RecyclerView的Adapter
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleHolder> { private Context mContext; private int mResID; private List<Data> mDatas; public SimpleAdapter(Context mContext, int mResID, List<Data> mDatas) { this.mContext = mContext; this.mResID = mResID; this.mDatas = mDatas; } @Override public SimpleHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view= LayoutInflater.from(mContext).inflate(mResID,parent,false); return new SimpleHolder(view);//載入item的佈局 } @Override public void onBindViewHolder(final SimpleHolder holder, int position) { //item的單擊事件 holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mOnItemClickListener != null) { int layoutPosition = holder.getLayoutPosition(); mOnItemClickListener.onItemClick(holder.itemView, layoutPosition); } } }); //設定item的子view設定資料 holder.tv_name.setText(mDatas.get(position).getName()); holder.tv_title.setText(mDatas.get(position).getTitle()); } @Override public int getItemCount() { return mDatas==null?0:mDatas.size(); } public class SimpleHolder extends RecyclerView.ViewHolder { private TextView tv_name; private TextView tv_title; public SimpleHolder(View itemView) { super(itemView); //初始化id tv_name= (TextView) itemView.findViewById(R.id.tv_name); tv_title= (TextView) itemView.findViewById(R.id.tv_title); } } public void addData(int pos, Data t) { mDatas.add(pos, t); // notifyDataSetChanged(); notifyItemInserted(pos); } public void addData(Data t) { mDatas.add(t); notifyDataSetChanged(); } public void removeData(int pos) { mDatas.remove(pos); // notifyDataSetChanged(); notifyItemRemoved(pos); } public interface OnItemClickListener { void onItemClick(View view, int position); } private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) { this.mOnItemClickListener = mOnItemClickListener; } }6.在MainActivity.java使用
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDatas(); assignViews(); initRecyclerview(); } private SimpleAdapter mAdapter; private void initRecyclerview() { //設定mRecyclerview的item向下排列 mRecyclerview.setLayoutManager(new LinearLayoutManager(this)); mAdapter=new SimpleAdapter(this,R.layout.item_data,mDatas); mRecyclerview.setAdapter(mAdapter); //設定mRecyclerview的item單擊事件 mAdapter.setOnItemClickListener(new SimpleAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(MainActivity.this,"toast"+position,Toast.LENGTH_SHORT).show(); } }); } private List<Data> mDatas; //初始化資料 private void initDatas() { mDatas=new ArrayList<>(); for (int i=0;i<10;i++){ mDatas.add(new Data("jesse"+i,"title"+i)); } } private RecyclerView mRecyclerview; //初始化佈局id private void assignViews() { mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview); } }
執行效果:
二:Adapter萬能適配的使用示例(減少 “一” 的第5步,也就是不用新建SimpleAdapter)
1.在app的module的build.gradle加入
compile 'com.jesse.recyclerview:recyclerview:23.1.1'
引入recyclerview包
2,3,4和 “一” 的2,3,4步一樣
5.在MainActivity.java使用
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initDatas(); assignViews(); initRecyclerview(); } private JesseAdapter<Data> mAdapter; /** * 設定Recyclerview */ private void initRecyclerview() { mRecyclerview.setLayoutManager(new LinearLayoutManager(this)); mAdapter=new JesseAdapter<Data>(this,R.layout.item_data,mDatas) { @Override public void onBindView(JesseHolder holder, int position) { TextView tv_name=holder.getView(R.id.tv_name); tv_name.setText(mDatas.get(position).getName()); TextView tv_title=holder.getView(R.id.tv_title); tv_title.setText(mDatas.get(position).getTitle()); } }; mRecyclerview.setAdapter(mAdapter); } private List<Data> mDatas; /** * 初始化資料 */ private void initDatas() { mDatas=new ArrayList<>(); for (int i=0;i<10;i++){ mDatas.add(new Data("jesse"+i,"title"+i)); } } private RecyclerView mRecyclerview; /** * 初始化佈局的id */ private void assignViews() { mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview); } }
執行效果和 “一” 的執行效果一樣
三.分享Adapter的萬能適配類的原始碼
JesseAdapter.java作為RecyclerView的Adapter的萬能適配
public abstract class JesseAdapter<T> extends RecyclerView.Adapter<JesseHolder> { protected Context mContext; protected int mLayout; protected List<T> mDatas; public JesseAdapter(Context mContext, int mLayout, List<T> mDatas) { this.mContext = mContext; this.mLayout = mLayout; this.mDatas = mDatas; } @Override public JesseHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(mLayout, parent, false); return new JesseHolder(view); } @Override public void onBindViewHolder(final JesseHolder holder, int position) { holder.getItemView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mOnItemClickListener != null) { int layoutPosition = holder.getLayoutPosition(); mOnItemClickListener.onItemClick(holder.getItemView(), layoutPosition); } } }); onBindView(holder, position); } public abstract void onBindView(JesseHolder holder, int position); @Override public int getItemCount() { return mDatas == null ? 0 : mDatas.size(); } public void addData(int pos, T t) { mDatas.add(pos, t); // notifyDataSetChanged(); notifyItemInserted(pos); } public void addData(T t) { mDatas.add(t); notifyDataSetChanged(); } public void removeData(int pos) { mDatas.remove(pos); // notifyDataSetChanged(); notifyItemRemoved(pos); } public interface OnItemClickListener { void onItemClick(View view, int position); } private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) { this.mOnItemClickListener = mOnItemClickListener; } }
JesseHolder作為RecyclerView的Holder
public class JesseHolder extends RecyclerView.ViewHolder { private final SparseArray<View> mViews; private View mItemView; public JesseHolder(View mItemView) { super(mItemView); this.mItemView = mItemView; this.mViews = new SparseArray<View>(); } public View getItemView() { return mItemView; } public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mItemView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } }
各位看官,沒事就評論下,你的評論,是我寫下去的動力。