【问题标题】:Android ListView select items to delete with 3 column layoutAndroid ListView 使用 3 列布局选择要删除的项目
【发布时间】:2012-05-12 22:40:36
【问题描述】:

我想在 android 中实现一个Listview,我可以在其中启用删除模式,用户可以在其中选择要删除的条目。它应该类似于android中的消息应用程序。

我已经有一个ListActivity,它的左侧有一个图标,右侧有一个文本。我现在想在列表条目的右侧添加一个浮动的CheckBoxListActivity 被我的一个朋友列在另一个问题中:android listactivity background color

布局应该是:

  • 图片
  • 居中 列表项
  • 用于删除选择的复选框

我怎样才能做到这一点?我可以使用框架中的标准ListView 项吗?

【问题讨论】:

  • 我不明白你的问题出在哪里。您不知道如何设置删除机制或问题在于您的行布局构建?
  • 问题是,我无法正确设置布局 (xml)。我希望中间部分(消息标题和日期)具有动态宽度。左侧是带有 48 dip 的图标,右侧是固定的复选框。每次我将复选框添加到 xml 时,它都不会显示。

标签: android checkbox android-listview


【解决方案1】:

我猜您希望CheckBox 出现(仅)何时从ListView 中删除项目。假设您使用另一个问题的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:background="@color/darkbluelogo" >

    <ImageView android:id="@+id/list_image"
        android:layout_width="48dip"
        android:layout_height="48dip"
        android:contentDescription="@id/list_image"
         />
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="fill_parent"
        android:padding="5dp" 
        android:background="@color/darkbluelogo"
        android:scrollingCache="false" 
        android:cacheColorHint="#00000000" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@+id/title" >
        </TextView>

        <TextView
            android:id="@+id/datetime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@+id/datetime" >
        </TextView>

    </LinearLayout>
       <CheckBox
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:visibility="gone" />
</LinearLayout>

ListView 启动时,CheckBox 将不存在,TextViews 的内容将占据所有空间。在适配器的getView 方法中添加一个标志,表示必须显示CheckBox(此处您将设置CheckBox 在布局中的可见性为可见)。当需要删除项目时,修改标志,然后调用notifyDataSetChanged(),以便ListView 重绘其子项,这次CheckBox 存在。

注意: 您必须自己存储CheckBoxes 的状态。

【讨论】:

  • 非常感谢,这似乎可以完成这项工作。这正是我想要的。
  • @KeepAlive 在getView 方法中实现代码时,请记住ListView 将在您上下滚动时回收视图。
【解决方案2】:

首先,您需要为列表条目自定义布局。一个简单的包含 ImageView 、 TextView 和 CheckBox 的 RelativeLayout 就足够了。

然后您可能想要构建自己的自定义适配器,该适配器可以扩展 BaseAdapter(或 SimpleAdapter 或 CursorAdapter 或 ArrayAdapter 或...)。适配器会将列表的数据绑定到您的自定义布局。例如,如果您的数据包含在游标中,它将如下所示:

private class MyCustomAdapter extends CursorAdapter {

        public MyCustomAdapter(Context context) {
            super(context, null);
        }


        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            //Return a list item view
            return getLayoutInflater().inflate(R.layout.my_custom_list_item_layout, parent, false);

        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            //Get views from layout
            final ImageView imageView = (ImageView) view.findViewById(R.id.list_item_image);
            final TextView textView = (TextView) view.findViewById(R.id.list_item_text);
            final CheckBox checkBox = (CheckBox) view.findViewById(R.id.list_item_checkbox);


            //Get data from cursor
            final String text = cursor.getString(...); 

            //Add listener to the checkbox
            checkBox.setOnClickListener(new OnClickListener() {...});

            //Bind data
            textView.setText(text);
        }
    }

【讨论】:

    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2011-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    相关资源
    最近更新 更多