1. 程式人生 > >listView 多布局

listView 多布局

== rom .get case array tco 示例 ++ span

最近在開發項目中遇到了實現類似淘寶首頁的需求,使用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;
    }
}

listView 多布局