刚开始接触android的时候,有些东西并不会在意,可能自己并没有写太多的原因,随着项目越来越大,我们重复的工作做得越来越多,这个时候我们就会想着怎么才能简化我们的代码,android中的ListView用的还是很多的,所以就做了一个简化的万能适配器Adapter,主要就是利用JAVA中的命令模式进行开发的,如果有特别的功能,可自行在代码里添加修改,经过修改,本适配器可以多布局进行显示
这个是实例
public class MainActivity extends AppCompatActivity {
private ListView list_view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list_view = findViewById(R.id.list_view);
List<String> data = new ArrayList<>();
for (int i = 0; i < 30; i++) {
data.add("第" + i);
}
final Adapter<String> adapter = new Adapter<>(getApplicationContext());
adapter.setData(data);
adapter.addLayout(0, R.layout.item1);
adapter.addLayout(1, R.layout.item2);
adapter.setOnGetLayoutType(new OnGetLayoutType<String>() {
@Override
public int getLayoutType(int postion, List<String> data) {
return postion % 2 == 0 ? 0 : 1;
}
});
adapter.setOnGetView(new OnGetView<String>() {
@Override
public void getView(ViewHolder viewHolder, final int position, List<String> data) {
TextView textView = viewHolder.getView(R.id.text);
textView.setTextColor(Color.BLACK);
textView.setText(data.get(position));
RadioButton radio = viewHolder.getView(R.id.radio);
radio.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter.updateData(list_view, position, "我");
}
});
}
});
list_view.setAdapter(adapter);
}
public static class Adapter<T> extends BaseAdapter {
private Context context;
private List<T> data;
private OnGetView<T> onGetView;
private OnGetLayoutType<T> onGetLayoutType;
private Map<Integer, Integer> layoutTypeMap;
Adapter(Context context) {
this.context = context;
data = new ArrayList<>();
layoutTypeMap = new HashMap<>();
}
@Override
public int getViewTypeCount() {
if (layoutTypeMap.size() <= 0) {
return 0;
}
return layoutTypeMap.size();
}
@Override
public int getItemViewType(int position) {
return onGetLayoutType.getLayoutType(position, this.data);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
ViewHolder viewHolder = null;
//因为考虑到item布局重用的情况,优化ListView
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(layoutTypeMap.get(type), parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (onGetView != null) {
onGetView.getView(viewHolder, position, this.data);
}
return viewHolder.getConvertView();
}
//获取数据
public List<T> getData() {
return this.data;
}
//设置数据
public void setData(List<T> data) {
if (data == null) {
data = new ArrayList<T>();
}
this.data = data;
this.notifyDataSetChanged();
}
//更新数据
public void updateData(List<T> data) {
if (data == null) {
data = new ArrayList<T>();
}
this.data = data;
this.notifyDataSetChanged();
}
//添加数据
public void addData(List<T> data) {
if (this.data == null) {
data = new ArrayList<T>();
}
this.data.addAll(data);
this.notifyDataSetChanged();
}
//局部更新数据
public void updateData(ListView listView, int position, T data) {
if (data != null) {
if (this.data == null) {
this.data = new ArrayList<T>();
}
this.data.set(position, data);
//获得第一条数据
int firstVisiblePosition = listView.getFirstVisiblePosition();
//获取最后一条数据
int lastVisiblePosition = listView.getLastVisiblePosition();
if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
/**获取指定位置view对象 ,如果存在头部布局,所以减去头布局**/
View view = listView.getChildAt(position - firstVisiblePosition);
getView(position, view, listView);
}
}
}
//添加布局--两者都不能为空
public void addLayout(int type, int layoutId) {
layoutTypeMap.put(type, layoutId);
}
public void setOnGetView(OnGetView<T> onGetView) {
this.onGetView = onGetView;
}
//新建一个接口-把给用户的操作集中到一块,给用户操作,采用java的命令模式,可自行学习
public interface OnGetView<T> {
void getView(ViewHolder viewHolder, final int position, List<T> data);
}
//用于返回指定布局的类型
public void setOnGetLayoutType(OnGetLayoutType<T> onGetLayoutType) {
this.onGetLayoutType = onGetLayoutType;
}
public interface OnGetLayoutType<T> {
int getLayoutType(int postion, List<T> data);
}
//封装一个万能的ViewHolder
static class ViewHolder {
//用于存储控件
private SparseArray<View> views;
private View convertView;
public ViewHolder(View convertView) {
this.convertView = convertView;
views = new SparseArray();
}
//获得控件
public <T extends View> T getView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = convertView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
//获得布局
public View getConvertView() {
return convertView;
}
}
}
}
这个就是万能的适配器,我已经在项目中使用了,至今还没发现bug,如果出现问题可进行留言
public static class Adapter<T> extends BaseAdapter {
private Context context;
private List<T> data;
private OnGetView<T> onGetView;
private OnGetLayoutType<T> onGetLayoutType;
private Map<Integer, Integer> layoutTypeMap;
Adapter(Context context) {
this.context = context;
data = new ArrayList<>();
layoutTypeMap = new HashMap<>();
}
@Override
public int getViewTypeCount() {
if (layoutTypeMap.size() <= 0) {
return 0;
}
return layoutTypeMap.size();
}
@Override
public int getItemViewType(int position) {
return onGetLayoutType.getLayoutType(position, this.data);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);
ViewHolder viewHolder = null;
//因为考虑到item布局重用的情况,优化ListView
if (convertView == null) {
convertView = LayoutInflater.from(context)
.inflate(layoutTypeMap.get(type), parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (onGetView != null) {
onGetView.getView(viewHolder, position, this.data);
}
return viewHolder.getConvertView();
}
//获取数据
public List<T> getData() {
return this.data;
}
//设置数据
public void setData(List<T> data) {
if (data == null) {
data = new ArrayList<T>();
}
this.data = data;
this.notifyDataSetChanged();
}
//更新数据
public void updateData(List<T> data) {
if (data == null) {
data = new ArrayList<T>();
}
this.data = data;
this.notifyDataSetChanged();
}
//添加数据
public void addData(List<T> data) {
if (this.data == null) {
data = new ArrayList<T>();
}
this.data.addAll(data);
this.notifyDataSetChanged();
}
//局部更新数据
public void updateData(ListView listView, int position, T data) {
if (data != null) {
if (this.data == null) {
this.data = new ArrayList<T>();
}
this.data.set(position, data);
//获得第一条数据
int firstVisiblePosition = listView.getFirstVisiblePosition();
//获取最后一条数据
int lastVisiblePosition = listView.getLastVisiblePosition();
if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
/**获取指定位置view对象 ,如果存在头部布局,所以减去头布局**/
View view = listView.getChildAt(position - firstVisiblePosition);
getView(position, view, listView);
}
}
}
//添加布局--两者都不能为空
public void addLayout(int type, int layoutId) {
layoutTypeMap.put(type, layoutId);
}
public void setOnGetView(OnGetView<T> onGetView) {
this.onGetView = onGetView;
}
//新建一个接口-把给用户的操作集中到一块,给用户操作,采用java的命令模式,可自行学习
public interface OnGetView<T> {
void getView(ViewHolder viewHolder, final int position, List<T> data);
}
//用于返回指定布局的类型
public void setOnGetLayoutType(OnGetLayoutType<T> onGetLayoutType) {
this.onGetLayoutType = onGetLayoutType;
}
public interface OnGetLayoutType<T> {
int getLayoutType(int postion, List<T> data);
}
//封装一个万能的ViewHolder
static class ViewHolder {
//用于存储控件
private SparseArray<View> views;
private View convertView;
public ViewHolder(View convertView) {
this.convertView = convertView;
views = new SparseArray();
}
//获得控件
public <T extends View> T getView(int viewId) {
View view = views.get(viewId);
if (view == null) {
view = convertView.findViewById(viewId);
views.put(viewId, view);
}
return (T) view;
}
//获得布局
public View getConvertView() {
return convertView;
}
}
}