實現通用的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中。java

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。android

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;
    }

}

最終的實現,僅僅用一個內部類便可實現。app

相關文章
相關標籤/搜索