网上关于这方面的好的文章:
http://yilee.info/android-diy-listview.html
http://blog.csdn.net/xiaominghimi/article/details/6314704
http://blog.csdn.net/chenzheng_java/article/details/6202586#
主布局文件:
- main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/lv"
- />
- <ListView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/lv_2"
- />
- </LinearLayout>
- item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_marginTop="10dip"
- android:layout_marginLeft="20dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <RelativeLayout android:id="@+id/rl_root"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:layout_marginTop="5dip"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#0F6D91"
- android:textSize="18dip"
- android:id="@+id/tv_wordbreeak_danci"
- />
- <TextView
- android:layout_marginTop="5dip"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#0F6D91"
- android:textSize="18dip"
- android:id="@+id/tv_wordbreeak_dancinum"
- />
- <Button
- android:id="@+id/bt_wordbreak_xiangxi"
- android:layout_height="wrap_content"
- android:layout_width="110dp"
- android:text="详细"
- android:textColor="#0F6D91"
- android:layout_alignParentRight="true"
- android:layout_gravity="right"
- />
- </RelativeLayout>
- </LinearLayout>
- item2.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_marginTop="10dip"
- android:layout_marginLeft="20dip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <RelativeLayout android:id="@+id/rl_root"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:layout_marginTop="5dip"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#0F6D91"
- android:textSize="18dip"
- android:id="@+id/tv_1"
- />
- <TextView
- android:layout_marginTop="5dip"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="50dip"
- android:textColor="#0F6D91"
- android:textSize="18dip"
- android:id="@+id/tv_2"
- />
- <TextView
- android:layout_marginTop="5dip"
- android:layout_width="wrap_content"
- android:layout_marginLeft="100dip"
- android:layout_height="wrap_content"
- android:textColor="#0F6D91"
- android:textSize="18dip"
- android:id="@+id/tv_3"
- />
- </RelativeLayout>
- </LinearLayout>
- MainActivity.java
- package cn.ls;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import android.app.Activity;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.widget.ListView;
- /**
- * 这里有两个适配器。
- * @author Administrator
- *
- */
- public class MainActivity extends Activity {
- ListView listView , lv_2;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /*通过ListView的ID来获取*/
- listView = (ListView) findViewById(R.id.lv);
- lv_2 = (ListView) findViewById(R.id.lv_2);
- ArrayList<HashMap<String, Object>> data = getData();
- ArrayList<HashMap<String, Object>> data2 = getData2();
- MyAdapter adapter = new MyAdapter(this, data);
- MyAdapter2 adapter_2 = new MyAdapter2(this, data2); //适配三个TextView.
- // setListAdapter(adapter); //这个是ListView中的内部类
- listView.setAdapter(adapter);
- lv_2.setAdapter(adapter_2);
- }
- /**
- * 显示三个TextView的数据。
- * @return
- */
- private ArrayList<HashMap<String, Object>> getData2() {
- ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
- //根据需求添加一些数据,
- HashMap<String, Object> tempHashMap = new HashMap<String, Object>();
- //相同的样式布局,可以设置显示不同的文字。
- for (int i = 0; i <25; i++) {
- tempHashMap.put("tv_1", "左面");
- tempHashMap.put("tv_2", "中间");
- tempHashMap.put("tv_3", "右面");
- arrayList.add(tempHashMap);
- }
- System.out.println("个数---------------:" + arrayList.size());
- return arrayList;
- }
- /**
- * 显示TextView和按钮。 TextView是变动的。
- * @return
- */
- private ArrayList<HashMap<String, Object>> getData() {
- ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>();
- //根据需求添加一些数据,
- HashMap<String, Object> tempHashMap = new HashMap<String, Object>();
- //相同的样式布局,可以设置显示不同的文字。
- for (int i = 0; i <5; i++) {
- tempHashMap.put("danci1", "餐饮美食");
- tempHashMap.put("danci1_2", "美食");
- tempHashMap.put("danci1_3", "我爱美食");
- tempHashMap.put("danci2", "10");//sin.get();
- arrayList.add(tempHashMap);
- }
- System.out.println("个数:" + arrayList.size());
- return arrayList;
- }
- }
- <pre name="code" class="java">MyAdapter.java
- /**
- * 自定义适配器。
- * @author Administrator
- *
- */
- public class MyAdapter extends BaseAdapter{
- private ArrayList<HashMap<String, Object>> data;
- private LayoutInflater layoutInflater;
- private Context context;
- public MyAdapter(Context context,ArrayList<HashMap<String, Object>> data) {
- this.context = context;
- this.data = data;
- this.layoutInflater = LayoutInflater.from(context);
- }
- public int getCount() {
- return data.size();
- }
- public Object getItem(int position) {
- return data.get(position);
- }
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ZuJian zuJian = null;
- if (convertView == null){
- zuJian = new ZuJian();
- // 获取组件布局
- convertView = layoutInflater.inflate(R.layout.item, null);
- zuJian.dnaci1 = (TextView) convertView.findViewById(R.id.tv_wordbreeak_danci);
- zuJian.button = (Button) convertView.findViewById(R.id.bt_wordbreak_xiangxi);
- // 这里要注意,是使用的tag来存储数据的。
- convertView.setTag(zuJian);
- }else{
- zuJian = (ZuJian) convertView.getTag();
- }
- // 绑定数据、以及事件触发 。可以根据不同的条目添加不同的文字。
- switch(position){
- case 0:
- zuJian.dnaci1.setText((String)data.get(position).get("danci1"));
- break;
- case 1:
- zuJian.dnaci1.setText((String)data.get(position).get("danci1_3"));
- break;
- case 2:
- zuJian.dnaci1.setText((String)data.get(position).get("danci1_2"));
- break;
- case 3:
- zuJian.dnaci1.setText((String)data.get(position).get("danci1_2"));
- break;
- case 4:
- zuJian.dnaci1.setText((String)data.get(position).get("danci1"));
- break;
- }
- zuJian.button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- //点击事件。
- }
- });
- return convertView;
- }
- }</pre><br>
- <br>
- <pre></pre>
- <p><br>
- </p>
- 适配器文件2<br>
- <pre name="code" class="java">MyAdapter2.java
- /**
- * 这是第二个适配器
- * @author Administrator
- *
- */
- public class MyAdapter2 extends BaseAdapter{
- private ArrayList<HashMap<String, Object>> data2;
- private LayoutInflater layoutInflater;
- private Context context;
- public MyAdapter2(Context context, ArrayList<HashMap<String, Object>> data2) {
- this.context = context;
- this.data2 = data2;
- this.layoutInflater = LayoutInflater.from(context);
- }
- @Override
- public int getCount() {
- return data2.size();
- }
- @Override
- public Object getItem(int position) {
- return data2.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- ZuJian zuJian = null;
- if (convertView == null){
- zuJian = new ZuJian();
- // 获取组件布局
- convertView = layoutInflater.inflate(R.layout.item_2, null);
- zuJian.tv_1 = (TextView) convertView.findViewById(R.id.tv_1);
- zuJian.tv_2 = (TextView) convertView.findViewById(R.id.tv_2);
- zuJian.tv_3 = (TextView) convertView.findViewById(R.id.tv_3);
- // 这里要注意,是使用的tag来存储数据的。
- convertView.setTag(zuJian);
- }else{
- zuJian = (ZuJian) convertView.getTag();
- }
- /*获取map中对应的数据的数据,然后显示在ListView中*/
- zuJian.tv_1.setText((String)data2.get(position).get("tv_1"));
- zuJian.tv_2.setText((String)data2.get(position).get("tv_2"));
- zuJian.tv_3.setText((String)data2.get(position).get("tv_3"));
- return convertView;
- }
- }</pre><br>
- <pre name="code" class="java">ZuJian.java
- public class ZuJian {
- public TextView dnaci1;
- public TextView danci2;
- public Button button;
- public TextView dnaci1_2;
- public TextView danci2_2;
- public Button button_2;
- public TextView tv_1;
- public TextView tv_2;
- public TextView tv_3;
- }</pre><br>
- <br>