继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

☆ListView的封装支持多种条目

张风捷特烈
关注TA
已关注
手记 72
粉丝 21
获赞 57

零、前言

[1.]封装了一晚,总算把多条目的ListView封装了一下
[2.]有一点不足:不能复用convertView,否则滑动时条目会复用错乱,有大神知道怎么改,请指点一下

listview.gif

一、使用

1.初始化数据
        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));
            }
        }
2.使用:
//类型和布局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;
        }
    }
});
3.必须有个实体类,且继承ItemBean类:保证实体类有type属性
/**
 * 作者:张风捷特烈<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;
    }
}

二、具体封装类

1.Item类:保证实体类有type属性
/**
 * 作者:张风捷特烈<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;
    }
}
2.适配器类
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, position, type);
        setData(holder, getItem(position), position, type);

        return holder.getConvertView(type);
    }

    /**
     * 设置数据抽象方法
     *
     * @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();
    }
}
3.Holder类
public class MyLVsHolder<T> {

    /**
     * 条目内部控件的view集合
     */
    private SparseArray<View> mViews;
    /**
     * 位置
     */
    private int mPosition;
    private int mType;
    /**
     * 条目视图
     */
    private View mItemView;
    private SparseArray<View> mItemViews;
    private List<Integer> mPos;

    /**
     * @param ctx      上下文
     * @param typeMap  布局ID
     * @param position 位置
     * @param type 类型
     */
    public MyLVsHolder(Context ctx, View convertView, Map<Integer, Integer> typeMap, int position, int type) {
        mPosition = position;
        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);
            }
        }
    }

    public List<Integer> handleCheckBox(int checkBoxId) {
        mPos = new ArrayList<>();
        final CheckBox cb = this.getView(checkBoxId);
        cb.setChecked(false);
        if (mPos.contains(this.getPosition())) {
            cb.setChecked(true);
        }
        cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cb.isChecked()) {
                    mPos.add(getPosition());
                } else {
                    mPos.remove((Integer) getPosition());
                }
            }
        });
        return mPos;//返回选中的CheckBox位置集合
    }

    /**
     * 通过viewId获取控件
     *
     * @param viewId 条目内部控件的id
     * @param <T>    数据泛型
     * @return view
     */
    public <T extends View> T 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 (T) view;
    }

    public View getConvertView(int type) {
        L.d(type + L.l());
        return mItemView;
    }

    public int getPosition() {
        return mPosition;
    }

    /**
     * 设置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
你的喜欢与支持将是我最大的动力

附录、布局
1.layout/list_item_center.xml
<?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>
2.layout/list_item_left.xml
<?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>
3.layout/list_item_right.xml
<?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>
4.layout/activity_main.xml
<?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>
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP