通用的Adapter
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_main_apps" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </ListView> </LinearLayout>
item_app.xml
<?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="70dp" android:minHeight="70dp" android:orientation="horizontal" android:gravity="center_vertical"> <ImageView android:id="@+id/iv_item_icon" android:layout_width="70dp" android:layout_height="70dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv_item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>
AppInfo
package com.example.app3_listview; import android.graphics.drawable.Drawable; public class AppInfo { private Drawable icon; private String appName; private String packageName; public AppInfo(Drawable icon, String appName, String packageName) { super(); this.icon = icon; this.appName = appName; this.packageName = packageName; } public AppInfo() { super(); } public Drawable getIcon() { return icon; } public void setIcon(Drawable icon) { this.icon = icon; } public String getAppName() { return appName; } public void setAppName(String appName) { this.appName = appName; } public String getPackageName() { return packageName; } public void setPackageName(String packageName) { this.packageName = packageName; } @Override public String toString() { return "AppInfo [icon=" + icon + ", appName=" + appName + ", packageName=" + packageName + "]"; } }
MainActivity
package com.example.app3_listview; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import com.example.adapter.base.CommonBaseAdapter; import com.example.adapter.base.ViewHolder; public class MainActivity extends Activity { private ListView lv_main_apps; private List<AppInfo> data; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 载入视图--仅仅有一个listview setContentView(R.layout.activity_main); // 获取id lv_main_apps = (ListView) findViewById(R.id.lv_main_apps); // 得到数据 data = getAllAppInfos(); // 创建adapter adapter = new MyAdapter(this, data, R.layout.item_app); // 设置adapter显示 lv_main_apps.setAdapter(adapter); // 设置每项(item)的点击监听 lv_main_apps .setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {// position 点击的下标 // 得到数据 AppInfo appInfo = data.get(position); String appName = appInfo.getAppName(); // 显示 Toast.makeText(MainActivity.this, appName, 0).show(); } }); } /* * 得到手机中全部应用信息的列表 AppInfo */ protected List<AppInfo> getAllAppInfos() { List<AppInfo> list = new ArrayList<AppInfo>(); // 得到应用的packgeManager PackageManager packageManager = getPackageManager(); // 创建一个主界面的intent Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); // 得到包括应用信息的列表 List<ResolveInfo> ResolveInfos = packageManager.queryIntentActivities( intent, 0); // 遍历 for (ResolveInfo ri : ResolveInfos) { // 得到包名 String packageName = ri.activityInfo.packageName; // 得到图标 Drawable icon = ri.loadIcon(packageManager); // 得到应用名称 String appName = ri.loadLabel(packageManager).toString(); // 封装应用信息对象 AppInfo appInfo = new AppInfo(icon, appName, packageName); // 加入到list list.add(appInfo); } return list; } /** * 继承自CommonBaseAdapter的adapter类 */ class MyAdapter extends CommonBaseAdapter<AppInfo> { public MyAdapter(Context context, final List<AppInfo> data, int layoutId) { super(context, data, layoutId, new Convert() { @Override public void convert(ViewHolder holder, int position) { AppInfo appInfo = data.get(position); holder.setText(R.id.tv_item_name, appInfo.getAppName()).setImageDrawable(R.id.iv_item_icon,appInfo.getIcon()); } }); } } }
CommonBaseAdapter
package com.example.adapter.base; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; /** * 通用的baseAdapter */ public abstract class CommonBaseAdapter<T> extends BaseAdapter { private Context context; private List<T> data; private int layoutId; public Convert convert; /** * 设置视图数据的抽象方法, 由详细的adapter子类来实现 */ public interface Convert{ void convert(ViewHolder holder, int position); } public CommonBaseAdapter(Context context, List<T> data, int layoutId,Convert convert) { this.context = context; this.data = data; this.layoutId = layoutId; this.convert = convert; } @Override public int getCount() { return data.size(); } @Override public T getItem(int position) { return data.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { //得到Viewholder对象 ViewHolder holder = ViewHolder.getHolder(context, convertView, layoutId); //调用未实现的抽象方法设置数据 convert.convert(holder, position); //返回holder中的convertView return holder.getConvertView(); } }
ViewHolder
package com.example.adapter.base; import android.content.Context; import android.graphics.drawable.Drawable; import android.util.SparseArray; import android.view.View; import android.widget.ImageView; import android.widget.TextView; /** * 通用的ViewHolder类 */ public class ViewHolder { // 代表当前行的view对象 private View itemConvertView; // 用来替代Map<Integer,Object>的容器, 效率比map高 private SparseArray<View> views; /******************************************************************* * 得到ViewHolder对象 context--layoutId:这个是用来构造时候,须要动态载入布局文件 * convertview:是可复用的item布局 */ public static ViewHolder getHolder(Context context, View convertView, int layoutId) { /** * 假设没有可复用的布局。就创建本类对象 假设有的话,就通过getTag获取 * */ if (convertView == null) { return new ViewHolder(context, layoutId); } else { ViewHolder holder = (ViewHolder) convertView.getTag(); return holder; } } /** * 构造方法 载入布局文件。将本类标记即viewholder保存到itemConvertView中 */ private ViewHolder(Context context, int layoutId) { this.itemConvertView = View.inflate(context, layoutId, null); this.itemConvertView.setTag(this); views = new SparseArray<View>(); } /** * 得到当前相应的convertView */ public View getConvertView() { return itemConvertView; } /************************* * %%%%%%%%%%%%******************************** 依据视图id得到相应的视图对象 */ public <T extends View> T getView(int viewId) { View view = views.get(viewId); if (view == null) { view = itemConvertView.findViewById(viewId); views.put(viewId, view); } return (T) view; } // /** * 设置文本数据 */ public ViewHolder setText(int viewId, String text) { TextView textView = getView(viewId); textView.setText(text); return this; } /** * 设置drawable图片 */ public ViewHolder setImageDrawable(int viewId, Drawable drawable) { ImageView imageView = getView(viewId); imageView.setImageDrawable(drawable); return this; } /** * 设置资源图片 */ public ViewHolder setImageResource(int viewId, int resourceId) { ImageView imageView = getView(viewId); imageView.setImageResource(resourceId); return this; } }