参考网上,自己总结的项目中经常用到的adapter
ListView
package com.wiseljz.dy.common;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by l.wang(516066490@qq.com) on 2017/7/18.
*/
public abstract class CommonAdapter<T> extends BaseAdapter
{
protected List<T> data;
protected Context context;
public CommonAdapter(Context context) {
this(context, null);
}
public CommonAdapter(Context context, List<T> data) {
this.context = context;
this.data = data;
}
@Override
public int getCount() {
return data == null ? 0 : data.size();
}
@Override
public T getItem(int position) {
if(data == null || position<0 ||position>=data.size())
{
return null;
}
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.get(context, convertView, parent,
getItemLayoutRes(position), position);
convert(holder, getItem(position));
return holder.convertView;
}
/**
* 设置adapter item layout resource id
*
* @return
*/
public abstract @LayoutRes
int getItemLayoutRes(int position);
/**
* 开发者实现该方法,进行业务处理
*/
public abstract void convert(ViewHolder holder, T item);
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
public static class ViewHolder {
private View convertView;
private SparseArray<View> views;
private int position;
public ViewHolder(Context context, ViewGroup parent, int layoutId,
int position) {
this.position = position;
this.views = new SparseArray<View>();
convertView = LayoutInflater.from(context).inflate(layoutId,
parent, false);
convertView.setTag(this);
}
public static ViewHolder get(Context context, View convertView,
ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new ViewHolder(context, parent, layoutId, position);
} else {
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.position = position;
return holder;
}
}
public View findViewById(int id) {
View v = views.get(id);
if (v == null) {
v = convertView.findViewById(id);
views.put(id, v);
}
return v;
}
public View getConvertView() {
return convertView;
}
public ViewHolder setText(int viewResId, String text) {
TextView tv = (TextView) findViewById(viewResId);
tv.setText(text);
return this;
}
public int getPosition() {
return position;
}
}
}
使用:
mNavAdapter = new CommonAdapter<Node>(context)
{
@Override
public int getItemLayoutRes(int position) {
return R.layout.item_nav1;
}
@Override
public void convert(ViewHolder holder, Node item) {
holder.setText(R.id.tv, item.getCname());
}
};
mNav.setAdapter(mNavAdapter);
Spinner
package com.wiseljz.dy.adapter;
import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.ThemedSpinnerAdapter;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.wiseljz.dy.common.CommonAdapter;
import java.util.List;
/**
* Created by l.wang(516066490@qq.com) on 2017/7/18.
*/
public abstract class SpinnerThemedAdapter<T> extends CommonAdapter<T> implements ThemedSpinnerAdapter
{
private final ThemedSpinnerAdapter.Helper mDropDownHelper;
public SpinnerThemedAdapter(Context context) {
this(context, null);
}
public SpinnerThemedAdapter(Context context, List<T> data) {
super(context, data);
mDropDownHelper = new ThemedSpinnerAdapter.Helper(context);
}
@Override
public void setDropDownViewTheme(@Nullable Resources.Theme theme) {
mDropDownHelper.setDropDownViewTheme(theme);
}
@Nullable
@Override
public Resources.Theme getDropDownViewTheme() {
return mDropDownHelper.getDropDownViewTheme();
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = ViewHolder.get(context, convertView, parent,
getDropDownItemLayoutRes(position), position);
dropdownConvert(holder,getItem(position));
return holder.getConvertView();
}
public abstract void dropdownConvert(ViewHolder holder, T item);
public abstract @LayoutRes int getDropDownItemLayoutRes(int position);
}
使用
mSalaryExpectAdapter = new SpinnerThemedAdapter<String>(context, Arrays.asList(getResources().getStringArray(R.array.salary_expect)))
{
@Override
public void dropdownConvert(ViewHolder holder, String item) {
holder.setText(R.id.tv, item);
}
@Override
public int getDropDownItemLayoutRes(int position) {
return R.layout.item_spinner_dropdown;
}
@Override
public int getItemLayoutRes(int position) {
return R.layout.item_spinner;
}
@Override
public void convert(ViewHolder holder, String item) {
holder.setText(R.id.tv, item);
}
};
mSpinnerSalaryExpect.setAdapter(mSalaryExpectAdapter);
RecycleView
package com.wiseljz.dy.common;
import android.support.v7.widget.RecyclerView;
import java.util.List;
/**
* Created by l.wang(516066490@qq.com) on 2017/7/18.
*/
public abstract class CommonRVAdapter<T> extends RecyclerView.Adapter
{
private List<T> data;
public CommonRVAdapter() {
}
public CommonRVAdapter(List<T> data) {
this.data = data;
}
@Override
public int getItemCount() {
return data == null ? 0 : data.size();
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
使用
mSubNav.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
mSubNavAdapter = new CommonRVAdapter<Node>()
{
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item_subnav, parent, false);
return new RecyclerView.ViewHolder(v)
{
};
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
ImageView iv = (ImageView) holder.itemView.findViewById(R.id.iv);
Utils.loadIntoUseFitWidth(context, ApiManager.IMG_HOST + getData().get(holder.getAdapterPosition()).getImg_url(), iv);
iv.findViewById(R.id.iv).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
mPresenter.onSubNavClick(getData().get(holder.getAdapterPosition()));
}
});
}
};
mSubNav.setAdapter(mSubNavAdapter);