我們日常開發中BaseAdapter使用非常的多,大家有沒有想過,再進一步封裝,將平常使用的對象集合,利用泛型放入集合中,再也不用每次都重寫那幾個方法了,當然我也提供我的下載地址(https://github.com/androidsihai1/BaseAdapterDemo) ...
我們日常開發中BaseAdapter使用非常的多,大家有沒有想過,再進一步封裝,將平常使用的對象集合,利用泛型放入集合中,再也不用每次都重寫那幾個方法了,當然我也提供我的下載地址(https://github.com/androidsihai1/BaseAdapterDemo)
1 package com.example.administrator.mybaseadapter; 2 3 import android.content.Context; 4 import android.widget.TextView; 5 6 7 8 public class MyAdapter extends BaseAdapter { 9 10 public MyAdapter(Context context) { 11 super(context); 12 } 13 14 15 @Override 16 public int getItemViewType(int position) { 17 return 0; 18 } 19 20 @Override 21 public int getItemLayoutId(int getItemViewType) { 22 return R.layout.item_layout; 23 } 24 25 @Override 26 public void handleItem(int itemViewType, int position, Object item, ViewHolder holder, boolean isRecycle) { 27 TextView leftTv = holder.get(R.id.left_tv, TextView.class); 28 TextView righTv = holder.get(R.id.right_tv, TextView.class); 29 Book book = (Book) item; 30 leftTv.setText(book.id + ""); 31 righTv.setText(book.name); 32 } 33 34 35 }
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); } }
1 package com.example.administrator.mybaseadapter; 2 3 4 5 public class Book { 6 public int id; 7 public String name; 8 }
1 package com.example.administrator.mybaseadapter; 2 3 import android.content.Context; 4 import android.util.SparseArray; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 /** 13 * Created by [email protected] on 2016/11/15. 14 */ 15 16 public abstract class BaseAdapter<T> extends android.widget.BaseAdapter { 17 18 protected Context mContext; 19 protected List<T> mData = new ArrayList<T>(); 20 LayoutInflater mInflater; 21 22 public BaseAdapter(Context context) { 23 this.mContext = context; 24 mInflater = LayoutInflater.from(context); 25 } 26 27 final public void setData(List<T> list) { 28 this.mData = list; 29 } 30 31 final public List<T> getData() { 32 return mData; 33 } 34 35 36 @Override 37 final public int getCount() { 38 return mData.size(); 39 } 40 41 @Override 42 public T getItem(int i) { 43 return mData.get(i); 44 } 45 46 @Override 47 final public long getItemId(int i) { 48 return i; 49 } 50 51 public abstract int getItemViewType(int position); 52 public abstract int getItemLayoutId(int getItemViewType); 53 public abstract void handleItem(int itemViewType , int position , T item , ViewHolder holder , boolean isRecycle); 54 @Override 55 public View getView(int position, View view, ViewGroup viewGroup) { 56 int itemLayoutType = getItemViewType(position); 57 ViewHolder viewHolder = null; 58 boolean isRecycle =false; 59 if(view == null) { 60 view = mInflater.inflate(getItemLayoutId(itemLayoutType) , null); 61 viewHolder = new ViewHolder(view); 62 view.setTag(viewHolder); 63 } else { 64 viewHolder = (ViewHolder) view.getTag(); 65 isRecycle = true; 66 } 67 handleItem(itemLayoutType , position ,mData.get(position) , viewHolder , isRecycle); 68 return view; 69 } 70 71 public static class ViewHolder{ 72 View mRootView; 73 SparseArray<View> mViews = new SparseArray<View>(); 74 public ViewHolder(View view) { 75 this.mRootView = view; 76 } 77 78 public View getView() { 79 return mRootView; 80 } 81 82 public <T extends View> T get(int viewId) { 83 View childView = mViews.get(viewId); 84 if(childView == null) { 85 childView = mRootView.findViewById(viewId); 86 mViews.put(viewId , childView); 87 } 88 return (T)childView; 89 } 90 91 public <T extends View> T get(int viewId , Class<T> viewClass) { 92 View childView = mViews.get(viewId); 93 if(childView == null) { 94 childView = mRootView.findViewById(viewId); 95 mViews.put(viewId , childView); 96 } 97 return (T) childView; 98 } 99 100 } 101 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_main" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.administrator.mybaseadapter.MainActivity"> 8 9 <ListView 10 android:id="@+id/lv" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" /> 13 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="horizontal" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:text="TextView" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:id="@+id/left_tv" 11 android:layout_weight="1" /> 12 13 <TextView 14 android:text="TextView" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:id="@+id/right_tv" 18 android:layout_weight="1" /> 19 </LinearLayout>