实现通用的ViewHolder+Adapter

package com.example.commonadaptertest;

import android.util.SparseArray;
import android.view.View;
public class CommonViewHolder extends SparseArray<View>{
    /**
     * 
     * @param view Adapter中传过来的View
     * @return 返回一个ViewHolder
     */
    public static CommonViewHolder get(View view){
        CommonViewHolder viewHolder= (CommonViewHolder) view.getTag();
        if(viewHolder==null){
            viewHolder=new CommonViewHolder();
            view.setTag(viewHolder);
        }
        return viewHolder;
    }

    /**
     * 此方法在CommonAdapter的子类中有使用,返回一个View,可以来设置View的内容
     * @param commonViewHolder 当前View对应的ViewHolder
     * @param currentView    当前的View
     * @param key    View中的一个控件View存放的Key
     * @return
     */
    public View get(CommonViewHolder commonViewHolder,View currentView,int key) {
        // TODO Auto-generated method stub
        View view=get(key);
        if(view==null){
            view=currentView.findViewById(key);
            commonViewHolder.put(key, view);
        }
        return view;
    }
    
}

通用的ViewHolder,存放在SparseArray中。

package com.example.commonadaptertest;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
 * 
 * @param <T> 传入List的类型
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    Context mContext; 
    List<T> mList;
    int resource=-1; 
    LayoutInflater mLayoutInflater;
    /**
     * 
     * @param mContext 
     * @param mList
     * @param resource 对应的layout的xml,如R.id.XXXX
     */
    public CommonAdapter(Context mContext, List<T> mList, int resource) {
        super();
        this.mContext = mContext;
        this.mList = mList;
        this.resource = resource;
        mLayoutInflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mList.size();
    }

    @Override
    public T getItem(int position) {
        // TODO Auto-generated method stub
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /**
     * getView中间子View的内容设置的代码提出来,向外提供一个抽象发放,自定义实现。
     * @param commonViewHolder 当前的ViewHolder
     * @param currentView    当前的View
     * @param item    对应View的数据
     */
    public abstract void setViewData(CommonViewHolder commonViewHolder,View currentView,T item);
    
    /**
     * 方法完全抽离只剩下获取当前View和ViewHolder,然后返回View
     */
    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        // TODO Auto-generated method stub
        if(view==null){
            view=mLayoutInflater.inflate(resource, null);
        }
        CommonViewHolder cvh=CommonViewHolder.get(view);
        setViewData(cvh,view,getItem(position));
        return view;
    }

}

通用的Adapter。

package com.example.commonadaptertest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    ListView listView1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }
    void initView(){
        listView1=(ListView) findViewById(R.id.listView1);
        listView1.setAdapter(new CommonAdapter<String>(this, list, R.layout.listview_layout_1) {

            @Override
            public void setViewData(CommonViewHolder commonViewHolder,View currentView,
                    String item) {
                // TODO Auto-generated method stub
                TextView tv1=(TextView) commonViewHolder.get(commonViewHolder,currentView,R.id.textView1);
                TextView tv2=(TextView) commonViewHolder.get(commonViewHolder,currentView,R.id.textView2);
                tv1.setText(item);
                tv2.setText(item);
            }
        });
    }
    
    List<String> list=new  ArrayList<String>();
    void initData(){
        for (int i = 0; i < 500; i++) {
            list.add("第"+i+"个元素");
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

最终的实现,仅仅用一个内部类即可实现。

 

posted @ 2014-12-04 18:13  q429786006  阅读(283)  评论(0编辑  收藏  举报