内部类中创建的adapter和holder,代码如下:
private class MineAdapter extends MyBaseAdapter<String> { public MineAdapter(List<String> dataSets) { super(dataSets); this.list = dataSets; } /** * Get a View that displays the data at the specified position in the data set. You can either * create a View manually or inflate it from an XML layout file. When the View is inflated, the * parent View (GridView, ListView...) will apply default layout parameters unless you use * {@link LayoutInflater#inflate(int, ViewGroup, boolean)} * to specify a root view and to prevent attachment to the root. * * @param position The position of the item within the adapter's data set of the item whose view * we want. * @param convertView The old view to reuse, if possible. Note: You should check that this view * is non-null and of an appropriate type before using. If it is not possible to convert * this view to display the correct data, this method can create a new view. * Heterogeneous lists can specify their number of view types, so that this View is * always of the right type (see {@link #getViewTypeCount()} and * {@link #getItemViewType(int)}). * @param parent The parent that this view will eventually be attached to * @return A View corresponding to the data at the specified position. */ @Override public View getView(int position, View convertView, ViewGroup parent) { switch (getItemViewType(position)) { case TYPE_ONE: YBTLog.e(TAG, "走到了新建 1"); TextView textView = new TextView(DemoActivity.this); ViewGroup.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp2px(YBTApplication.getContext(), 40)); textView.setLayoutParams(params); textView.setText(list.get(position)); return textView; case TYPE_TWO: ViewHolder holder = null; if (convertView == null) { YBTLog.e(TAG, "走到了 新建 2"); holder = new ViewHolder(); convertView = View.inflate(DemoActivity.this, R.layout.item_type_two, null); holder.textView = convertView.findViewById(R.id.tv_item_demo_two); convertView.setTag(holder); } else { YBTLog.e(TAG, "走到了 复用 2"); holder = (ViewHolder) convertView.getTag(); } ViewGroup.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp2px(YBTApplication.getContext(), 80)); holder.textView.setLayoutParams(params2); holder.textView.setText(list.get(position)); break; default: break; } return convertView; } private final int TYPE_ONE = 0; private final int TYPE_TWO = 1; @Override public int getViewTypeCount() { return 2; } @Override public int getItemViewType(int position) { if (position % 2 == 0) { return TYPE_TWO; } else { return TYPE_ONE; } } } private class ViewHolder { TextView textView; }
通过log输出测试,可以正常的复用item,复用的TYPE_TWO。对于TYPE_ONE是正常使用的。在下感觉是正确的。但是水平有限,无法正确确定。需要大牛给予肯定才能确信。
当有多种item的时候getViewTypeCount方法和getItemViewType方法分别返回具体的数量还有哪种条件时候的判断即可。
MyBaseAdapter是自己抽取的一个基类,代码如下:
public abstract class MyBaseAdapter<T> extends BaseAdapter { public List<T> list; public MyBaseAdapter(List<T> dataSets) { // list = dataSets; } @Override public int getCount() { if (list != null) { return list.size(); } return 0; } @Override public Object getItem(int position) { if (list != null) { return list.get(position); } return null; } @Override public long getItemId(int position) { return position; } public void setList(List<T> list) { this.list = list; } public List<T> getList() { return list; } }
如有不正确,欢迎指教探讨。