码迷,mamicode.com
首页 > 其他好文 > 详细

常见的适配器总结

时间:2015-09-16 14:17:40      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

学习适配器有一段时间了,现在对BaseAdapter的优化和要是项目中用到的适配其很多的话,完全可以建立一个SimpleBaseAdapter(继承自BaseAdapter),方便扩展.

 

主布局代码:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity">
 7 
 8     <ListView
 9         android:id="@+id/listview"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         />
13 
14 </LinearLayout>

 

item的布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:background="@drawable/bread__selector_pink_item"
 6     android:orientation="horizontal"
 7     android:gravity="center"
 8     android:padding="10dp">
 9 
10     <RelativeLayout
11         android:id="@+id/shop_info"
12         android:layout_width="fill_parent"
13         android:layout_height="wrap_content"
14         android:gravity="center_vertical"
15         android:paddingRight="12dp"
16         android:paddingLeft="12dp">
17 
18         <TextView
19             android:id="@+id/student_name"
20             android:layout_width="match_parent"
21             android:layout_height="wrap_content"
22             android:text="东海堂天河公园店"
23             android:textSize="14sp" />
24 
25         <TextView
26             android:id="@+id/student_addr"
27             android:layout_width="match_parent"
28             android:layout_height="wrap_content"
29             android:layout_below="@+id/student_name"
30             android:layout_marginTop="10dp"
31             android:text="广州天河区建业路华翠街"
32             android:textColor="@color/font_gray"
33             android:textSize="10sp" />
34 
35     </RelativeLayout>
36 
37 </LinearLayout>

 

现在来说说BaseAdapter的优化,目前通用的优化就是ViewHolder,在getView()方法中,首次对convertView非空的处理,convertView会绑定一个ViewHolder对象,这个viewHolder主要用于帮convertView存储布局中的控件。了解这个就可以了.

来看看BaseAdapter的写法:

 1 package com.fightzhao.myadapter1.ui.adapter;
 2 
 3 import android.content.Context;
 4 import android.view.LayoutInflater;
 5 import android.view.View;
 6 import android.view.ViewGroup;
 7 import android.widget.BaseAdapter;
 8 import android.widget.TextView;
 9 
10 import com.fightzhao.myadapter1.R;
11 import com.fightzhao.myadapter1.dao.Student;
12 
13 import java.util.ArrayList;
14 
15 /**
16  * Created by fightzhao on 15-9-16.
17  */
18 public class FirstAdapter extends BaseAdapter {
19     private ArrayList<Student> data;
20     private Context context;
21 //    private Student chosenStudent;
22 
23     public FirstAdapter(Context context, ArrayList<Student> data) {
24         this.context = context;
25         this.data = data == null ? new ArrayList<Student>() : data;
26     }
27 
28     @Override
29     public int getCount() {
30         return data.size();
31     }
32 
33     @Override
34     public Object getItem(int position) {
35         if (position > data.size())
36             return null;
37         return data.get(position);
38     }
39 
40     @Override
41     public long getItemId(int position) {
42         return position;
43     }
44 
45     @Override
46     public View getView(int position, View convertView, ViewGroup parent) {
47         ViewHolder holder;
48         if (null == convertView) {
49             convertView = LayoutInflater.from(context).inflate(R.layout.item_student, null);
50             holder = new ViewHolder();
51             holder.student_name = (TextView) convertView.findViewById(R.id.student_name);
52             holder.student_addr = (TextView) convertView.findViewById(R.id.student_addr);
53             convertView.setTag(holder);
54         } else {
55             holder = (ViewHolder) convertView.getTag();
56         }
57         holder.student_name.setText(data.get(position).getName());
58         holder.student_addr.setText(data.get(position).getAddr());
59         return convertView;
60     }
61 
62     class ViewHolder {
63         TextView student_name;
64         TextView student_addr;
65     }
66 //    public void setChosenStudent(Student student){
67 //        chosenStudent = student;
68 //        notifyDataSetChanged();
69 //    }
70 //    public Student getChosenStudent(){
71 //        return chosenStudent;
72 //    }
73 }

这里的Student就是以模拟数据,不贴代码了.

 

如果项目中用到的适配器不是很多的话,用这个是没问题的,但是,为了更好扩展的话,看来下张奇大大的做法,这里直接引用:

Android简便通用的SimpleBaseAdapter 链接:http://www.stormzhang.com/androidtips/2014/08/31/android-common-simplebaseadapter/

 

代码:

  1 package com.fightzhao.myadapter1.ui.adapter;
  2 
  3 import android.content.Context;
  4 import android.util.SparseArray;
  5 import android.view.View;
  6 import android.view.ViewGroup;
  7 import android.widget.BaseAdapter;
  8 
  9 import java.util.ArrayList;
 10 import java.util.List;
 11 
 12 /**
 13  * Created by fightzhao on 15-9-16.
 14  */
 15 public abstract class SimpleBaseAdapter<T> extends BaseAdapter {
 16     protected Context context;
 17     protected List<T> data;
 18 
 19     public SimpleBaseAdapter(Context context, List<T> data) {
 20         this.context = context;
 21         this.data = data == null ? new ArrayList<T>() : new ArrayList<T>(data);
 22     }
 23 
 24     @Override
 25     public int getCount() {
 26         return data.size();
 27     }
 28 
 29     @Override
 30     public Object getItem(int position) {
 31         if (position >= data.size())
 32             return null;
 33         return data.get(position);
 34     }
 35 
 36     @Override
 37     public long getItemId(int position) {
 38         return position;
 39     }
 40 
 41     /**
 42      * 该方法需要子类实现,需要返回item布局的resource id
 43      *
 44      * @return
 45      */
 46     public abstract int getItemResource();
 47 
 48     /**
 49      * 使用该getItemView方法替换原来的getView方法,需要子类实现
 50      *
 51      * @param position
 52      * @param convertView
 53      * @param holder
 54      * @return
 55      */
 56     public abstract View getItemView(int position, View convertView, ViewHolder holder);
 57 
 58     @SuppressWarnings("unchecked")
 59     @Override
 60     public View getView(int position, View convertView, ViewGroup parent) {
 61         ViewHolder holder;
 62         if (null == convertView) {
 63             convertView = View.inflate(context, getItemResource(), null);
 64             holder = new ViewHolder(convertView);
 65             convertView.setTag(holder);
 66         } else {
 67             holder = (ViewHolder) convertView.getTag();
 68         }
 69         return getItemView(position, convertView, holder);
 70     }
 71 
 72     public class ViewHolder {
 73         private SparseArray<View> views = new SparseArray<View>();
 74         private View convertView;
 75         public ViewHolder(View convertView) {
 76             this.convertView = convertView;
 77         }
 78         /**
 79          * 通过控件的Id获取对应的控件,如果没有则加入views
 80          *
 81          * @param resId
 82          * @param <T>
 83          * @return
 84          */
 85         @SuppressWarnings("unchecked")
 86         public <T extends View> T getView(int resId) {
 87             View v = views.get(resId);
 88             if (null == v) {
 89                 v = convertView.findViewById(resId);
 90                 views.put(resId, v);
 91             }
 92             return (T) v;
 93         }
 94     }
 95 
 96     public void add(T t) {
 97         data.add(t);
 98         notifyDataSetChanged();
 99     }
100 
101     public void addAll(List<T> elem) {
102         data.addAll(elem);
103         notifyDataSetChanged();
104     }
105 
106     public void remove(T elem) {
107         data.remove(elem);
108         notifyDataSetChanged();
109     }
110 
111     public void remove(int index) {
112         data.remove(index);
113         notifyDataSetChanged();
114     }
115 
116     public void replaceAll(List<T> elem) {
117         data.clear();
118         data.addAll(elem);
119         notifyDataSetChanged();
120     }
121 }

使用:

 1 package com.fightzhao.myadapter1.ui.adapter;
 2 
 3 import android.content.Context;
 4 import android.view.View;
 5 import android.widget.TextView;
 6 
 7 import com.fightzhao.myadapter1.R;
 8 import com.fightzhao.myadapter1.dao.Student;
 9 
10 import java.util.List;
11 
12 /**
13  * Created by fightzhao on 15-9-16.
14  */
15 public class SecoundAdapter extends SimpleBaseAdapter<Student> {
16     public SecoundAdapter(Context context, List<Student> data) {
17         super(context, data);
18     }
19     @Override
20     public int getItemResource() {
21         return R.layout.item_student;
22     }
23     @Override
24     public View getItemView(int position, View convertView, ViewHolder holder) {
25         TextView student_name = holder.getView(R.id.student_name);
26         TextView student_addr = holder.getView(R.id.student_addr);
27         student_name.setText(data.get(position).getName());
28         student_addr.setText(data.get(position).getAddr());
29         return convertView;
30     }
31 }

之后在MainActivity中使用即可:

 1 package com.fightzhao.myadapter1;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.AdapterView;
 7 import android.widget.ListView;
 8 import android.widget.Toast;
 9 
10 import com.fightzhao.myadapter1.dao.Student;
11 import com.fightzhao.myadapter1.ui.adapter.FirstAdapter;
12 import com.fightzhao.myadapter1.ui.adapter.SecoundAdapter;
13 
14 import java.util.ArrayList;
15 
16 public class MainActivity extends Activity {
17     private ListView listView;
18     private ArrayList<Student> data;
19     private FirstAdapter firstAdapter;
20     SecoundAdapter secoundAdapter;
21 
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         initView();
27         initData();
28 //        firstAdapter = new FirstAdapter(this,data);
29 //        listView.setAdapter(firstAdapter);
30         secoundAdapter =new SecoundAdapter(this,data);
31         listView.setAdapter(secoundAdapter);
32 
33     }
34 
35 
36     private void initView() {
37         listView = (ListView) findViewById(R.id.listview);
38         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
39             @Override
40                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
41                 Student student = (Student) parent.getItemAtPosition(position);
42 //                firstAdapter.setChosenStudent(student);
43                 if(view.hasFocusable()){
44                     view.setBackgroundResource(R.color.item_pink);
45                 }
46 
47                 Toast.makeText(getApplicationContext(),"sad",Toast.LENGTH_SHORT).show();
48             }
49         });
50 
51     }
52 
53 
54 
55 
56 
57     private void initData() {
58         data = new ArrayList<>();
59         data.add(new Student("张三", "北京"));
60         data.add(new Student("张学友", "香港"));
61         data.add(new Student("周杰伦", "台湾"));
62         data.add(new Student("梁朝伟", "香港"));
63         data.add(new Student("李四", "合肥"));
64 
65 
66     }
67 
68 }

 

常见的适配器总结

标签:

原文地址:http://www.cnblogs.com/fightzhao/p/4813143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!