You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android ListView中添加收藏按钮(CheckBox/ToggleButton/ImageView实现)

ListView条目添加收藏功能实现方案

1. 调整条目布局(row.xml)

将原垂直排列的LinearLayout改为水平排列,在文本右侧添加收藏按钮,这里使用CheckBox实现,你可以后续自定义selector样式替换成收藏/未收藏的图标效果:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1"
    android:gravity="center_vertical"
    android:background="@drawable/row"
    android:paddingHorizontal="16dp">

    <TextView
        android:id="@+id/txtRecipeName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/text"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_weight="0.9"
        android:typeface="normal"
        android:background="@drawable/button2"/>
    
    <!-- 新增收藏按钮 -->
    <CheckBox
        android:id="@+id/cb_favorite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_star" <!-- 可替换为自定义收藏图标selector -->
        />

</LinearLayout>

2. 修改ListAdapter代码

新增收藏状态存储集合,修改ViewHolder和条目绑定逻辑:

static class ListAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private Context ctx;
    // 新增:存储每个条目的收藏状态,初始化全为未收藏
    private boolean[] isFavorited = new boolean[RecipeName.length];

    public ListAdapter(Context context) {
        inflater = LayoutInflater.from(context);
        ctx = context;
    }

    public int getCount() {
        return RecipeName.length;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    // 新增:外部获取指定位置收藏状态的方法
    public boolean getFavoritedState(int position) {
        return isFavorited[position];
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if(convertView == null){
            convertView = inflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
            holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);
            // 新增:绑定收藏按钮控件
            holder.cbFavorite = (CheckBox) convertView.findViewById(R.id.cb_favorite);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtRecipeName.setText(RecipeName[position]);
        // 新增:设置当前条目的收藏状态
        holder.cbFavorite.setChecked(isFavorited[position]);
        // 新增:收藏按钮点击事件
        holder.cbFavorite.setOnClickListener(v -> {
            isFavorited[position] = holder.cbFavorite.isChecked();
            // 这里可以添加点击后的业务逻辑,比如提示收藏成功/取消、同步到本地存储等
        });

        return convertView;
    }

    static class ViewHolder {
        TextView txtRecipeName;
        // 新增:收藏按钮控件声明
        CheckBox cbFavorite;
    }
}

可选优化:持久化存储收藏状态

如果需要退出应用后收藏状态不丢失,可以将isFavorited数组的内容存储到SharedPreferences、Room数据库等本地存储组件中,每次初始化Adapter时读取本地存储的状态更新数组即可。

内容的提问来源于stack exchange,提问作者simo spotify

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 00:54:09