一:BaseAdapter:共同实现的基类的适配器,是ArrayAdapter SimpleAdapter等的父类,
一般用于比较复杂的ListView,扩展性强。
详细信息可查看谷歌官方API:http://developer.android.com/reference/android/widget/BaseAdapter.html
1:构造方法:BaseAdapter();
2:公共方法:
ArrayAdapter:所支持的一个具体BaseAdapter任意对象数组。这个类在默认情况下预计提供的资源id引用一个TextView。如果您希望使用更复杂的布局,使用构造函数,还需要一个字段id。该字段id应该引用一个TextView在大布局资源。
使用简单轻便。
详细信息可查看谷歌官方API:http://developer.android.com/reference/android/widget/ArrayAdapter.html
构造方法:
其中:ArrayAdapter(Context context, int resource, List<T> objects)较为常用;
公共方法:
二:案例学习:
1:应用App功能展示:
2:布局代码为:activity_main.xml imagelistview.xml textlistview.xml
activity_main.xml:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context=".MainActivity"> 11 12 <ListView 13 android:id="@+id/listview_main" 14 android:layout_width="wrap_content" 15 android:layout_height="match_parent" 16 android:layout_weight="1"></ListView> 17 18 <ListView 19 android:id="@+id/listText_item" 20 android:layout_width="wrap_content" 21 android:layout_height="match_parent" 22 android:layout_weight="2"> 23 24 25 </ListView> 26 </LinearLayout>
imagelistview.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="horizontal"> 6 7 <ImageView 8 android:id="@+id/iamge_item" 9 android:layout_width="60dp" 10 android:layout_height="90dp" 11 android:background="@drawable/image1"/> 12 13 <LinearLayout 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:orientation="vertical"> 17 18 <TextView 19 android:id="@+id/title_item" 20 android:layout_width="match_parent" 21 android:layout_height="20dp" 22 android:text="标题" /> 23 24 <ScrollView 25 android:layout_width="match_parent" 26 android:layout_height="70dp"> 27 28 <TextView 29 android:id="@+id/content_item" 30 android:layout_width="match_parent" 31 android:layout_height="match_parent" 32 android:text="内容" /> 33 </ScrollView> 34 35 </LinearLayout> 36 </LinearLayout>
textlistview.xml:
1 <?xml version="1.0" encoding="utf-8"?> 2 <TextView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="30dp" 5 android:orientation="vertical" 6 android:scrollbars="vertical"></TextView>
3:Activity代码为:
Msg对象类如下:
1 package activity.cyq.listviewlearn; 2 3 /** 4 * Created by Android_007 on 2015/8/4. 5 */ 6 public class Msg { 7 8 public int iconId; 9 public String title; 10 public String content; 11 12 public Msg(int iconId, String title, String content) { 13 this.iconId = iconId; 14 this.title = title; 15 this.content = content; 16 } 17 18 public int getIconId() { 19 return iconId; 20 } 21 22 public void setIconId(int iconId) { 23 this.iconId = iconId; 24 } 25 26 public String getTitle() { 27 return title; 28 } 29 30 public void setTitle(String title) { 31 this.title = title; 32 } 33 34 public String getContent() { 35 return content; 36 } 37 38 public void setContent(String content) { 39 this.content = content; 40 } 41 }
Maincity代码:
1 package activity.cyq.listviewlearn; 2 3 import android.app.AlertDialog; 4 import android.content.DialogInterface; 5 import android.support.v7.app.AppCompatActivity; 6 import android.os.Bundle; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup; 10 import android.widget.AdapterView; 11 import android.widget.ArrayAdapter; 12 import android.widget.BaseAdapter; 13 import android.widget.ImageView; 14 import android.widget.LinearLayout; 15 import android.widget.ListView; 16 import android.widget.TextView; 17 import android.widget.Toast; 18 import java.util.ArrayList; 19 20 public class MainActivity extends AppCompatActivity { 21 22 private ListView leftView; 23 private ListView rightView; 24 private BaseAdapter adapter_image; /*左边含image的列表适配器*/ 25 private ArrayAdapter adapter_Text; /*右边含text的列表适配器*/ 26 private ArrayList<Msg> list_image = new ArrayList<>(); 27 private ArrayList<String> list_Text = new ArrayList<>(); 28 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.activity_main); 33 /**/ 34 leftView = (ListView) findViewById(R.id.listview_main); 35 rightView = (ListView) findViewById(R.id.listText_item); 36 37 /*为list_image添加数据*/ 38 list_image.add(new Msg(R.drawable.image1, "title 1", "context 1")); 39 list_image.add(new Msg(R.drawable.image2, "title 2", "context 2")); 40 list_image.add(new Msg(R.drawable.image3, "title 3", "context 3")); 41 list_image.add(new Msg(R.drawable.image4, "title 4", "context 4")); 42 /*为list_Text添加数据*/ 43 for (int i = 0; i < 12; i++) { 44 list_Text.add("这是ArrayAdapter适配器生成的第" + (i + 1) + "条数据"); 45 } 46 47 /*获得左侧列表的适配器 adapter_image 匿名类实例化 并实现四个方法 */ 48 adapter_image = new BaseAdapter() { 49 /*返回数据条数*/ 50 @Override 51 public int getCount() { 52 return list_image.size(); 53 } 54 55 /*返回每一项数据*/ 56 @Override 57 public Msg getItem(int position) { 58 return list_image.get(position); 59 } 60 61 /*返回该项数据的索引指*/ 62 @Override 63 public long getItemId(int position) { 64 return position; 65 } 66 67 /*构造列表项的视图,并返回*/ 68 @Override 69 public View getView(final int position, View convertView, ViewGroup parent) { 70 /* convertView 是回收的item视图 回收视图的再利用可减少每次在内存中新建一个视图 提高程序性能 */ 71 LinearLayout ll = null; 72 if (convertView == null) { 73 /*获得item视图*/ 74 ll = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.imagelistview, null); 75 } 76 if (convertView != null) { 77 /*使用回收视图 */ 78 ll = (LinearLayout) convertView; 79 } 80 81 ImageView icon = (ImageView) ll.findViewById(R.id.iamge_item);/*注意ll这个View容器下获findViewById(R.id.**)*/ 82 TextView title_ = (TextView) ll.findViewById(R.id.title_item); 83 TextView content_ = (TextView) ll.findViewById(R.id.content_item); 84 85 icon.setImageResource(getItem(position).iconId); /*调用上面的geeItem方法 获得Msg对象 */ 86 title_.setText(getItem(position).title); 87 content_.setText(getItem(position).content); 88 89 /*item的点击事件*/ 90 ll.setOnClickListener(new View.OnClickListener() { 91 @Override 92 public void onClick(View v) { 93 Toast.makeText(MainActivity.this, "你点击了第" + (position + 1) + "个列表项。", Toast.LENGTH_SHORT).show(); 94 95 } 96 }); 97 /*item子元素的点击事件*/ 98 icon.setOnClickListener(new View.OnClickListener() { 99 @Override 100 public void onClick(View v) { 101 Toast.makeText(MainActivity.this, "你点击了第" + (position + 1) + "张图片。", Toast.LENGTH_SHORT).show(); 102 } 103 }); 104 105 /*item长按事件 实现删除该item*/ 106 ll.setOnLongClickListener(new View.OnLongClickListener() { 107 @Override 108 public boolean onLongClick(View v) { 109 new AlertDialog.Builder(MainActivity.this) 110 .setTitle("警告!") 111 .setMessage("确认删除该项?") 112 .setIcon(R.drawable.delete) 113 .setPositiveButton("是", new DialogInterface.OnClickListener() { 114 @Override 115 public void onClick(DialogInterface dialog, int which) { 116 /*如何删除该项数据 ???*/ 117 list_image.remove(position); 118 leftView.setAdapter(adapter_image); 119 } 120 }) 121 .setNegativeButton("否", null) 122 .show(); 123 return true;/* true:接受系统自定义的反馈,比如震动,音效等 false:无反馈 */ 124 } 125 }); 126 return ll; 127 } 128 }; 129 130 /*右侧的ArrayAdapter适配器 通过匿名类实现。也可以通过子类继承ArrayAdapter */ 131 //adapter_Text = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, list_Text);/*这列代码,使用的是android系统自带的资源*/ 132 adapter_Text = new ArrayAdapter(MainActivity.this, R.layout.textlistveiw, list_Text); /* 使用自定义的资源文件 该文件标签只能<TextView/> */ 133 /*item的点击事件*/ 134 rightView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 135 @Override 136 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 137 Toast.makeText(MainActivity.this, "你点击了第" + (position + 1) + "个Item项", Toast.LENGTH_SHORT).show(); 138 } 139 }); 140 /*iitem长按删除改项的事件*/ 141 rightView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 142 @Override 143 public boolean onItemLongClick(AdapterView<?> parent, final View view, final int position, long id) { 144 new AlertDialog.Builder(MainActivity.this) 145 .setTitle("警告") 146 .setMessage("是否删除该项?") 147 .setIcon(R.drawable.delete) 148 .setPositiveButton("是", new DialogInterface.OnClickListener() { 149 @Override 150 public void onClick(DialogInterface dialog, int which) { 151 /* adapter_Text.remove(position); 不能这样使用 152 查看谷歌的Guide API得知 ArrayAdapter.remove(object) 153 这个方法的作用的删除数组中指定的对象 显然我们删除索引是行不通的 154 所以我们需要删除ArrayAdapter中的object对象,也就是list_Text[position] 155 adapter_Text.remove(list_Text.get(position)); 156 rightView.setAdapter(adapter_Text); 157 */ 158 list_Text.remove(position); 159 rightView.setAdapter(adapter_Text); 160 } 161 }) 162 .setNegativeButton("否", null) 163 .show(); 164 return true; 165 } 166 }); 167 168 /*为试图添加适配器*/ 169 leftView.setAdapter(adapter_image); 170 rightView.setAdapter(adapter_Text); 171 } 172 173 }
时间: 2024-10-14 00:54:02