ListView网上资料很多,我所知扩展性最好、最通用的代码做个备忘。
总体上来讲:ListView+BaseAdapter+xml文件实现:
话不多说,先贴个图,预期效果如下:
首先:这样一个ListView的item包含:图片、文字,而且布局也比较复杂。首选自定义这样一个模版的xml文件。
第一步、主方法lin.xml,加一个listveiw布局
<?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="#FFFFFF" android:orientation="vertical" > <ListView android:id="@+id/lst_v" android:layout_width="match_parent" android:layout_height="match_parent" ></ListView> </LinearLayout>
第二步:自定义item.xml(写Demo有些懒,稍微简化了几个textview)
<?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:background="#ffffff" > <ImageView android:id="@+id/img_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:background="@drawable/img_1" /> <TextView android:id="@+id/title_1" android:layout_width="130dp" android:layout_height="wrap_content" android:layout_toRightOf="@+id/img_1" android:gravity="left" android:paddingLeft="10dp" android:paddingTop="10dp" android:text="豫蜀房河南烩面" android:textColor="#000000" android:singleLine="true" android:textSize="16sp" /> <ImageView android:id="@+id/img_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_toRightOf="@+id/title_1" android:background="@drawable/img_2" /> <ImageView android:id="@+id/img_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="4dp" android:layout_marginTop="8dp" android:layout_toRightOf="@+id/img_2" android:background="@drawable/img_3" /> <ImageView android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title_1" android:layout_marginLeft="6dp" android:layout_marginTop="6dp" android:layout_toRightOf="@+id/img_1" android:background="@drawable/score" /> <TextView android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/score" android:layout_toRightOf="@+id/img_1" android:paddingLeft="12dp" android:paddingTop="10dp" android:text="快餐 五棵松" android:textSize="14sp" /> <View android:layout_width="match_parent" android:layout_height="0.2dp" android:layout_alignBottom="@+id/img_1" android:background="#cccccc" /> </RelativeLayout>
第二步、写一个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<DataBean> mData; public ListViewAdapter(Context mContext, ArrayList<DataBean> 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<DataBean> 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<DataBean>(); 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); } } }
最后贴一张实现的效果图
时间: 2024-11-03 01:21:38