ListView自定义
作者:小强 | 来源:互联网 | 2023-10-11 10:20
ListView网上资料很多,我所知扩展性最好、最通用的代码做个备忘。总体上来讲:ListView+BaseAdapter+xml文件实现:话不多说,先贴个图,预期效果如下:
ListView网上资料很多,我所知扩展性最好、最通用的代码做个备忘。
总体上来讲:ListView+BaseAdapter+xml文件实现:
话不多说,先贴个图,预期效果如下:
第一步、主方法lin.xml,加一个listveiw布局
第二步:自定义item.xml(写Demo有些懒,稍微简化了几个textview)
第二步、写一个Databean文件,用来包含所有item的内容集
import java.io.Serializable;
public class DataBean implements Serializable{
/**
*
*/
private static final long serialVersiOnUID= 1L;
int imgId;
String title;
String address;
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
第三步、写一个adapter继承自baseAdapter
package com.mylistview.listviewdm;
import java.util.ArrayList;
import com.mylistview.bean.DataBean;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
Context mContext;
ArrayList mData;
public ListViewAdapter(Context mContext, ArrayList mData) {
super();
this.mCOntext= mContext;
this.mData = mData;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (cOnvertView== null) {
holder = new ViewHolder();
cOnvertView= View.inflate(mContext, R.layout.item, null);
holder.img_1 = (ImageView) convertView.findViewById(R.id.img_1);
holder.title_1 = (TextView) convertView.findViewById(R.id.title_1);
holder.address = (TextView) convertView.findViewById(R.id.address);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.img_1.setBackgroundResource(mData.get(position).getImgId());
holder.title_1.setText(mData.get(position).getTitle());
holder.address.setText(mData.get(position).getAddress());
return convertView;
}
class ViewHolder {
ImageView img_1;
TextView title_1;
TextView address;
}
}
第五步、拟数据到主方法,设置listveiw适配器
package com.mylistview.listviewdm;
import java.util.ArrayList;
import com.mylistview.bean.DataBean;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends Activity {
private ArrayList mData;
private ListView list_v;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lin);
initView();
initData();
}
private void initView() {
// TODO Auto-generated method stub
list_v = (ListView)findViewById(R.id.lst_v);
}
private void initData() {
// TODO Auto-generated method stub
mData = new ArrayList();
setData();
list_v.setAdapter(new ListViewAdapter(this, mData));
}
private void setData() {
// TODO Auto-generated method stub
for (int i = 0; i <4; i++) {
DataBean mBean2 = new DataBean();
mBean2.setImgId(R.drawable.img_1);
mBean2.setTitle("豫蜀房河南烩面");
mBean2.setAddress("快餐 五棵松");
mData.add(mBean2);
DataBean mBean = new DataBean();
mBean.setImgId(R.drawable.img_5);
mBean.setTitle("三样菜(阜成路店)");
mBean.setAddress("川菜 四季青");
mData.add(mBean);
DataBean mBean3 = new DataBean();
mBean3.setImgId(R.drawable.img_6);
mBean3.setTitle("郁陵岛炭火烤肉");
mBean3.setAddress("韩国菜 航天桥");
mData.add(mBean3);
}
}
}
最后贴一张实现的效果图
ListView自定义