android 學習筆記:自訂通用ListView/GridView,實現ListAdapter 類

來源:互聯網
上載者:User

android 學習中發現經常血藥用到ListView,而且還需要自訂View顯示,實現ListAdapter interface

於是寫了一個通用的CommonAdapter 類,簡化ListAdapter 的實現.把Adaapter 需要根據layout 動態實現的部分提取出來.做成介面.靜態部分做成通用方法,以後開發就可以只關注layout 部分的綁定.簡化開發.

CommonAdapter 類

View Code public class CommonAdapter extends BaseAdapter {

    private int customXmlName;
    protected ViewHolderModel Holder = null;
    protected final List<Map<String, Object>> list;
    private final Context context;

    /*
     * 建構函式設定Context 和 資料來源
     */
    public CommonAdapter(ViewHolderModel vModel) {

        customXmlName = com.gzh.search.R.layout.list_item;
        this.context = vModel.context;
        this.list = vModel.list;
        this.Holder = vModel;
    }

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

    public void setLayout_Name(int name) {
        if (name <= 0) {
            return;
        }
        customXmlName = name;
    }

    @Override
    public Object getItem(int position) {
        if (position < list.size()) {
            return list.get(position);
        } else {

            return null;
        }
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    public void HolderModel(ViewHolderModel vModel) {
        this.Holder = vModel;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(customXmlName,
                    null);
            Holder.InitViewHoler(convertView);
            convertView.setTag(Holder);

        } else {
            Holder = (ViewHolderModel) convertView.getTag();
        }
        Holder.SetViewHolerValues(convertView, position);
        return convertView;
    }

}

customXmlName = com.gzh.search.R.layout.list_item :表示預設layout

ViewHolderModel  layout 對應的java Holder 抽象基類

 

View Code /*初始化ItemView
 *設定View
 */
interface IViewHolderController {
    /*
     * 初始化
     */
    void InitViewHoler(View convertView);

    /*
     * 設定顯示內容
     */
    void SetViewHolerValues(View convertView, int position);
}

public abstract class ViewHolderModel implements IViewHolderController {
    public ViewHolderModel Holder = null;
    protected List<Map<String, Object>> list;
    protected Context context;

    public ViewHolderModel(Context context, List<Map<String, Object>> list) {
        this.context = context;
        this.list = list;

    }
}

只需要實現抽象類別ViewHolderModel,就完成了自訂Adapter的開發.

應用舉例:

實現:ViewHolderModel 基類

 

 

public class HolderView extends ViewHolderModel {

    private ImageView itemLeftImage;
    private TextView itemText;
    private ImageView itemRightImage;
    private Button itemButton;

    public HolderView(Context context, List<Map<String, Object>> list) {
        super(context, list);

        // TODO Auto-generated constructor stub
    }

    @Override
    public void InitViewHoler(View convertView) {
        HolderView viewHolder = (HolderView) Holder;
        viewHolder.itemLeftImage = (ImageView) convertView
                .findViewById(R.id.item_left);
        viewHolder.itemText = (TextView) convertView
                .findViewById(R.id.item_text);
        viewHolder.itemRightImage = (ImageView) convertView
                .findViewById(R.id.item_right);
        viewHolder.itemButton = (Button) convertView.findViewById(R.id.btnOK);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.gzh.common.IViewHolderController#SetViewHolerValues(android.view.
     * View, int)
     */
    @Override
    public void SetViewHolerValues(View convertView, int position) {
        HolderView viewHolder = (HolderView) Holder;
        Map<String, Object> item = list.get(position);

        viewHolder.itemLeftImage.setImageResource(Integer.parseInt(item.get(
                "left").toString()));
        viewHolder.itemText.setText(item.get("Text").toString());
        viewHolder.itemRightImage.setImageResource(Integer.parseInt(item.get(
                "Right").toString()));
        viewHolder.itemButton.setTag(position);

    }
}

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.