ListView的适配器和Holder的封装

  • 1未封装标准写法
  • 2封装BaseHolder
  • 3实体Holder
  • 4封装BaseAdapter
  • 5实体Adapter

1未封装标准写法

MyAdapter.java

class MyAdapter extends BaseAdapter {

        private ArrayList data;

        public MyAdapter(List data){
            this.data = data;
        }
        @Override  
        public int getCount() {  
            return data.size();  
        }  
        @Override  
        public Object getItem(int position) {  
            return data.get(position);  
        }  
        @Override  
        public long getItemId(int position) {  
            return position;  
        }  
        @Override  
        public View getView(int position, View convertView, ViewGroup parent) {  
            ViewHolder holder;  
            if(convertView == null){  
                holder = new ViewHolder();  
                // 1.加载布局文件  
                View view = View.inflate(context, R.layout.listviewitem, null);  
                // 2.初始化控件  
                holder.textView = view.findViewById(R.id.textView1);  
                // 3.打一个tag标记  
                view.setTag(holder);  
            }else{  
                holder = (ViewHolder) convertView.getTag();  
            }  
                // 4.设置数据刷新内容  
            holder.textView.setText("设置内容");  
            return convertView;  
        }  
    }  

    static class ViewHolder {  
        TextView textView;  
    }  

2封装BaseHolder

MyBaseHolder.java

public abstract class MyBaseHolder {  
    // 一个item的根布局  
    private View mRootView;  
    // 一个item的数据  
    private T data;  

    // 当new这个对象时, 就会加载布局, 初始化控件,设置tag  
    public MyBaseHolder() {  
        mRootView = initView();  
        // 3. 打一个标记tag  
        mRootView.setTag(this);  
    }  
    // 1. 加载布局文件  
    // 2. 初始化控件 findViewById  
    public abstract View initView();  
    // 返回item的布局对象  
    public View getRootView() {  
        return mRootView;  
    }  
    // 设置当前item的数据  
    public void setData(T data) {  
        this.data = data;  
        refreshView(data);  
    }  
    // 获取当前item的数据  
    public T getData() {  
        return data;  
    }  
    // 4. 根据数据来刷新界面  
    public abstract void refreshView(T data);  
}  

3实体Holder

SpecificHolder.java

public class SpecificHolder extends MyBaseHolder<String> {  

    private TextView mTextView;  

    @Override  
    public View initView() {  
        // 1. 加载布局  
        View view = View.inflate(null, R.layout.item_listview, null);  
        // 2. 初始化控件  
        mTextView = (TextView) view.findViewById(R.id.textView1);  
        return view;  
    }  

    @Override  
    public void refreshView(String data) {  
        // 3. 设置内容刷新界面  
        mTextView.setText(data);  
    }  

}  

4封装BaseAdapter

MyBaseAdapter.java

public abstract class MyBaseAdapter extends BaseAdapter {

    private ArrayList data;

    public MyBaseAdapter(ArrayList data) {
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public T getItem(int position) {
        return data.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyBaseHolder baseHolder;
        if(convertView == null){
            // 1. 加载布局文件
            // 2. 初始化控件 findViewById
            // 3. 打一个标记tag
            baseHolder = getHolder();
        }else{
            baseHolder = (MyBaseHolder) convertView.getTag();
        }
        // 4. 设置内容刷新界面
        baseHolder.setData(getItem(position));
        return baseHolder.getRootView();
    }

    public abstract MyBaseHolder getHolder();
}

5实体Adapter

MyAdapter.java

class MyAdapter extends MyBaseAdapter<String>{  

        public MyAdapter(ArrayList data) {  
            super(data);  
        }  

        @Override  
        public MyBaseHolder getHolder() {  
            return new SpecificHolder();  
        }  

    }  

你可能感兴趣的:(Android,listview封装,listview,泛型,抽象,接口)