zoukankan      html  css  js  c++  java
  • Android,通用ListAdapter

    优点

    不需要每个列表都建立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>();
        }
    }
    
  • 相关阅读:
    iOS No suitable application records were found. Verify your bundle identifier 'xxx' is correct.
    隐私安全加密
    python-标示符和关键字
    Xcode打包提交至itunes connect后,提交审核成功,随后出现二进制文件无效
    linking against a dylib which is not safe for use in application extensions
    CH收藏的书
    颜色管理
    子线程回到主线程
    python- if-else
    安卓手机传递文件到Windows系统电脑
  • 原文地址:https://www.cnblogs.com/zhang740/p/3934902.html
Copyright © 2011-2022 走看看