平时我们写listView的时候都要写Adater
public class Main2Activity extends AppCompatActivity {
String[] arry = {"张三","里斯"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(new MyAdapter(getLayoutInflater()));
}
public class MyAdapter extends BaseAdapter{
private LayoutInflater mInflater;//定义Inflater,加载我们自定义的布局。
/*
定义构造器,在Activity创建对象Adapter的时候将数据data和Inflater传入自定义的Adapter中进行处理。
*/
public MyAdapter(LayoutInflater inflater){
mInflater = inflater;
}
@Override
public int getCount() {
return arry.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertview, ViewGroup viewGroup) {
//获得ListView中的view
if (convertview == null){
convertview = mInflater.inflate(R.layout.slide_layout,null);
TextView textView = convertview.findViewById(R.id.text);
MyViewHolder viewHolder = new MyViewHolder(textView);
convertview.setTag(viewHolder);
}
else
{
MyViewHolder myViewHolder = (MyViewHolder)convertview.getTag();
myViewHolder.getTextView().setText(arry[position]);
}
return convertview ;
}
}
class MyViewHolder {
TextView textView;
public MyViewHolder(TextView textView) {
this.textView = textView;
}
public TextView getTextView() {
return textView;
}
}
}
像上述一样,一个listView就要写一个Adapter,那么有没有方法写一个通用的Adapter,即时数据源改变了,item布局改变了依然可以使用??
具体思路参考博客:https://blog.csdn.net/lmj623565791/article/details/38902805。这篇是CSDN大神鸿洋写的,思路非常清晰,多看几遍就能懂,有兴趣可以看看,没兴趣可以直接copy代码拿走。
/**
* @Author: david.lvfujiang
* @Date: 2019/10/30
* @Describe:
*/
public class ViewHolder
{
private final SparseArray mViews;
private View mConvertView;
private ViewHolder(Context context, ViewGroup parent, int layoutId,
int position)
{
this.mViews = new SparseArray();
mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
false);
//setTag
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;
}
2.通用的CommonAdapter
/**
* @Author: david.lvfujiang
* @Date: 2019/10/30
* @Describe:
*/
public abstract class CommonAdapter extends BaseAdapter
{
protected LayoutInflater mInflater;
protected Context mContext;
protected List mDatas;
protected final int mItemLayoutId;
public CommonAdapter(Context context, List mDatas, int itemLayoutId)
{
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));
return viewHolder.getConvertView();
}
public abstract void convert(ViewHolder helper, T item);
private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent)
{
return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);
}
}
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
ListView mListView;
List mDatas;
private SwipeRefreshLayout swipeRefreshLayout;
CommonAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = (ListView) findViewById(R.id.id_lv_main);
mDatas = new ArrayList();
mDatas.add("哈哈哈哈哈");
mDatas.add("哈哈哈哈哈");
mDatas.add("哈哈哈哈哈");
mDatas.add("哈哈哈哈哈");
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.sr1);
//设置刷新圈的颜色
swipeRefreshLayout.setColorSchemeColors(Color.GREEN);
//设置监听
swipeRefreshLayout.setOnRefreshListener(this);
adapter = new CommonAdapter(
this, mDatas, R.layout.list_item) {
@Override
public void convert(ViewHolder c, String item) {
TextView view = c.getView(R.id.text);
ImageView imageView = c.getView(R.id.img);
imageView.setImageResource(R.mipmap.ic_launcher);
view.setText(item);
}
};
//设置适配器
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this,""+i,Toast.LENGTH_SHORT).show();
}
});
}
/**
* 下拉时刷新的方法
*/
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(false);
mDatas.add("哈哈哈哈");
//适配器刷新
adapter.notifyDataSetChanged();
}
}
demo的github地址:https://github.com/David-lvfujiang/ListViewAdapterApplication.git