当我想要在listview上展示不同类型的数据的时候,就不能仅仅满足于使用一个简单的ArrayAdapter适配器了,这时,我就想要扩展Adapter的功能,自定义一个适配器继承自BaseAdapter,BaseAdapter有良好的扩充性能。例子如下:
现在.xml布局文件中添加listview控件,可以参考我上一篇文章。
我想在listview的每一个item中放入图片、文字等信息,所以新建一个.xml布局文件设计item,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img"
android:layout_width="80dp"
android:layout_height="120dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="@drawable/img0" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/img"
android:layout_alignParentRight="true"
android:text="购票" />
<TextView
android:id="@+id/tv3"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/img"
android:layout_marginTop="17dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:padding="5dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="我是内容我是内容我是内容我是内容我是内容我是内容我是内容"
/>
//这里的文字有跑马灯效果
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/img"
android:layout_marginLeft="24dp"
android:layout_toRightOf="@+id/img"
android:text="电影名字"
android:textSize="20sp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn"
android:layout_alignLeft="@+id/tv1"
android:text="周星驰"
android:textSize="20sp" />
</RelativeLayout>
我新建一个类Information放置我需要的变量的类型。
public class Information {
String movie_name;
String actor;
int image;
String introduce;
}
接下来就可以实现我想要实现的功能了:
public class SecondActivity extends Activity implements OnItemClickListener,
OnItemLongClickListener {
ListView lv;
/* 数据 */
String[] movie_name = { "喜剧之王", "大话西游", "功夫", "逃学威龙", "大内密探零零发" };
String[] actor = { "周星驰", "朱茵", "黄圣依", "吴孟达", "刘嘉玲" };
int[] image = { R.drawable.img0, R.drawable.img1, R.drawable.img2,
R.drawable.img3, R.drawable.img4 };
String[] introduce = {
"喜剧之王的介绍喜剧之王的介绍喜剧之王的介绍喜剧之王的介绍喜剧之王的介绍",
"故事发生在《大话西游》上一集《月光宝盒》之前500年。至尊宝被月光宝盒带回到五百年前,恰巧遇到紫霞仙子...",
" 《功夫》(英语:Kung Fu Hustle)是一部于2004年上映的华语电影,由周星驰担任导演、编剧兼主演。片中包含了对武侠文化的敬仰、独特性与周星驰一贯的无厘头、夸张的搞笑风格...",
" 周星星(周星驰 饰)是飞虎队队长,奉命以卧底的身份到圣育强中学寻找警司于参观时被窃的配枪。周星星就算百般无奈还是接受了任务...",
"《大内密探零零发》是一部拍摄于1996年的古装武侠外星科幻再加上无厘头的搞笑贺岁片电影..." };
Myadapter myadapter = new Myadapter();
List<Information> list = new ArrayList<Information>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lv = (ListView) findViewById(R.id.lv);
AddData();//调用AddData()函数
/*
* LayoutInflater inflater = getLayoutInflater();//LayoutInflater布局加载类
* View v = inflater.inflate(R.layout.item, null);
* lv.addHeaderView(v);//在 这个组件的上方添加其他组件
*/
lv.setAdapter(myadapter);
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
}
private void AddData() {
// TODO Auto-generated method stub
int x = 0;
for (int i = 0; i < 100; i++) {
x = i % 5;
Information info = new Information();
info.actor = actor[x];
info.image = image[x];
info.introduce = introduce[x];
info.movie_name = movie_name[x];
list.add(info);
}
}
class Myadapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) { // 返回指定索引项对应的数据项
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* 第一个参数表示该item在adapter的位置,第二个参数表示item的view对象,也就是滑动listview时显示的每个item
* , listview有循环回收机制,会把每一个滑出界面的item返回 到回收站,第三个参数用来加载视图
*/
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(SecondActivity.this);
View v = inflater.inflate(R.layout.item, null);
ImageView img = (ImageView) v.findViewById(R.id.img);
TextView tv1 = (TextView) v.findViewById(R.id.tv1);
TextView tv2 = (TextView) v.findViewById(R.id.tv2);
TextView tv3 = (TextView) v.findViewById(R.id.tv3);
Information info = (Information) getItem(position);
img.setImageResource(info.image);
tv1.setText(info.movie_name);
System.out.println("电影名结果是::" + info.movie_name);
tv2.setText(info.actor);
tv3.setText(info.introduce);
**tv3.setSelected(true);// 为了设置跑马灯效果**
return v;
}
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(SecondActivity.this, "djjshj", Toast.LENGTH_SHORT)
.show();
}
效果图如下:
有一些问题需要注意:
- Layoutinflater布局加载类,在item显示一个独立的布局文件时,需要用这个类加载这个布局,之后再做一系列的操作。
LayoutInflater inflater = LayoutInflater.from(SecondActivity.this);
View v = inflater.inflate(R.layout.item, null);
还有一种方法:
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.head, null); 我在item里加入了跑马灯效果,一般来说在.xml布局中设置好就可以显示效果了,在ListView的 Adapter里面,加上TextView.setSelected(true)
关于list…