依赖:recyclerview+glide图片加载器
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.github.bumptech.glide:glide:4.0.0'
1、ListView、GridView等适配器
CommonAdapter
public abstract class CommonAdapter extends BaseAdapter {
//关联的布局
protected LayoutInflater mInflater;
//上下文
protected Context mContext;
//数据源
protected List mDatas;
protected final int mItemLayoutId;
public CommonAdapter(Context context, int itemLayoutId, List mDatas) {
this.mContext = context;
this.mInflater = LayoutInflater.from(mContext);
this.mDatas = mDatas;
this.mItemLayoutId = itemLayoutId;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder = getViewHolder(position, convertView, parent);
convert(viewHolder, getItem(position),position);
return viewHolder.getConvertView();
}
public abstract void convert(ViewHolder holder, T item,int postion);
private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);
}
}
辅助类viewhodler
public class ViewHolder {
private final SparseArray mViews;
private int mPostion;
private View mConvertView;
private Context mContext;
private ViewHolder(Context context, ViewGroup parent, int layoutId, int position) {
this.mViews = new SparseArray<>();
this.mPostion = position;
this.mContext = context;
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent, false);
mConvertView.setTag(this);
}
/**
* 拿到一个ViewHolder对象
*
* @param context
* @param convertView
* @param parent
* @param layoutId
* @param position
* @return
*/
public static ViewHolder get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ViewHolder(context, parent, layoutId, position);
}
return (ViewHolder) convertView.getTag();
}
/**
* 通过控件的Id获取对于的控件,如果没有则加入views
*
* @param viewId
* @return
*/
public T getView(int viewId) {
View view = mViews.get(viewId);
if (view == null) {
view = mConvertView.findViewById(viewId);
mViews.put(viewId, view);
}
return (T) view;
}
public View getConvertView() {
return mConvertView;
}
/**
* 设置文本(可以自己添加设置image bitmap color等)
*/
public ViewHolder setText(int viewId, String text) {
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
}
2、RecyclerView适配器
CommonRecyclerAdapter
public abstract class CommonRecyclerAdapter extends RecyclerView.Adapter {
private Context context;//上下文
private List list;//数据源
private LayoutInflater inflater;//布局器
private int itemLayoutId;//布局id
private OnItemClickListener listener;//点击事件监听器
private OnItemLongClickListener longClickListener;//长按监听器
private RecyclerView recyclerView;
//在RecyclerView提供数据的时候调用
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
}
@Override
public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
super.onDetachedFromRecyclerView(recyclerView);
this.recyclerView = null;
}
/**
* 定义一个点击事件接口回调
*/
public interface OnItemClickListener {
void onItemClick(RecyclerView parent, View view, int position);
}
public interface OnItemLongClickListener {
boolean onItemLongClick(RecyclerView parent, View view, int position);
}
public CommonRecyclerAdapter(Context context, int itemLayoutId, List list) {
this.context = context;
this.list = list;
this.itemLayoutId = itemLayoutId;
inflater = LayoutInflater.from(context);
}
@Override
public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(itemLayoutId, parent, false);
return RecyclerHolder.getRecyclerHolder(context, view);
}
@Override
public void onBindViewHolder(final RecyclerHolder holder, int position) {
if (listener != null) {
holder.itemView.setBackgroundResource(R.mipmap.ic_launcher);//设置背景
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null && view != null && recyclerView != null) {
int position = recyclerView.getChildAdapterPosition(view);
listener.onItemClick(recyclerView, view, position);
}
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if (longClickListener != null && view != null && recyclerView != null) {
int position = recyclerView.getChildAdapterPosition(view);
longClickListener.onItemLongClick(recyclerView, view, position);
return true;
}
return false;
}
});
convert(holder, list.get(position), position);
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public void setOnItemLongClickListener(OnItemLongClickListener longClickListener) {
this.longClickListener = longClickListener;
}
/**
* 填充RecyclerView适配器的方法,子类需要重写
*
* @param holder ViewHolder
* @param item 子项
* @param position 位置
*/
public abstract void convert(RecyclerHolder holder, T item, int position);
}
RecyclerHolder
public class RecyclerHolder extends RecyclerView.ViewHolder {
private SparseArray views;
private Context context;
private RecyclerHolder(Context context, View itemView) {
super(itemView);
this.context = context;
this.views = new SparseArray<>();
}
/**
* 取得一个RecyclerHolder对象
*
* @param context 上下文
* @param itemView 子项
* @return 返回一个RecyclerHolder对象
*/
public static RecyclerHolder getRecyclerHolder(Context context, View itemView) {
return new RecyclerHolder(context, itemView);
}
public SparseArray getViews() {
return this.views;
}
/**
* 通过view的id获取对应的控件,如果没有则加入views中
*
* @param viewId 控件的id
* @return 返回一个控件
*/
@SuppressWarnings("unchecked")
public T getView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = itemView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
/**
* 设置字符串
*/
public RecyclerHolder setText(int viewId, String text) {
TextView tv = getView(viewId);
tv.setText(text);
return this;
}
/**
* 设置图片
*/
public RecyclerHolder setImageResource(int viewId, int drawableId) {
ImageView iv = getView(viewId);
iv.setImageResource(drawableId);
return this;
}
/**
* 设置图片
*/
public RecyclerHolder setImageBitmap(int viewId, Bitmap bitmap) {
ImageView iv = getView(viewId);
iv.setImageBitmap(bitmap);
return this;
}
/**
* 设置图片
*/
public RecyclerHolder setImageByUrl(int viewId, String url) {
RequestOptions requestOptions = new RequestOptions();
requestOptions.error(R.mipmap.ic_launcher);
requestOptions.placeholder(R.mipmap.ic_launcher);
Glide.with(context).load(url).apply(requestOptions).into((ImageView) getView(viewId));
return this;
}
}
3、RecyclerView使用方法,listview的几乎一致
3.1、定义一个数据实体类
public class DataBean {
/**
* 标题
*/
private String title;
/**
* 图片地址
*/
private String url;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public DataBean(String title, String url) {
this.title = title;
this.url = url;
}
}
3.2、定义一个adapter 继承CommonRecyclerAdapter放入数据源实现其抽象方法
public class MyAdapter extends CommonRecyclerAdapter<DataBean> {
public MyAdapter(Context context, int itemLayoutId, List list) {
super(context, itemLayoutId, list);
}
@Override
public void convert(RecyclerHolder holder, DataBean item, int position) {
}
}
其中关联布局可放在Activity中,个人喜欢放在里面
public class MyAdapter extends CommonRecyclerAdapter<DataBean> {
public MyAdapter(Context context, List list) {
super(context, R.layout.list_data_item, list);
}
@Override
public void convert(RecyclerHolder holder, DataBean item, int position) {
//标题
String title = item.getTitle();
holder.setText(R.id.item_title, TextUtils.isEmpty(title) ? "-" : title);//三木判断下返回是否为空
//图片地址
String url = item.getUrl();
holder.setImageByUrl(R.id.item_url, url);
}
}
主布局中Main2Activity
public class Main2Activity extends AppCompatActivity {
private RecyclerView recycler;
private List list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
recycler = (RecyclerView) this.findViewById(R.id.recycler);
if (list == null) {
list = new ArrayList<>();
}
//模拟10条假数据
for (int i = 0; i < 10; i++) {
list.add(new DataBean("android", "http://inews.gtimg.com/newsapp_bt/0/876781763/1000"));
}
MyAdapter adapter = new MyAdapter(this, list);
recycler.setLayoutManager(new LinearLayoutManager(this));
recycler.setAdapter(adapter);
adapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
@Override
public void onItemClick(RecyclerView parent, View view, int position) {
Toast.makeText(Main2Activity.this,"点击事件"+position,Toast.LENGTH_SHORT).show();
}
});
adapter.setOnItemLongClickListener(new CommonRecyclerAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(RecyclerView parent, View view, int position) {
Toast.makeText(Main2Activity.this,"长按事件"+position,Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
activity_main2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler"/>
LinearLayout>
item布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_title"/>
<ImageView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/item_url"/>
LinearLayout>