zoukankan      html  css  js  c++  java
  • ListView优化-通用CommonAdapter编写备份

    【ps:CommonAdapter基于ViewHolder工具类】

    CommonAdapter.java

    package cn.edu.bzu.util;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import java.util.List;
    
    import cn.edu.bzu.listview_demo.R;
    
    /**
     * CommonAdapter makes Common extends BaseAdapter
     * Created by monster on 2015/5/3.
     */
    public abstract class CommonAdapter<T> extends BaseAdapter {
        protected Context mContext;
        protected List<T> mDatas;
        protected LayoutInflater mInflater;
    
        public CommonAdapter(Context context,List<T>datas){
            this.mContext=context;
            this.mDatas=datas;
            mInflater=LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return mDatas.size();
        }
    
        @Override
        public T getItem(int position) {
            return mDatas.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            ViewHolder holder=ViewHolder.get(mContext,convertView,parent, R.layout.listview_item,position);
            convert(holder,getItem(position));
            return holder.getConvertView();
        }
        public abstract void convert(ViewHolder holder,T t);
    }
    

    MyAdapterWithCommonViewHolder.java

    package cn.edu.bzu.adapter;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import java.util.List;
    
    import cn.edu.bzu.entity.Bean;
    import cn.edu.bzu.listview_demo.R;
    import cn.edu.bzu.util.CommonAdapter;
    import cn.edu.bzu.util.ViewHolder;
    
    /**
     * Created by monster on 2015/4/19.
     * extends CommonAdapter
     */
    public class MyAdapterWithCommonViewHolder extends CommonAdapter<Bean> {
    //    private LayoutInflater mInflater; //LayoutInflater是用来找layout下xml布局文件,并且实例化
    //    private List<Bean> mlist;
    //    private Context mcontext;
        public MyAdapterWithCommonViewHolder(Context context, List<Bean> mDatas) {
            super(context,mDatas);
        }
    
        @Override
        public void convert(ViewHolder holder, Bean bean) {
            ((TextView)holder.getView(R.id.id_title)).setText(bean.getTitle());
            ((TextView)holder.getView(R.id.id_desc)).setText(bean.getDesc());
            ((TextView)holder.getView(R.id.id_time)).setText(bean.getDate());
            ((TextView)holder.getView(R.id.id_phone)).setText(bean.getPhone());
        }
    
    //    @Override
    //    public int getCount() {
    //        return mlist.size();
    //    }
    //
    //    @Override
    //    public Object getItem(int position) {
    //        return mlist.get(position);
    //    }
    //
    //    @Override
    //    public long getItemId(int position) {
    //        return position;
    //    }
    
    
    //    @Override
    //    public View getView(int position, View convertView, ViewGroup parent) {
    //        ViewHolder holder=ViewHolder.get(mContext,convertView,parent,R.layout.listview_item,position);
    //        ((TextView)holder.getView(R.id.id_title)).setText(mDatas.get(position).getTitle());
    //        ((TextView)holder.getView(R.id.id_desc)).setText(mDatas.get(position).getDesc());
    //        ((TextView)holder.getView(R.id.id_time)).setText(mDatas.get(position).getDate());
    //        ((TextView)holder.getView(R.id.id_phone)).setText(mDatas.get(position).getPhone());
    //        return holder.getConvertView();
    //    }
    
    }
    

    源代码下载链接:https://github.com/monsterLin/CommonAdapter_Package

  • 相关阅读:
    DLL相关注意
    给我个理由要用虚析构函数
    字符指针常量和字符数组
    给自己普及网络基本知识
    面向对象三大基本特性,五大基本原则
    C/C++内存区域划分
    C++多态性
    Java XML Schema详解
    Java SE 第一百零五讲 对象的克隆与浅拷贝
    Java SE 第九十七~一百零五讲 线程与进程
  • 原文地址:https://www.cnblogs.com/boy1025/p/4473488.html
Copyright © 2011-2022 走看看