字符串数组 ArrayAdapter
1 |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,new String[]{"111","222","333"}); |
List<Map<String, Object>>数组 SimpleAdapter
1 |
SimpleAdapter myAdapter = new SimpleAdapter(this, listitem, R.layout.list_item, new String[]{"icon", "name", "desc"}, new int[]{R.id.icon, R.id.name, R.id.desc}); |
子布局 list_item.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?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:orientation="horizontal"> <!-- 定义一个用于显示头像的 ImageView --> <ImageView android:id="@+id/icon" android:layout_width="42dp" android:layout_height="45dp" android:baselineAlignBottom="true" android:paddingLeft="8dp" /> <!-- 定义一个竖直方向的 LinearLayout,把 语言 与 简介 的文本框设置出来 --> <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8dp" android:textColor="#1D1D1C" android:textSize="20sp" /> <TextView android:id="@+id/desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8px" android:textColor="#B4B4B9" android:textSize="14sp" /> </LinearLayout> </LinearLayout> |
空数据时候的视图 listview_empty.xml
1 2 3 4 5 6 7 8 9 10 |
<?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:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="没有数据可以显示"/> </LinearLayout> |
布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<ListView android:id="@+id/listview2" android:layout_width="match_parent" android:footerDividersEnabled="true" android:cacheColorHint="#00000000" android:dividerHeight="2dp" android:layout_height="100dp" /> <include android:id="@+id/listview3Empty" layout="@layout/listview_empty"/> <ListView android:id="@+id/listview3" android:layout_width="match_parent" android:footerDividersEnabled="true" android:cacheColorHint="#00000000" android:dividerHeight="2dp" android:layout_height="200dp" /> |
SimpleAdapter
数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
private String[] langs = new String[]{ "Kotlin", "Scala", "Swift", "TypeScript" }; private String[] descs = new String[]{ "Kotlin 是运行在 Java 虚拟机上的静态语言,被称之为 Android 世界的 Swift", "Scala 是一门多范式(multi-paradigm)的编程语言", "Swift 是开发 Mac APP 和 iOS APP 的语言", "TypeScript 是一种由微软开发的自由和开源的编程语言" }; private int[] icons = new int[]{ R.drawable.kotlin, R.drawable.scala, R.drawable.swift, R.drawable.typescript }; |
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@BindView(R.id.listview2) ListView listview2; ...... List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>(); for (int i = 0; i < langs.length; i++) { Map<String, Object> showitem = new HashMap<String, Object>(); showitem.put("icon", icons[i]); showitem.put("name", langs[i]); showitem.put("desc", descs[i]); listitem.add(showitem); } //创建一个simpleAdapter SimpleAdapter myAdapter = new SimpleAdapter(this, listitem, R.layout.list_item, new String[]{"icon", "name", "desc"}, new int[]{R.id.icon, R.id.name, R.id.desc}); listview2.setAdapter(myAdapter); |
自定义Adapter适配器
数据对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.example.test.myapplication.domain; public class YetLanguage { private String aName; private String aDesc; private int aIcon; private YetLanguage() { } public YetLanguage(String aName, String aDesc, int aIcon) { this.aName = aName; this.aDesc = aDesc; this.aIcon = aIcon; } ..... }继承 |
适配器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
package com.example.test.myapplication.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.example.test.myapplication.R; import com.example.test.myapplication.domain.YetLanguage; import java.util.LinkedList; public class YetAdapter extends BaseAdapter { private LinkedList<YetLanguage> mData; private Context mContext; private YetAdapter() { } public YetAdapter(LinkedList<YetLanguage> mData, Context mContext) { this.mData = mData; this.mContext = mContext; } public void add(YetLanguage language){ this.mData.add(language); //必须通知更新,否则无效 notifyDataSetChanged(); } public void add(int idx, YetLanguage language){ this.mData.add(idx, language); //必须通知更新,否则无效 notifyDataSetChanged(); } public YetLanguage remove(int idx){ YetLanguage language = this.mData.remove(idx); notifyDataSetChanged(); //必须通知更新,否则无效 return language; } public void clear() { mData.clear(); //必须通知更新,否则无效 notifyDataSetChanged(); } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //避免重复载入 ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false); holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.icon); holder.name = (TextView) convertView.findViewById(R.id.name); holder.desc = (TextView) convertView.findViewById(R.id.desc); convertView.setTag(holder); //将Holder存储到convertView中 } else { holder = (ViewHolder) convertView.getTag(); } //设定子布局信息 holder.icon.setBackgroundResource(mData.get(position).getaIcon()); holder.name.setText(mData.get(position).getaName()); holder.desc.setText(mData.get(position).getaDesc()); return convertView; } /** * ViewHolder 重用组件 */ static class ViewHolder { ImageView icon; TextView name; TextView desc; } } |
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@BindView(R.id.listview3) ListView listview3; @BindView(R.id.listview3Empty) View listview3Empty; YetAdapter yetAdapter = null; ...... List<YetLanguage> mData = new LinkedList<YetLanguage>(); //初始化 mData.add(new YetLanguage("Kotlin", "Kotlin 是运行在 Java 虚拟机上的静态语言,被称之为 Android 世界的 Swift", R.drawable.kotlin)); //mData.add(new YetLanguage("Scala", "Scala 是一门多范式(multi-paradigm)的编程语言", R.drawable.scala)); //mData.add(new YetLanguage("Swift", "Swift 是开发 Mac APP 和 iOS APP 的语言", R.drawable.swift)); //mData.add(new YetLanguage("TypeScript", "TypeScript 是一种由微软开发的自由和开源的编程语言", R.drawable.typescript)); //创建一个 YetAdapter yetAdapter = new YetAdapter((LinkedList<YetLanguage>) mData,getApplicationContext()); listview3.setEmptyView(listview3Empty); listview3.setAdapter(yetAdapter); //使用适配器的方式来动态增加 yetAdapter.add(new YetLanguage("Swift", "Swift 是开发 Mac APP 和 iOS APP 的语言", R.drawable.swift)); |
ListView列表中获取EditText输入的值 https://blog.csdn.net/DickyQie/article/details/78036462
Android 可复用 BaseAdapter https://www.twle.cn/l/yufei/android/android-basic-customer-baseadapter.html
一个通用的适配器 用法参考: https://www.twle.cn/l/yufei/android/android-basic-gridview.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
package com.pandy.app.adapter; import android.content.Context; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.Switch; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public abstract class AbsCommAdapter<T> extends BaseAdapter { private List<T> mData; private int mLayoutRes; private AbsCommAdapter() { } /** * * @param mData 数据。 * @param mLayoutRes 单个对象所映射的子布局,例如 list_item.xml。 */ public AbsCommAdapter(List<T> mData, int mLayoutRes) { this.mData = mData; this.mLayoutRes = mLayoutRes; } /** * 设定每一行元素对应的方法 * @param holder * @param obj */ public abstract void bindView(ViewHolder holder, T obj); @Override public int getCount() { return mData != null ? mData.size() : 0; } @Override public T 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 = ViewHolder.bind(parent.getContext(), convertView, parent, mLayoutRes , position); bindView(holder, getItem(position)); return holder.getConvertViewView(); } //添加一个元素 public void add(T data) { if (mData == null) { mData = new ArrayList<>(); } mData.add(data); notifyDataSetChanged(); } //往特定位置,添加一个元素 public void add(int position, T data) { if (mData == null) { mData = new ArrayList<>(); } mData.add(position, data); notifyDataSetChanged(); } public void addAll(List<T> data) { if (mData == null) { mData = new ArrayList<>(); } mData.addAll(data); notifyDataSetChanged(); } public void remove(T data) { if (mData != null) { mData.remove(data); } notifyDataSetChanged(); } public void remove(int position) { if (mData != null) { mData.remove(position); } notifyDataSetChanged(); } public void removeAll() { if (mData != null) { mData.clear(); } notifyDataSetChanged(); } public void clear() { if (mData != null) { mData.clear(); } notifyDataSetChanged(); } /** * 内部数据处理 */ public static class ViewHolder { private SparseArray<View> mViews; //存储ListView 的 item中的View private View convertView; //存放convertView private int position; //游标 private Context context; //Context上下文 //构造方法,完成相关初始化 private ViewHolder(Context context, ViewGroup parent, int layoutRes) { mViews = new SparseArray<>(); this.context = context; convertView = LayoutInflater.from(context).inflate(layoutRes, parent, false); convertView.setTag(this); //convertView = convertView; } //绑定ViewHolder与convertView public static ViewHolder bind(Context context, View convertView, ViewGroup parent, int layoutRes, int position) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(context, parent, layoutRes); } else { holder = (ViewHolder) convertView.getTag(); holder.convertView = convertView; } holder.position = position; return holder; } /** * 获得空间 * * @param id * @param <T> * @return */ @SuppressWarnings("unchecked") private <T extends View> T getView(int id) { T t = (T) mViews.get(id); if (t == null) { t = (T) convertView.findViewById(id); mViews.put(id, t); } return t; } /** * 获取当前条目 */ public View getConvertViewView() { return convertView; } /** * 获取条目位置 */ public int getItemPosition() { return getPosition(); } /** * 获取条目位置 */ public int getPosition() { return position; } /** * 设置标签 */ private ViewHolder setTag(int id, Object obj) { getView(id).setTag(obj); return this; } /** * 设置点击监听 */ public ViewHolder setOnClickListener(int id, View.OnClickListener listener) { getView(id).setOnClickListener(listener); return this; } /** * 设置可见 */ public ViewHolder setVisibility(int id, int visible) { getView(id).setVisibility(visible); return this; } /** * 设置文字 */ public ViewHolder setText(int id, CharSequence text) { TextView view = getView(id); if (view instanceof TextView) { ((TextView) view).setText(text); } return this; } /** * 读取文字 */ public CharSequence getText(int id) { View view = getView(id); if (view instanceof TextView) { return ((TextView) view).getText(); } return null; } /** * 设置图片 */ public ViewHolder setImageResource(int id, int drawableRes) { View view = getView(id); if (view instanceof ImageView) { ((ImageView) view).setImageResource(drawableRes); } else { view.setBackgroundResource(drawableRes); } return this; } /** * 设置选中 */ public ViewHolder setSwitchCheck(int id, boolean checked) { Switch view = getView(id); if (view instanceof Switch) { ((Switch) view).setChecked(checked); } return this; } /** * 设置选中 */ public Switch getSwitch(int id) { Switch view = getView(id); return view; } //其他方法可自行扩展 } } |