BaseAdapter
BaseAdapter主要用于Spinner和ListView,GridView的实现,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,它是直接继承自接口类Adapter的,需要重写许多方法。
BaseAdapter的基本使用步骤
1.item布局文件,自定义item布局文件,列如:
res/layout/listview_item.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="match_parent" android:orientation="horizontal" > <ImageView android:id="@+id/iconIv" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/singerTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1" android:singleLine="true" /> <TextView android:id="@+id/songNameTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="2" android:singleLine="true" /> <!-- ListView的item中加入Button后 导致ListView对OnItemClick事件无法响应 原因是因为Button的事件响应优先级高于List Item,所以屏蔽了ListItem的单击事件 android:focusableInTouchMode="false" android:clickable="false" --> <Button android:id="@+id/deleteBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:focusable="false" android:text="删除" /> <Button android:id="@+id/downloadBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:focusable="false" android:text="下载" /> </LinearLayout> |
|
2.定义数据源,在实际开发中数据来源于数据库,这里我就直接定义了
public class MainActivity extends ListActivity {
private ArrayList songList;
private String songName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
songList = new ArrayList();
HashMap songMap = new HashMap();
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "陈奕迅");
songMap.put("songName", "好久不见.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "王菲");
songMap.put("songName", "旋转的木马.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "陈奕迅");
songMap.put("songName", "婚礼的祝福.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "王菲");
songMap.put("songName", "棋子.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "王菲");
songMap.put("songName", "红豆.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "陈奕迅");
songMap.put("songName", "幸福摩天轮.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "陈奕迅");
songMap.put("songName", "爱情转移.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "陈奕迅");
songMap.put("songName", "稳稳的幸福.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "王菲");
songMap.put("songName", "我愿意.mp3");
songList.add(songMap);
songMap = new HashMap();
songMap.put("image", R.mipmap.ic_launcher);
songMap.put("singer", "王菲");
songMap.put("songName", "传奇.mp3");
songList.add(songMap);
3.应用自定义适配器
SongAdapter adapter = new SongAdapter(this, songList);
4.将适配器设置给控件
setListAdapter(adapter);
5.监听事件
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View itemView,
int position, long id) {
Intent intent = new Intent(MainActivity.this,MainActivity.class);
intent.putExtra("songName", (String) (((HashMap) (songList.get(position))).get("songName")));
startActivity(intent);
}
});
}
6.自定义适配器,继承BaseAdapter 并重写其方法
public class SongAdapter extends BaseAdapter {
private Context mContext;
private ArrayList songBAList;
public SongAdapter(Context c, ArrayList a) {
mContext = c;
songBAList = a;
}
//统计item数
@Override
public int getCount() {
return songBAList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// LayoutInflater 获取layout
//findViewById()获取控件
LayoutInflater mInflater = LayoutInflater.from(mContext);
View itemView = mInflater.inflate(R.layout.listview_item, null);
ImageView iconIv = (ImageView) itemView.findViewById(R.id.iconIv);
TextView singerTv = (TextView) itemView.findViewById(R.id.singerTv);
TextView songNameTv = (TextView) itemView.findViewById(R.id.songNameTv);
Button deleteBtn = (Button) itemView.findViewById(R.id.deleteBtn);
Button downloadBtn = (Button) itemView.findViewById(R.id.downloadBtn);
iconIv.setImageResource((Integer) (((HashMap) (songBAList.get(position)))
.get("image")));
singerTv.setText((String) (((HashMap) (songBAList.get(position)))
.get("singer")));
songNameTv.setText((String) (((HashMap) (songBAList.get(position)))
.get("songName")));
// 设置相关联的View的额外的信息
//给Button设置标签
deleteBtn.setTag(position);
downloadBtn.setTag(position);
// 点击Item中的删除按钮
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取Button的标签
int position = Integer.parseInt(v.getTag().toString());
songList.remove(position);//删除一条数据
SongAdapter.this.notifyDataSetChanged();//刷新
}
});
// 点击Item中的下载按钮
downloadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
Toast.makeText(MainActivity.this, "准备下载:" + (String) (((HashMap) (songBAList.get(position)))
.get("songName")),
Toast.LENGTH_SHORT).show();
}
});
return itemView;
}
}
}