【问题标题】:CardView selector?CardView 选择器?
【发布时间】:2021-02-24 22:32:39
【问题描述】:

我正在创建一个笔记应用程序,我正在使用上下文操作栏来删除笔记。我的笔记显示在卡片视图中。我的操作栏工作到可以选择笔记并删除它们的地步。但是当我选择笔记时,我想尽可能使用复选标记作为选择器(否则任何选择器),但我未能成功实现。

上下文操作栏

mListViewNotes = (ListView) findViewById(R.id.listview_notes);
    mListViewNotes.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    mListViewNotes.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        CardView cardView = (CardView) findViewById(R.id.cardView);

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
            if (list_items.contains(notes.get(position))) {
                count = count-1;
                list_items.remove(notes.get(position));
                mode.setTitle(count + " Notes Selected");
            } else {
                count = count+1;
                list_items.add(notes.get(position));
                mode.setTitle(count + " Notes Selected");
            }

            if (count == 0) {
                mode.setTitle("No Notes Selected");
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            app_bar.setVisibility(View.GONE);
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            actionMode = mode;
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            deleteNote();
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            count = 0;
            app_bar.setVisibility(View.VISIBLE);
            list_items.clear();
        }
    });
}

item_note.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/light_sand"
android:orientation="vertical"
android:id="@+id/item_note">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:layout_marginBottom="6dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="6dp"
    card_view:cardCornerRadius="4dp"
    card_view:cardElevation="6dp">

    <ImageView
        android:layout_width="1000dp"
        android:layout_height="150dp"
        android:layout_gravity="start"
        android:layout_marginLeft="-80dp"
        android:layout_marginTop="-12dp"
        android:background="@drawable/texture_tile_old"
        android:scaleType="fitStart" />

    <ImageView
        android:id="@+id/clef_note"
        android:layout_width="70dp"
        android:layout_height="120dp"
        android:layout_gravity="start"
        android:layout_marginTop="10dp"
        android:background="@drawable/treble_clef" />

    <View
        android:layout_width="780dp"
        android:layout_height="1dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="43dp"
        android:background="@color/black" />

    <View
        android:layout_width="780dp"
        android:layout_height="1dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="70dp"
        android:background="@color/black" />

    <View
        android:layout_width="780dp"
        android:layout_height="1dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="97dp"
        android:background="@color/black" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:scaleType="center">

        <TextView
            android:id="@+id/list_note_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="65dp"
            android:layout_marginTop="8dp"
            android:textColor="@color/black"
            android:textSize="28sp" />

        <TextView
            android:id="@+id/list_note_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="46dp"
            android:layout_marginLeft="65dp"
            android:layout_marginTop="0dp"
            android:maxLines="2"
            android:textColor="@color/gunmetal"
            android:textSize="20sp"
            android:lineSpacingExtra="4dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/brown"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="4dp"
            android:text="Last Updated:"
            android:textColor="@color/white" />

        <TextView
            android:id="@+id/list_note_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="4dp"
            android:gravity="end"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/white" />
    </LinearLayout>
</android.support.v7.widget.CardView>

【问题讨论】:

  • 你的意思是CheckBox?
  • 如果这是唯一的方法,那么是的,atm 没有选择器,所以除了计数之外没有办法判断它是否被选中

标签: java android xml contextual-action-bar


【解决方案1】:

您可以将CheckBox 添加到您的项目布局,和/或更改布局本身的背景以显示选定状态。

使用第一个选项,您只需将isChecked 设置为true 即可将布局更改为选定状态。

checkBox.setChecked(true);

要更改布局的背景,您只需将背景设置为新的颜色资源即可。

itemLayout.setBackgroundResource(R.color.color_selected);

或者您可以使用ColorStateList 进行一些更高级的状态更改(例如将波纹效果变为纯色)。

ripple_selector.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/color_highlight">
    <item android:drawable="@drawable/color_highlight"/>
</ripple>

item_selector_background.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"
            android:drawable="@color/color_selected"/>
    <item android:drawable="@drawable/ripple_effect"/>
</selector>

要使用它,您可以将布局 xml 中的背景属性设置为 item_selector_background,然后在代码中更改选定状态。

itemLayout.setSelected(true);

【讨论】:

  • 哦,好吧,所以我已经尝试了这两种方法,但没有运气:/我不确定我做错了什么......当我添加复选框时,我什至无法选择列表项。当我在我的 onCreate 中设置复选框的可见性时,应用程序在启动时崩溃..:/ 至于背景,每当我尝试使用 setBackgroundResource(); 更改我的卡片视图的背景图像时;我也遇到了崩溃..还有 setSelected(true); ..碰撞。似乎没有任何效果:/这是使用cardView的问题吗?还是我做错了什么?原谅我的无知,感谢您的所有帮助! :D
  • @ShadowSuave 您遇到的这些崩溃是什么?我猜NullPointerException
  • 我不确定在哪里可以检查崩溃中的错误。再次原谅我的无知,我对 android 很陌生,但我学到了很多东西。不确定它是否值得一提,但正如我昨天所说,当尝试通过我的主要活动更改我的 Item_note 布局文件中的任何内容时,我遇到了崩溃,但是在 noteAdapter 中更改它时似乎可以工作。虽然我仍然不确定从那里去哪里:/
  • @ShadowSuave 你应该一定了解Logcat窗格;它应该在 Android Studio 窗口的左下角可用。它将被标记为6: Logcat
  • @ShadowSuave 最后,您提到您对 Android 开发还很陌生;这就是为什么我建议您通过Developer TrainingAndroid Studio User Guide 参观。两者都是了解 Android 开发的重要资源,应该可以回答很多基本问题。
【解决方案2】:

MaterialCardView 可以是一个很好的解决方案。 result

<com.google.android.material.card.MaterialCardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:strokeWidth="4dp"
            app:rippleColor="@color/colorPrimary"
            app:strokeColor="@color/colorAccent"
            app:cardCornerRadius="@dimen/margin_app"
            app:cardBackgroundColor="@android:color/white">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:paddingVertical="@dimen/margin_large"
                android:paddingHorizontal="@dimen/margin_xlarge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                ...

            </androidx.constraintlayout.widget.ConstraintLayout> 

  </com.google.android.material.card.MaterialCardView>

【讨论】:

    猜你喜欢
    • 2021-10-18
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    相关资源
    最近更新 更多