[关闭]
@zhutoulwz 2015-03-26T23:44:05.000000Z 字数 4962 阅读 2293

Android自定义ListView

android


实现自定义Adapter

首先需要自定义一个adapter类,继承BaseAdapter,具体如下:

  1. class ViewHolder
  2. {
  3. public ImageView imageView;
  4. public TextView textView;
  5. public Button button;
  6. }
  7. class MyAdapter extends BaseAdapter
  8. {
  9. private LayoutInflater inflater;
  10. public MyAdapter(Context context)
  11. {
  12. inflater = LayoutInflater.from(context);
  13. }
  14. @Override
  15. public int getCount() {
  16. return data.size();
  17. }
  18. @Override
  19. public Object getItem(int location) {
  20. return data.get(location);
  21. }
  22. @Override
  23. public long getItemId(int position) {
  24. return 0;
  25. }
  26. @Override
  27. public View getView(int position, View convertView, ViewGroup parent) {
  28. ViewHolder holder = null;
  29. if (convertView == null)
  30. {
  31. holder = new ViewHolder();
  32. convertView = inflater.inflate(R.layout.list_item, null);
  33. holder.imageView = (ImageView) convertView.findViewById(R.id.img);
  34. holder.textView = (TextView) convertView.findViewById(R.id.text);
  35. holder.button = (Button) convertView.findViewById(R.id.button);
  36. convertView.setTag(holder);
  37. }
  38. else {
  39. holder = (ViewHolder) convertView.getTag();
  40. }
  41. holder.imageView.setImageResource((Integer)data.get(position).get("img"));
  42. holder.textView.setText(data.get(position).get("info").toString());
  43. final int p = position;
  44. holder.button.setOnClickListener(new OnClickListener() {
  45. @Override
  46. public void onClick(View v) {
  47. Toast.makeText(getApplicationContext(), String.valueOf(p), Toast.LENGTH_LONG).show();
  48. }
  49. });
  50. return convertView;
  51. }
  52. }

自定义布局文件

然后,需要自定义一个xml文件,即list_item的布局文件,如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/bg"
  6. android:clickable="true">
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="50dip"
  10. android:orientation="horizontal"
  11. android:layout_alignParentLeft="true"
  12. android:gravity="center_vertical">
  13. <ImageView
  14. android:id="@+id/img"
  15. android:layout_width="30dip"
  16. android:layout_height="30dip"
  17. android:scaleType="centerInside"
  18. android:src="@drawable/ic_launcher"
  19. />
  20. <TextView
  21. android:id="@+id/text"
  22. android:layout_width="wrap_content"
  23. android:layout_height="fill_parent"
  24. android:text="hello"
  25. android:textSize="16sp"
  26. android:textColor="#ccc"
  27. android:gravity="center_vertical"
  28. android:layout_gravity="center_vertical"
  29. android:layout_marginLeft="10dip"/>
  30. </LinearLayout>
  31. <Button
  32. android:id="@+id/button"
  33. android:text="Button"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:gravity="center_vertical"
  37. android:layout_alignParentRight="true"/>
  38. </RelativeLayout>

整个完整代码如下:

  1. package com.lwz.activity;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.app.Activity;
  7. import android.content.Context;
  8. import android.os.Bundle;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12. import android.view.ViewGroup;
  13. import android.widget.AdapterView;
  14. import android.widget.AdapterView.OnItemClickListener;
  15. import android.widget.BaseAdapter;
  16. import android.widget.Button;
  17. import android.widget.ImageView;
  18. import android.widget.ListView;
  19. import android.widget.TextView;
  20. import android.widget.Toast;
  21. import com.example.testandroid.R;
  22. public class MainActivity extends Activity {
  23. private ListView list;
  24. private List<Map<String, Object>> data;
  25. @Override
  26. protected void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.activity_main);
  29. init();
  30. }
  31. public void init()
  32. {
  33. list = (ListView) findViewById(R.id.list1);
  34. // set data
  35. data = new ArrayList<Map<String,Object>>();
  36. Map<String, Object> map = new HashMap<String, Object>();
  37. map.put("img", R.drawable.ic_launcher);
  38. map.put("info", "hello");
  39. data.add(map);
  40. MyAdapter adapter = new MyAdapter(this);
  41. list.setAdapter(adapter);
  42. list.setOnItemClickListener(new OnItemClickListener() {
  43. @Override
  44. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  45. long arg3) {
  46. Toast.makeText(getApplicationContext(), "click" + arg2, Toast.LENGTH_LONG).show();
  47. }
  48. });
  49. }
  50. class ViewHolder
  51. {
  52. public ImageView imageView;
  53. public TextView textView;
  54. public Button button;
  55. }
  56. class MyAdapter extends BaseAdapter
  57. {
  58. private LayoutInflater inflater;
  59. public MyAdapter(Context context)
  60. {
  61. inflater = LayoutInflater.from(context);
  62. }
  63. @Override
  64. public int getCount() {
  65. return data.size();
  66. }
  67. @Override
  68. public Object getItem(int location) {
  69. return data.get(location);
  70. }
  71. @Override
  72. public long getItemId(int position) {
  73. return 0;
  74. }
  75. @Override
  76. public View getView(int position, View convertView, ViewGroup parent) {
  77. ViewHolder holder = null;
  78. if (convertView == null)
  79. {
  80. holder = new ViewHolder();
  81. convertView = inflater.inflate(R.layout.list_item, null);
  82. holder.imageView = (ImageView) convertView.findViewById(R.id.img);
  83. holder.textView = (TextView) convertView.findViewById(R.id.text);
  84. holder.button = (Button) convertView.findViewById(R.id.button);
  85. convertView.setTag(holder);
  86. }
  87. else {
  88. holder = (ViewHolder) convertView.getTag();
  89. }
  90. holder.imageView.setImageResource((Integer)data.get(position).get("img"));
  91. holder.textView.setText(data.get(position).get("info").toString());
  92. final int p = position;
  93. holder.button.setOnClickListener(new OnClickListener() {
  94. @Override
  95. public void onClick(View v) {
  96. Toast.makeText(getApplicationContext(), String.valueOf(p), Toast.LENGTH_LONG).show();
  97. }
  98. });
  99. return convertView;
  100. }
  101. }
  102. }

注:这是自己的笔记,仅供参考

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注