我相信一般写安卓的,写适配器避免不了这样的写法,这也是非常正常的行数
其实本质上就是把很多东西分离出去了,把其他的当成工具类来使用。
效果是一样的
这里是调用
package com.example.acer.mymusic.Adapter;
import android.content.Context;
import com.example.acer.mymusic.R;
import com.example.acer.mymusic.Utils.info_array_Util;
import java.util.List;
/**
* Created by kang on 2018/4/2.
*/
public class LuageAdapterextends CommonAdapter
{
public LuageAdapter(Context context, List datas) {
super(context, datas, R.layout.content_item);
}
@Override
protected void convert(ViewHolder viewHolder, info_array_Util info_array_util,int position)
{
info_array_Util luageModel= (info_array_Util) getItem(position);
viewHolder.setText(R.id.tv_item_one,luageModel.getTitle()).setText(R.id.tv_item_two,luageModel.getText());
}
}
这里只是支持链式调用,如果不想这样写的,也可以单独的把他提出来。
package com.example.acer.mymusic.Adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.List;
public abstract class CommonAdapterextends BaseAdapter
{
// 显示的是谁的数据 团队列表TeamBean
protected ContextmContext;
protected ListmDatas;
private int layoutId;
public CommonAdapter(Context context,List datas,int layoutId){
this.mContext = context;
this.mDatas = datas;
this.layoutId = layoutId;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public Object 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) {
ViewHolder viewHolder = ViewHolder.getHolder(mContext,convertView,layoutId);
// 把设置参数的内容传递给子类
convert(viewHolder,mDatas.get(position),position);
return viewHolder.getConvertView();
}
protected abstract void convert(ViewHolder viewHolder,T t,int position);
}
package com.example.acer.mymusic.Adapter;
import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class ViewHolder {
// 实例化我们的convertView
private ViewconvertView;
// 存储View
// Map mViews = new HashMap<>();
// SparseArrays map integers to Objects. Unlike a normal array of Objects, there can be gaps in the indices.
// It is intended to be more efficient than using a HashMap to map Integers to Objects.
SparseArraymViews =new SparseArray();
public ViewHolder(Context context, View convertView,int layoutId) {
// 实例化我们的convertView
convertView = View.inflate(context,layoutId,null);
// 设置 convertView.setTag(viewHolder);
convertView.setTag(this);
this.convertView = convertView;
}
public static ViewHolder getHolder(Context context,View convertView,int layoutId){
if(convertView ==null){
ViewHolder viewHolder =new ViewHolder(context,convertView, layoutId);
return viewHolder;
}else{
return (ViewHolder)convertView.getTag();
}
}
public View getConvertView(){
return convertView;
}
/**
* 设置文本
* @param viewId 根布局里面的Id
* @param item 给该TextView设置的内容
* @return ViewHolder 为了给调用者 可以用链式调用
*/
public ViewHolder setText(int viewId, String item) {
// 减少findViewById的次数
TextView tv = getView(viewId);
tv.setText(item);
return this;
}
/**
* 设置资源图片
* @param viewId 根布局里面的Id
* @param resourceId 给该ImageView设置的资源
*/
public ViewHolder setImageResource(int viewId,int resourceId) {
ImageView iv = getView(viewId);
iv.setImageResource(resourceId);
return this;
}
// /**
// * 设置网络图片
// * @param viewId 根布局里面的Id
// * @param url 网络图片路径
// */
// public ViewHolder setImageUrl(int viewId, String url) {
// ImageView iv = getView(viewId);
// // 加载网络图片
// ImageLoadCouplingUtil.getInstance().loadImage(url,iv);
// return this;
// }
/**
* 根据View的Id 获取 View
*/
public T getView(int viewId){
// 去缓存集合中找
View view = (View)mViews.get(viewId);
if(view ==null){
// 如果是第一次
view =convertView.findViewById(viewId);
mViews.put(viewId,view);
}
return (T) view;
}
}