1.封装了一晚,总算把多条目的ListView封装了一下
ArrayList<Message> messages = new ArrayList<>();
messages.add(new Message("你瞅啥?!", "巫缨:", 0));
messages.add(new Message("瞅你怎滴!!", "捷特:", 1));
messages.add(new Message("2018-8-13", "9:30", 2));
messages.add(new Message("你再瞅试试!", "巫缨:", 0));
for (int i = 0; i < 100; i++) {
if (i % 3 == 0) {
messages.add(new Message("我试了怎滴!!--1", "捷特:", 1));
} else if (i % 3 == 1) {
messages.add(new Message("2018-8-13", "9:30", 2));
} else {
messages.add(new Message("你再瞅试试!--0", "巫缨:", 0));
}
}
//类型和布局id放在HashMap中
HashMap<Integer, Integer> typeMap = new HashMap<>();
typeMap.put(0, R.layout.list_item_left);
typeMap.put(1, R.layout.list_item_right);
typeMap.put(2, R.layout.list_item_center);
mLv.setAdapter(new MyLVsAdapter<Message>(this, messages, typeMap) {
@Override
public void setData(MyLVsHolder holder, Message data, int position, int type) {
switch (type) {
case 0:
holder.setText(R.id.id_tv_right, data.getValue());
holder.setText(R.id.id_tv_name, data.getName());
break;
case 1:
holder.setText(R.id.id_tv_left, data.getValue());
holder.setText(R.id.id_tv_name, data.getName());
break;
case 2:
holder.setText(R.id.id_tv_center, data.getValue());
holder.setText(R.id.id_tv_name, data.getName());
break;
}
}
});
/**
* 作者:张风捷特烈<br/>
* 时间:2018/8/27 0027:18:37<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:信息实体类
*/
public class Message extends ItemBean {
public Message(String value, String name, int type) {
super(type);
this.value = value;
this.name = name;
}
private String value;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
/**
* 作者:张风捷特烈<br/>
* 时间:2018/8/27 0027:19:30<br/>
* 邮箱:1981462002@qq.com<br/>
* 说明:多类型ListView实体类父类
*/
public class ItemBean {
private int type;
public ItemBean(int type) {
this.type = type;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
/**
* 作者:张风捷特烈
* 时间:2018/4/6:13:51
* 邮箱:1981462002@qq.com
* 说明:ListView适配器
*/
public abstract class MyLVsAdapter<T> extends BaseAdapter {
/**
* 数据
*/
protected List<T> mDatas;
/**
* 布局映射
*/
protected Map<Integer, Integer> mTypeMap;
/**
* 上下文
*/
protected Context mCtx;
public MyLVsAdapter(Context ctx, List<T> datas, Map<Integer, Integer> typeMap) {
mCtx = ctx;
mDatas = datas;
mTypeMap = typeMap;
}
@Override
public int getCount() {
return mDatas.size();
}
@Override
public T getItem(int position) {
return mDatas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//创建 MyLVHolder 对象
T t = mDatas.get(position);
int type = ((ItemBean) t).getType();
MyLVsHolder holder = new MyLVsHolder(mCtx, convertView, mTypeMap, type);
setData(holder, getItem(position), position, type);
return holder.getConvertView();
}
/**
* 设置数据抽象方法
*
* @param holder MyLVHolder
* @param data 数据
* @param position 位置
* @param type
*/
public abstract void setData(MyLVsHolder holder, T data, int position, int type);
/**
* 根据数据源的position返回需要显示的的layout的type
*
* @param position 位置
* @return
*/
@Override
public int getItemViewType(int position) {
T t = mDatas.get(position);
((ItemBean) t).getType();
return ((ItemBean) t).getType();
}
@Override
public int getViewTypeCount() {
return mTypeMap.size();
}
}
/**
* 作者:张风捷特烈
* 时间:2018/4/6:11:30
* 邮箱:1981462002@qq.com
* 说明:MyLVHolder
*/
public class MyLVsHolder<T> {
/**
* 条目内部控件的view集合
*/
private SparseArray<View> mViews;
private int mType;
/**
* 条目视图
*/
private View mItemView;
private SparseArray<View> mItemViews;
/**
* @param ctx 上下文
* @param typeMap 布局ID
* @param type 类型
*/
public MyLVsHolder(Context ctx, View convertView, Map<Integer, Integer> typeMap, int type) {
mType = type;
mViews = new SparseArray<>();
mItemViews = new SparseArray<>();
//生成条目的View
for (int i = 0; i < typeMap.size(); i++) {
if (type == i) {
if (convertView == null) {
mItemView = LayoutInflater.from(ctx).inflate(typeMap.get(i), null);
} else {
mItemView = convertView;
//用MyLVHolder为条目View设置标签
mItemView.setTag(this);
}
mItemViews.put(i, mItemView);
return;
}
}
}
/**
* 通过viewId获取控件
*
* @param viewId 条目内部控件的id
* @param <W> 数据泛型
* @return view
*/
public <W extends View> W getView(int viewId) {
//通过viewId为键获取View值
View view = mViews.get(viewId);
//如果view为空
if (view == null) {
//条目findViewById获取View
view = mItemViews.get(mType).findViewById(viewId);
//以id为键,View为值存入mViews集合
mViews.put(viewId, view);
}
return (W) view;
}
public View getConvertView() {
return mItemView;
}
/**
* 设置TextView文本方法
*
* @param viewId 条目内部控件的id
* @param text 文本
* @return MyLVHolder对象
*/
public MyLVsHolder setText(int viewId, String text) {
TextView view = getView(viewId);
if (view != null) {
view.setText(text);
}
return this;
}
/**
* 通过id设置图片
*
* @param viewId 条目内部控件的id
* @param resId 资源id
* @return MyLVHolder对象
*/
public MyLVsHolder setImageViewRes(int viewId, int resId) {
ImageView view = getView(viewId);
view.setImageResource(resId);
return this;
}
/**
* 通过id设置图片
*
* @param viewId 条目内部控件的id
* @param bitmap 图片
* @return MyLVHolder对象
*/
public MyLVsHolder setImageViewBitmap(int viewId, Bitmap bitmap) {
ImageView view = getView(viewId);
view.setImageBitmap(bitmap);
return this;
}
}
本文由张风捷特烈原创,转载请注明
更多安卓技术欢迎访问:https://www.jianshu.com/c/004f3fe34c94
张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com
你的喜欢与支持将是我最大的动力
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="176dp"
android:padding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/head_center"/>
<TextView
android:id="@+id/id_tv_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="@android:color/holo_orange_light"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="@+id/id_tv_name"
app:layout_constraintEnd_toStartOf="@+id/iv_icon"
app:layout_constraintTop_toTopOf="@+id/id_tv_name"
app:layout_constraintVertical_bias="1.0"/>
<TextView
android:id="@+id/id_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="?android:attr/colorActivatedHighlight"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="@+id/iv_icon"
app:layout_constraintStart_toEndOf="@+id/iv_icon"
app:layout_constraintTop_toTopOf="@+id/iv_icon"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/id_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="?attr/colorControlActivated"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/iv_icon"
app:layout_constraintStart_toEndOf="@+id/iv_icon"
app:layout_constraintTop_toTopOf="@+id/iv_icon"
app:layout_constraintVertical_bias="0.555"/>
<ImageView
android:id="@+id/iv_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/head_left"
tools:layout_editor_absoluteX="26dp"/>
<TextView
android:id="@+id/id_tv_right"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/iv_icon"
app:layout_constraintStart_toEndOf="@+id/id_tv_name"
app:layout_constraintTop_toTopOf="@+id/iv_icon"/>
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="16dp"
android:padding="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/test"/>
<TextView
android:id="@+id/id_tv_left"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@+id/id_tv_name"
app:layout_constraintEnd_toStartOf="@+id/id_tv_name"
app:layout_constraintTop_toTopOf="@+id/id_tv_name"
app:layout_constraintVertical_bias="1.0"/>
<TextView
android:id="@+id/id_tv_name"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="#283be8"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/iv_icon"
app:layout_constraintEnd_toStartOf="@+id/iv_icon"
app:layout_constraintTop_toTopOf="@+id/iv_icon"/>
</android.support.constraint.ConstraintLayout>
<?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="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true">
</ListView>
</LinearLayout>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。