Android乐学成语之自定义Adapter
一、首先对Adapter概念深刻的了解
首先看看他的继承图
- BaseAdapter:抽象类,实际开发中我们会继承这个类并且重写相关方法,用得最多的一个Adapter!
- ArrayAdapter:支持泛型操作,最简单的一个Adapter,只能展现一行文字~
- SimpleAdapter:同样具有良好扩展性的一个Adapter,可以自定义多种效果!
- SimpleCursorAdapter:用于显示简单文本类型的listView,一般在数据库那里会用到,不过有点过时, 不推荐使用!
在这里拿ListView控件给大家讲解!
二、实例部分
下面让我们实现一个这样的页面
主页布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_ling" android:orientation="vertical" > <ListView android:id="@+id/lvAnimalList" android:layout_width="match_parent" android:layout_height="wrap_content" android:layoutAnimation="@anim/anim_layout_listview" android:listSelector="#00000000" > </ListView> </LinearLayout>
子布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:gravity="center" android:text="@string/animal_name" android:textAppearance="?android:attr/textAppearanceLarge" > </TextView> <ImageButton android:id="@+id/btnSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:layout_alignParentRight="true" android:layout_alignTop="@+id/tvName" android:src="@drawable/btnsave" /> </RelativeLayout>
代码的实现:
package com.bzu.qilu.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bzu.qilu.R; import com.bzu.qilu.entity.Fable; /** * 寓言类适配器 * @author 刘齐鲁 * date 2016-5-31 */ public class FableAdapter extends ArrayAdapter<Fable> { private int resourceld; private Context context; public FableAdapter(Context context,int resource,List<Fable>objects){ super(context,resource,objects); this.context =context; resourceld = resource; } public View getView(int position,View convertView,ViewGroup parent){ final Fable fable= getItem(position);//获取当前项的Animal实例 View view ; ViewHolder viewHolder; if(convertView == null){ view = LayoutInflater.from(getContext()).inflate(resourceld, null); viewHolder = new ViewHolder(); viewHolder.tvName=(TextView)view.findViewById(R.id.tvName); viewHolder.btnSave=(ImageButton)view.findViewById(R.id.btnSave); ViewHolder.btnSave.setFocusable(false); ViewHolder.btnSave.setFocusableInTouchMode(false); viewHolder.btnSave.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Toast.makeText(context, "你要收藏"+fable.getName()+"吗", Toast.LENGTH_SHORT).show(); } }); view.setTag(viewHolder);//将ViewHolder存储在View中 }else{ view = convertView; viewHolder =(ViewHolder) view.getTag();//重新获取ViewHolder } viewHolder.tvName.setText(fable.getName()); return view; } static class ViewHolder{ static ImageView btnSave; TextView tvName; } }
我们新增了一个内部类ViewHolder,用于对控件的实例进行缓存。当convertView为空的时候,创建一个ViewHolder对象,并将控件的实例都存放在ViewHolder里,然后调用View的setTag()方法,将ViewHolder对象存储在View中。当convertView不为空的时候则调用View的getTag()方法,把ViewHolder重新取出。这样所有的控件的实例都缓存在了ViewHolder里,就没有必要每次都通过findViewById()方法来获取控件实例了。
Activity代码:
package com.bzu.qilu.activity; import java.util.ArrayList; import java.util.List; import com.bzu.qilu.R; import com.bzu.qilu.adapter.CategoryAdapter; import com.bzu.qilu.entity.Category; import android.app.Activity; import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; /** * 成语类别显示界面 * @author 刘齐鲁 * date 2016-5-16 */ public class StudyActivity extends Activity { private List<Category>categoryList; private String [] category_names; private int [] category_images; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_study); initCategories();//初始化类别 CategoryAdapter adapter =new CategoryAdapter(this, R.layout.category_item, categoryList); ListView listView =(ListView) findViewById(R.id.lvCategories); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { // TODO Auto-generated method stub switch (position) { case 0: { Intent intent = new Intent(StudyActivity.this,StudyAnimalActivity.class); startActivity(intent); break; } case 1: { Intent intent1 = new Intent(StudyActivity.this,StudyNatureActivity.class); startActivity(intent1); break; } case 2: { Intent intent1 = new Intent(StudyActivity.this,StudyPersonActivity.class); startActivity(intent1); break; } case 3: { Intent intent1 = new Intent(StudyActivity.this,StudySeasonActivity.class); startActivity(intent1); break; } case 4: { Intent intent1 = new Intent(StudyActivity.this,StudyNumberActivity.class); startActivity(intent1); break; } case 5: { Intent intent1 = new Intent(StudyActivity.this,StudyFableActivity.class); startActivity(intent1); break; } case 6: { Intent intent1 = new Intent(StudyActivity.this,StudyOtherActivity.class); startActivity(intent1); break; } default: break; } } }); } private void initCategories(){ categoryList = new ArrayList<Category>(); Resources resources =getResources(); category_names=resources.getStringArray(R.array.category); category_images= new int []{R.drawable.category_animal,R.drawable.category_nature, R.drawable.category_human,R.drawable.category_season,R.drawable.category_number,R.drawable.category_fable,R.drawable.category_other}; for(int i=0;i<category_names.length;i++){ categoryList.add(new Category(category_names[i],category_images[i])); } } }
以上就是那个页面的全部代码。