最近在开发项目中遇到了实现类似淘宝首页的需求,使用listView可以解决,在此记录一下。
实现步骤:
- 重写 getViewTypeCount() – 返回你有多少个不同的布局
- 重写 getItemViewType(int) – 由position返回view type id
- 根据view item的类型,在getView中创建正确的convertView
下面是代码示例:
Activity:
HomeItem jchdItem = new HomeItem(); jchdItem.setItemType(ItemType.JCHD); homeItems.add(jchdItem);
HomeItem yhhdItem = new HomeItem(); yhhdItem.setItemType(ItemType.YHHD_LIST); List<YhhdBean> yhhdLists = new ArrayList<>(); for (int i = 0; i < privilege.size(); i++) { JSONObject object2 = privilege.getJSONObject(i); YhhdBean yhhdList = new YhhdBean(); yhhdLists.add(yhhdList); } yhhdItem.setYhhdLists(yhhdLists); homeItems.add(yhhdItem); adapter = new HomeAdapter(getActivity(), homeItems); lv_home_list.setAdapter(adapter);
layout:
<ListView android:id="@+id/lv_home_list" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:divider="@null" android:overScrollMode="never" android:scrollbars="none" />
Adapter:
public class HomeAdapter extends BaseAdapter { private Context context; private List<HomeItem> list; private final static int JCHD = 3; private final static int YHHD_LIST = 4; public HomeAdapter(Context context, List<HomeItem> list) { this.context = context; this.list = list; } @Override public int getCount() { return list != null ? list.size() : 0; } @Override public Object getItem(int i) { return list == null ? null : list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { HomeItem homeItem = list.get(i); LayoutInflater inflater = LayoutInflater.from(context); YhhdHodlder yhhdHodlder; YhhdListHolder yhhdListHolder; int type = homeItem.getItemType().getValue(); switch (type) { case JCHD: if (view == null) { view = inflater.inflate(R.layout.view_yhhd, null); yhhdHodlder = new YhhdHodlder(context, view, "精彩"); view.setTag(yhhdHodlder); } else { yhhdHodlder = (YhhdHodlder) view.getTag(); } break; case YHHD_LIST: if (view == null) { view = inflater.inflate(R.layout.view_main_yhhd_list, null); yhhdListHolder = new YhhdListHolder(context, view); view.setTag(yhhdListHolder); } else { yhhdListHolder = (YhhdListHolder) view.getTag(); } yhhdListHolder.initView(homeItem); break; } return view; } @Override public int getItemViewType(int position) { if (list != null && position < list.size()) { return list.get(position).getItemType().getValue(); } return super.getItemViewType(position); } @Override public int getViewTypeCount() { return 2; } }
HomeItem:
public class HomeItem { private ItemType itemType; private List<YhhdBean> yhhdLists; public ItemType getItemType() { return itemType; } public void setItemType(ItemType itemType) { this.itemType = itemType; } public List<YhhdBean> getYhhdLists() { return yhhdLists; } public void setYhhdLists(List<YhhdBean> yhhdLists) { this.yhhdLists = yhhdLists; } }
ItemType:
public enum ItemType { JCHD(3), YHHD_LIST(4); public int getValue() { return value; } private int value; ItemType(int value) { this.value = value; } }