Android、ユニバーサルListAdapter

4521 ワード

長所
リストごとにAdapterを作成する必要はありません.
効果を使う
Itemクリックイベントは自筆するイベントの仕組みを使用します.
mListAdapter = new BaseListAdapter<PromotionsModel>(mView.getContext(), data, R.layout.promotion_list_item) {

    @Override
    public HashMap<String, Integer> GetViewList() {
        HashMap<String, Integer> tmp = new HashMap<String, Integer>();
        tmp.put("mainImage", R.id.item_image);
        tmp.put("title", R.id.item_title);
        tmp.put("desc", R.id.item_desc);
        tmp.put("price", R.id.item_price);
        tmp.put("yprice", R.id.item_yprice);
        return tmp;
    }

    @Override
    public void SetViewData(HashMap<String, View> viewList, PromotionsModel data) {
        ImageView iv = (ImageView) viewList.get("mainImage");
        ImageLoader.getInstance().displayImage(DataApis.FileDownloadUrl + data.extDataModel.breviaryImg, iv);
        TextView tv = (TextView) viewList.get("title");
        tv.setText(data.name);
        tv.getPaint().setFakeBoldText(true);
        tv = (TextView) viewList.get("desc");
        tv.setText(data.extDataModel.shopName);
        tv = (TextView) viewList.get("price");
        tv.setText("¥" + Double.toString(data.money));
        tv = (TextView) viewList.get("yprice");
        tv.setText("¥" + Double.toString(data.extDataModel.originalPrice));
        tv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    }
};
mListAdapter.OnItemClickEvent.addHandler(new EventHandle<PromotionsModel>() {
    @Override
    public void Handle(Object sender, PromotionsModel e) {
        Intent intent = new Intent();
        intent.setClass(mView.getContext(), DetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("Type", "Promotions");
        bundle.putLong("DataKey", HostedBox.putData(e));
        intent.putExtras(bundle);
        startActivity(intent);
    }
});
実現する
public abstract class BaseListAdapter<ModelType> extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private int mTemplateLayoutId;
    private List<ModelType> mItems;

    public EventTrans<ModelType> OnItemClickEvent;

    final public void clearItem() {
        mItems.clear();
    }

    public BaseListAdapter(Context context, List<ModelType> items, int templateLayoutId) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mTemplateLayoutId = templateLayoutId;
        mItems = items;
        if (mItems == null) mItems = new ArrayList<ModelType>();
        OnItemClickEvent = new EventTrans<ModelType>();
    }

    final public void addItem(ModelType item) {
        mItems.add(item);
    }

    final public void removeItem(ModelType item) {
        mItems.remove(item);
    }

    @Override
    final public int getCount() {
        return mItems.size();
    }

    @Override
    final public Object getItem(int i) {
        return mItems.get(i);
    }

    @Override
    final public long getItemId(int i) {
        return i;
    }

    public abstract HashMap<String, Integer> GetViewList();

    public abstract void SetViewData(HashMap<String, View> viewList, ModelType data);

    @Override
    final public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHodler holder;
        final ModelType model = mItems.get(i);

        if (view == null) {
            view = mInflater.inflate(mTemplateLayoutId, null);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    OnItemClickEvent.trigger(view, model);
                }
            });

            holder = new ViewHodler();

            HashMap<String, Integer> roll = GetViewList();
            for (String key : roll.keySet()) {
                holder.viewHashMap.put(key, view.findViewById(roll.get(key)));
            }

            view.setTag(holder);
        } else {
            holder = (ViewHodler) view.getTag();
        }

        SetViewData(holder.viewHashMap, model);
        return view;
    }

    final private class ViewHodler {
        HashMap<String, View> viewHashMap = new HashMap<String, View>();
    }
}