BaseAdapter日常的封装

手机开发 2016-11-15

我们日常开发中BaseAdapter使用非常的多,大家有没有想过,再进一步封装,将平常使用的对象集合,利用泛型放入集合中,再也不用每次都重写那几个方法了,当然我也提供我的下载地址(https://github.com/androidsihai1/BaseAdapterDemo)

package com.example.administrator.mybaseadapter;
 
 import android.content.Context;
 import android.widget.TextView;
 
 
 
 public class MyAdapter extends BaseAdapter {
 
     public MyAdapter(Context context) {
         super(context);
     }
 
 
     @Override
     public int getItemViewType(int position) {
         return 0;
     }
 
     @Override
     public int getItemLayoutId(int getItemViewType) {
         return R.layout.item_layout;
     }
 
     @Override
     public void handleItem(int itemViewType, int position, Object item, ViewHolder holder, boolean isRecycle) {
         TextView leftTv = holder.get(R.id.left_tv, TextView.class);
         TextView righTv = holder.get(R.id.right_tv, TextView.class);
         Book book = (Book) item;
         leftTv.setText(book.id + "");
         righTv.setText(book.name);
     }
 
 
 }
package com.example.administrator.mybaseadapter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;

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

public class MainActivity extends AppCompatActivity {

    List<Book> mList = new ArrayList<Book>();
    ListView mLv ;
    BaseAdapter<Book> mAdapter;
    int k = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLv = findView(R.id.lv);
        for(int i = 0 ; i < 50 ; i++) {
            Book book = new Book();
            k++;
            book.id = k;
            book.name = "name " + k;
            mList.add(book);
        }
        mAdapter = new MyAdapter(this);
        mAdapter.setData(mList);
        mLv.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }


    public <T extends View> T findView(int viewId) {
        return  (T)findViewById(viewId);
    }

}
package com.example.administrator.mybaseadapter;
 
 
 
 public class Book {
    public int id;
     public String name;
 }
package com.example.administrator.mybaseadapter;
 
 import android.content.Context;
 import android.util.SparseArray;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
 import java.util.ArrayList;
 import java.util.List;
 
 /**
  * Created by [email protected] on 2016/11/15.
  */
 
 public abstract class BaseAdapter<T> extends android.widget.BaseAdapter {
 
     protected Context mContext;
     protected List<T> mData = new ArrayList<T>();
     LayoutInflater mInflater;
 
     public BaseAdapter(Context context) {
         this.mContext = context;
         mInflater = LayoutInflater.from(context);
     }
 
     final public void setData(List<T> list) {
         this.mData = list;
     }
 
    final public List<T> getData() {
         return mData;
     }
 
 
     @Override
     final public int getCount() {
         return mData.size();
     }
 
     @Override
     public T getItem(int i) {
         return mData.get(i);
     }
 
     @Override
     final public long getItemId(int i) {
         return i;
     }
 
     public abstract   int getItemViewType(int position);
     public abstract int getItemLayoutId(int getItemViewType);
     public abstract void handleItem(int itemViewType , int position , T item , ViewHolder holder , boolean isRecycle);
     @Override
     public View getView(int position, View view, ViewGroup viewGroup) {
         int itemLayoutType = getItemViewType(position);
         ViewHolder viewHolder = null;
         boolean isRecycle =false;
         if(view == null) {
             view =  mInflater.inflate(getItemLayoutId(itemLayoutType) , null);
             viewHolder = new ViewHolder(view);
             view.setTag(viewHolder);
         } else {
             viewHolder = (ViewHolder) view.getTag();
             isRecycle = true;
         }
         handleItem(itemLayoutType , position ,mData.get(position) , viewHolder , isRecycle);
         return view;
     }
 
     public static  class ViewHolder{
         View mRootView;
         SparseArray<View> mViews = new SparseArray<View>();
         public ViewHolder(View view) {
             this.mRootView = view;
         }
 
         public View getView() {
             return mRootView;
         }
 
         public <T extends  View> T get(int viewId) {
             View childView = mViews.get(viewId);
             if(childView == null) {
                 childView = mRootView.findViewById(viewId);
                 mViews.put(viewId , childView);
             }
             return (T)childView;
         }
 
         public <T extends  View> T get(int viewId , Class<T> viewClass) {
             View childView = mViews.get(viewId);
             if(childView == null) {
                 childView = mRootView.findViewById(viewId);
                 mViews.put(viewId , childView);
             }
             return (T) childView;
         }
 
     }
 }
<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/activity_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="com.example.administrator.mybaseadapter.MainActivity">
 
         <ListView
             android:id="@+id/lv"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />
 </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal" android:layout_width="match_parent"
     android:layout_height="match_parent">
 
     <TextView
         android:text="TextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/left_tv"
         android:layout_weight="1" />
 
     <TextView
         android:text="TextView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/right_tv"
         android:layout_weight="1" />
 </LinearLayout>

相关推荐