直接贴代码
//这个接口是用来标志类型的 public interface IType { public static final int TYPE_1 = 0; public static final int TYPE_2 = 1; public int getType(); } //类型1的model public class Type1 implements IType { private String text; private int imageId; public Type1() { } public Type1(String text, int imageId) { this.text = text; this.imageId = imageId; } public String getText() { return text; } public void setText(String text) { this.text = text; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } @Override public int getType() { return IType.TYPE_1; } } //类型2的model public class Type2 implements IType { private String btnStr; private String etStr; private int imageId; public Type2() { } public Type2(String btnStr, String etStr, int imageId) { this.btnStr = btnStr; this.etStr = etStr; this.imageId = imageId; } public String getBtnStr() { return btnStr; } public void setBtnStr(String btnStr) { this.btnStr = btnStr; } public String getEtStr() { return etStr; } public void setEtStr(String etStr) { this.etStr = etStr; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } @Override public int getType() { return IType.TYPE_2; } } //listview的adapter public class MultipleAdapter extends BaseAdapter { private LayoutInflater inflater; private List<IType> types; public MultipleAdapter(Context context, List<IType> types) { inflater = LayoutInflater.from(context); this.types = types; } @Override public int getCount() { return types == null ? 0 : types.size(); } @Override public Object getItem(int position) { return types == null ? null : types.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return types.get(position).getType(); } @Override public int getViewTypeCount() { return 2;//这里的返回值必须是类型最大数大1,为什么 } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder1 holder1 = null; ViewHolder2 holder2 = null; int type = getItemViewType(position); if (convertView == null) { if (type == IType.TYPE_1) { holder1 = new ViewHolder1(); convertView = inflater.inflate(R.layout.adapter_1, null); holder1.tv = (TextView) convertView.findViewById(R.id.tv); holder1.iv = (ImageView) convertView.findViewById(R.id.iv); convertView.setTag(holder1); } else if (type == IType.TYPE_2) { holder2 = new ViewHolder2(); convertView = inflater.inflate(R.layout.adapter_2, null); holder2.btn = (Button) convertView.findViewById(R.id.btn); holder2.et = (EditText) convertView.findViewById(R.id.et); holder2.iv = (ImageView) convertView.findViewById(R.id.iv); convertView.setTag(holder2); } } else { if (type == IType.TYPE_1) { holder1 = (ViewHolder1) convertView.getTag(); } else { holder2 = (ViewHolder2) convertView.getTag(); } } // 赋值 if (type == IType.TYPE_1) { Type1 t = (Type1) types.get(position); holder1.tv.setText(t.getText()); } else { Type2 t = (Type2) types.get(position); holder2.btn.setText(t.getBtnStr()); holder2.et.setText(t.getEtStr()); } return convertView; } class ViewHolder1 { TextView tv; ImageView iv; } class ViewHolder2 { Button btn; EditText et; ImageView iv; } }
//R.layout.adapter_1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv" android:layout_width="100dp" android:layout_height="40dp" android:text="aaaa" /> <ImageView android:id="@+id/iv" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right|center_vertical" android:layout_margin="10dp" android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/ic_launcher" /> </LinearLayout>
//R.layout.adapter_2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn" android:layout_width="80dp" android:layout_height="wrap_content" android:text="btn" /> <EditText android:id="@+id/et" android:layout_width="100dp" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="right|center_vertical" android:layout_margin="10dp" android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/ic_launcher" /> </LinearLayout>
//主界面-- 就只有一个listview
public class MainActivity extends Activity { private ListView lv; private List<IType> types; private MultipleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); dataInit(); adapter = new MultipleAdapter(MainActivity.this, types); lv.setAdapter(adapter); } private void dataInit() { types = new ArrayList<IType>(); Type1 type1_1 = new Type1("type1_1", R.drawable.ic_launcher); Type2 type2_1 = new Type2("type2_1", "type2_1", R.drawable.ic_launcher); Type1 type1_2 = new Type1("type1_2", R.drawable.ic_launcher); Type2 type2_2 = new Type2("type2_2", "type2_2", R.drawable.ic_launcher); Type1 type1_3 = new Type1("type1_3", R.drawable.ic_launcher); Type1 type1_4 = new Type1("type1_4", R.drawable.ic_launcher); Type1 type1_6 = new Type1("type1_6", R.drawable.ic_launcher); Type2 type2_3 = new Type2("type2_3", "type2_3", R.drawable.ic_launcher); Type2 type2_4 = new Type2("type2_4", "type2_4", R.drawable.ic_launcher); Type2 type2_5 = new Type2("type2_5", "type2_5", R.drawable.ic_launcher); Type2 type2_6 = new Type2("type2_6", "type2_6", R.drawable.ic_launcher); Type1 type1_5 = new Type1("type1_5", R.drawable.ic_launcher); types.add(type1_1); types.add(type2_1); types.add(type1_2); types.add(type2_2); types.add(type1_3); types.add(type1_4); types.add(type1_6); types.add(type2_3); types.add(type2_4); types.add(type2_5); types.add(type2_6); types.add(type1_5); } }
//效果是能实现的,但是有个问题,没搞清楚原因,希望知道原因的朋友解释下。问题如下: //比如我的IType接口改成如下这样。
public interface IType { public static final int TYPE_1 = 0; public static final int TYPE_2 = 22;//注意,这里改成22了 public int getType(); }
/那么在adapter里面的getViewTypeCount()方法必须返回23(即是22+1),不然就会报错,但是事实上我们就只有Type类型就只有两个。 //希望用的朋友注意下,知道原因的朋友告知下。
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦