[Android] Android RecycleView和ListView 自定义Adapter封装类
Posted wukong1688
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Android] Android RecycleView和ListView 自定义Adapter封装类相关的知识,希望对你有一定的参考价值。
在网上查看了很多对应 Android RecycleView和ListView 自定义Adapter封装类 的文章,主要存在几个问题:
一)、网上代码一大抄,复制来复制去,大部分都运行不起来,或者 格式错乱
二)、剩下的那些能运行起来的,将Adapter类、ViewHolder类,放在不同文件里,导致文件多、杂
于是总结这两个单独的Custom***ViewAdapter 类,以方便调用!
一、RecycleView Adapter自定义封装类
CustomRecyclerViewAdapter.java
package com.jack.androidbase.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; public abstract class CustomRecyclerViewAdapter<T> extends RecyclerView.Adapter<CustomRecyclerViewAdapter.ViewHolder> { private int mLayoutRes; private List<T> mData; //定义接口 OnItemClickListener public interface OnItemClickListener { void onItemClick(View view, int position); void onItemLongClick(View view, int position); } private OnItemClickListener mOnItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { mOnItemClickListener = onItemClickListener; } public CustomRecyclerViewAdapter(List<T> mData, int mLayoutRes) { this.mLayoutRes = mLayoutRes; this.mData = mData; } /** * inflate操作 */ @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ViewHolder viewHolder = ViewHolder.get(parent.getContext(), parent, mLayoutRes); return viewHolder; } /** * 暴露出来的接口 * * @param holder * @param obj */ public abstract void bindView(ViewHolder holder, T obj); @Override public void onBindViewHolder(final ViewHolder holder, int position) { bindView(holder, mData.get(position)); //绑定事件 if (mOnItemClickListener != null) { holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int pos = holder.getLayoutPosition(); mOnItemClickListener.onItemClick(holder.itemView, pos); } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int pos = holder.getLayoutPosition(); mOnItemClickListener.onItemLongClick(holder.itemView, pos); return false; } }); } } /** * 获取高度 * * @return */ @Override public int getItemCount() { if (mData == null) { return 0; } return mData.size(); } /** * ViewHolder */ public static class ViewHolder extends RecyclerView.ViewHolder { private SparseArray<View> mViews; private View mConvertView; public ViewHolder(Context context, View itemView, ViewGroup parent) { super(itemView); mConvertView = itemView; mViews = new SparseArray<View>(); } public static ViewHolder get(Context context, ViewGroup parent, int layoutid) { View itemView = LayoutInflater.from(context).inflate(layoutid, parent, false); ViewHolder holder = new ViewHolder(context, itemView, parent); return holder; } public <T extends View> T getView(int viewid) { View view = mViews.get(viewid); if (view == null) { view = mConvertView.findViewById(viewid); mViews.put(viewid, view); } return (T) view; } /** * 设置TextView文本 */ public ViewHolder setText(int viewId, CharSequence text) { View view = getView(viewId); if (view instanceof TextView) { ((TextView) view).setText(text); } return this; } /** * 设置View的Visibility */ public ViewHolder setViewVisibility(int viewId, int visibility) { getView(viewId).setVisibility(visibility); return this; } /** * 设置ImageView的资源 */ public ViewHolder setImageResource(int viewId, int drawableRes) { View view = getView(viewId); if (view instanceof ImageView) { ((ImageView) view).setImageResource(drawableRes); } else { view.setBackgroundResource(drawableRes); } return this; } } }
如何使用:
MainActivity.java 调用
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ui_recycler_view2); bindView(); } private void bindView() { mRecyclerView = (RecyclerView) findViewById(R.id.ui_basic_recycler_custom_1); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); mData = new ArrayList(); for (int i = 0; i < 10; i++) { mData.add("" + i); } //数据装载 myAdapter = new CustomRecyclerViewAdapter(mData, R.layout.item_recyler_1) { @Override public void bindView(ViewHolder holder, Object obj) { holder.setText(R.id.recycler_txt_1, obj.toString()); } }; //点击监听 listener1 = new CustomRecyclerViewAdapter.OnItemClickListener(){ @Override public void onItemClick(View view, int position) { Toast.makeText(getApplicationContext(), position + " click", Toast.LENGTH_SHORT).show(); } @Override public void onItemLongClick(View view, int position) { Toast.makeText(getApplicationContext(), position + " Long click", Toast.LENGTH_SHORT).show(); } }; mRecyclerView.setAdapter(myAdapter); myAdapter.setOnItemClickListener(listener1); }
activity_ui_recycler_view2.xml 布局文件
<?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".activity.***Activity"> <android.support.v7.widget.RecyclerView android:id="@+id/ui_basic_recycler_custom_1" android:layout_width="match_parent" android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView> </RelativeLayout>
二、ListView Adapter 自定义封装类
CustomListViewAdapter.java
package com.jack.androidbase.adapter; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.jack.androidbase.R; import java.util.ArrayList; import java.util.LinkedList; public abstract class CustomListViewAdapter<T> extends BaseAdapter { private ArrayList<T> mData; private int mLayoutRes; //布局id public CustomListViewAdapter(ArrayList<T> mData, int mLayoutRes) { this.mData = mData; this.mLayoutRes = mLayoutRes; } @Override public int getCount() { return mData != null ? mData.size() : 0; } @Override public T getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = ViewHolder.bind(parent.getContext(), convertView, parent, mLayoutRes , position); bindView(holder, getItem(position)); return holder.getItemView(); } public abstract void bindView(ViewHolder holder, T obj); //添加一个元素 public void add(T data) { if (mData == null) { mData = new ArrayList<>(); } mData.add(data); notifyDataSetChanged(); } //往特定位置,添加一个元素 public void add(int position, T data) { if (mData == null) { mData = new ArrayList<>(); } mData.add(position, data); notifyDataSetChanged(); } public void remove(T data) { if (mData != null) { mData.remove(data); } notifyDataSetChanged(); } public void remove(int position) { if (mData != null) { mData.remove(position); } notifyDataSetChanged(); } public void clear() { if (mData != null) { mData.clear(); } notifyDataSetChanged(); } public static class ViewHolder { private SparseArray<View> mViews; //存储ListView 的 item中的View private View item; //存放convertView private int position; //游标 private Context context; //Context上下文 //构造方法,完成相关初始化 private ViewHolder(Context context, ViewGroup parent, int layoutRes) { mViews = new SparseArray<>(); this.context = context; View convertView = LayoutInflater.from(context).inflate(layoutRes, parent, false); convertView.setTag(this); item = convertView; } //绑定ViewHolder与item public static ViewHolder bind(Context context, View convertView, ViewGroup parent, int layoutRes, int position) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(context, parent, layoutRes); } else { holder = (ViewHolder) convertView.getTag(); holder.item = convertView; } holder.position = position; return holder; } @SuppressWarnings("unchecked") public <T extends View> T getView(int id) { T t = (T) mViews.get(id); if (t == null) { t = (T) item.findViewById(id); mViews.put(id, t); } return t; } /** * 获取当前条目 */ public View getItemView() { return item; } /** * 获取条目位置 */ public int getItemPosition() { return position; } /** * 设置文字 */ public ViewHolder setText(int id, CharSequence text) { View view = getView(id); if (view instanceof TextView) { ((TextView) view).setText(text); } return this; } /** * 设置图片 */ public ViewHolder setImageResource(int id, int drawableRes) { View view = getView(id); if (view instanceof ImageView) { ((ImageView) view).setImageResource(drawableRes); } else { view.setBackgroundResource(drawableRes); } return this; } /** * 设置点击监听 */ public ViewHolder setOnClickListener(int id, View.OnClickListener listener) { getView(id).setOnClickListener(listener); return this; } /** * 设置可见 */ public ViewHolder setVisibility(int id, int visible) { getView(id).setVisibility(visible); return this; } /** * 设置标签 */ public ViewHolder setTag(int id, Object obj) { getView(id).setTag(obj); return this; } //其他方法可自行扩展 } }
如何使用:
MainActivity.java 调用
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ui_base_list_view_custom); mContext = UIBaseListViewCustomActivity.this; initList(); } private void initList() { list_book = (ListView) findViewById(R.id.ui_base_list_v2_1); //数据初始化 mData1 = new ArrayList<>(); mData1.add(new Animal("狗说", "你是狗么?", R.drawable.avatar_1)); mData1.add(new Animal("牛说", "你是牛么?", R.drawable.avatar_2)); mData1.add(new Animal("鸭说", "你是鸭么?", R.drawable.avatar_3)); //Adapter初始化 myAdapter1 = new CustomListViewAdapter<Animal>((ArrayList) mData1, R.layout.item_list_animal) { @Override public void bindView(ViewHolder holder, Animal obj) { holder.setImageResource(R.id.img_icon, obj.getaIcon()); holder.setText(R.id.txt_aName, obj.getaName()); } }; //ListView设置下Adapter: list_book.setAdapter(myAdapter1); list_book.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(mContext, "你点击了~" + position + "~项", Toast.LENGTH_SHORT).show(); } }); }
item_list_animal 布局文件 :
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".activity.UIBaseListViewActivity"> <ImageView android:id="@+id/img_icon" android:layout_width="64dp" android:layout_height="64dp" android:baselineAlignBottom="true" android:paddingLeft="8dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:paddingLeft="20dp" android:paddingTop="10dp"> <TextView android:id="@+id/txt_aName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#1D1D1C" android:textSize="20sp" /> <TextView android:id="@+id/txt_aSpeak" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#B4B4B9" android:textSize="14sp" /> </LinearLayout> </LinearLayout>
三、Github仓库代码:
https://github.com/wukong1688/AndroidBase/tree/master/app/src/main/java/com/jack/androidbase/adapter
如果对您有帮助,欢迎 点赞,欢迎Star,并转发!
本博客地址: wukong1688
本文原文地址: https://www.cnblogs.com/wukong1688/p/10661391.html
转载请著名出处!谢谢~~
以上是关于[Android] Android RecycleView和ListView 自定义Adapter封装类的主要内容,如果未能解决你的问题,请参考以下文章
1.Android recycleView万能分隔线 GridLayoutManager布局item左右间距均等(最易懂)