CardView上的复选框与MultiChoiceModeListener?

时间:2017-12-12 17:32:46

标签: java android xml adapter cardview

我正在创建一个笔记应用程序,我的所有笔记都显示在我的MainActivity的cardview中。我有一个MultiChoiceModeListener,我用它来一次删除多个音符。我已成功将上下文操作栏的功能添加到我可以选择备注和删除所有选定备注的位置。我无法解决的问题是,当选择某个项目时,没有复选框,没有突出显示的选择器,什么也没有。我没有任何显示项目被选中,除了我知道我选择了哪个项目和数组中的项目数。

到目前为止我尝试了什么

  • 在itemstatechanged时将Checkbox添加到item_note并设置为可见+在CAB中选中,这导致崩溃(我只能从适配器更改item_note)
  • 我也尝试使用listSelector,因为我使用了cardview并且listSelector只影响了cardview背后的背景,所以我没有成功。

这是我的代码

ContextualActionBar

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) {

            String cab_no_note_count = getResources().getString(R.string.cab_no_notes_count);

            if (list_items.contains(notes.get(position))) {
                count = count-1;
                list_items.remove(notes.get(position));
                if (getThemeCountInt() == 0) {
                    String cab_note_count = getResources().getString(R.string.cab_notes_count, count);
                    mode.setTitle(Html.fromHtml(cab_note_count));
                } else if (getThemeCountInt() == 1){
                    mode.setTitle(count + " Notes Selected");
                }
            } else {
                count = count+1;
                list_items.add(notes.get(position));
                if (getThemeCountInt() == 0) {
                    String cab_note_count = getResources().getString(R.string.cab_notes_count, count);
                    mode.setTitle(Html.fromHtml(cab_note_count));
                } else if (getThemeCountInt() == 1){
                    mode.setTitle(count + " Notes Selected");
                }
            }

            if (count == 0) {
                if (getThemeCountInt() == 0) {
                    mode.setTitle(Html.fromHtml(cab_no_note_count));
                } else if (getThemeCountInt() == 1){
                    mode.setTitle("No Notes Selected");
                }
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            actionMode = mode;
            action = true;
            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();
        }
    });

NoteAdapter

public class NoteAdapter extends ArrayAdapter<Note>{

Context ctx;
MainActivity mainActivity;
public static CheckBox checkBox;

public NoteAdapter(Context context, int resource, ArrayList<Note> notes) {
    super(context, resource, notes);

    this.ctx = context;

}

@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
    //return super.getView(position, convertView, parent);



    if(convertView == null) {
        convertView = LayoutInflater.from(getContext())
                .inflate(R.layout.item_note, null);
    }


    mainActivity = (MainActivity) ctx;
    Note note = getItem(position);

    if(note != null) {
        TextView title = (TextView) convertView.findViewById(R.id.list_note_title);
        TextView content = (TextView) convertView.findViewById(R.id.list_note_content);
        TextView date = (TextView) convertView.findViewById(R.id.list_note_date);
        checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxTest);



        Typeface music_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/melodymakernotesonly.ttf");
        Typeface scribble_card = Typeface.createFromAsset(getContext().getAssets(), "fonts/the unseen.ttf");

        if (getThemeCountInt() == 0) {
            title.setTypeface(music_font);
        } else if (getThemeCountInt() == 1) {
            title.setTypeface(scribble_card);
        }

        content.setTypeface(scribble_card);

        title.setText(note.getTitle());
        date.setText(note.getDateTimeFormatted(getContext()));

        if(note.getContent().length() > 25) {
            content.setText(note.getContent().substring(0,25) + "...");
        } else {
            content.setText(note.getContent());
        }

        if(note.getContent().length() <= 0) {
            content.setText("(Empty Note..)");
        } else {
            content.setText(note.getContent());
        }

        if (note.getTitle().length() <= 0) {
            title.setText("(Untitled)");
        } else {
            title.setText(note.getTitle());
        }

    }

    return convertView;
}

private int getThemeCountInt() {
    SharedPreferences mSharedPreferences = this.getContext().getSharedPreferences("theme", MODE_PRIVATE);
    int selectedTheme = mSharedPreferences.getInt("theme", 0);
    return selectedTheme;

}

item_note

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:id="@+id/cardViewBackground"
        android:layout_width="1000dp"
        android:layout_height="150dp"
        android:layout_gravity="start"
        android:layout_marginLeft="-80dp"
        android:layout_marginTop="-12dp"
        android:scaleType="fitStart"
        app:theme="?attr/customCardBackground" />

    <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"
        app:theme="?attr/musicThemeRemove" />

    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/checkBoxTest"
        android:visibility="gone"
        android:focusable="false"
        android:buttonTint="@color/red"
        android:checked="false"/>

    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="40dp"
        android:background="@color/red"
        app:theme="?attr/notebookRedLine"/>

    <View
        android:layout_width="780dp"
        android:layout_height="1dp"
        android:layout_marginTop="43dp"
        style="?attr/musicThemeLines"/>

    <View
        android:layout_width="780dp"
        android:layout_height="1dp"
        android:layout_marginTop="70dp"
        style="?attr/musicThemeLines"/>

    <View
        android:layout_width="780dp"
        android:layout_height="1dp"
        android:layout_marginTop="97dp"
        style="?attr/musicThemeLines" />

    <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:textColor="@color/black"
            android:textSize="28sp"
            style="?attr/item_note_title"/>

        <TextView
            android:id="@+id/list_note_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="2"
            android:textColor="@color/gunmetal"
            android:textSize="20sp"
            android:lineSpacingExtra="4dp"
            style="?attr/item_note_content"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        app:theme="?attr/lastUpdatedDate">

        <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>

我只是想出了一些东西,但它尚未100%正常工作,任何见解都会很棒

if (mainActivity.list_items.contains(note)) {
            checkBox.setVisibility(View.VISIBLE);
            checkBox.setChecked(true);
        } else if (!mainActivity.list_items.contains(note)) {
            checkBox.setVisibility(View.GONE);
            checkBox.setChecked(true);
        }

我已将此添加到我的NoteAdapter中,它似乎正在做正确的事情。有了这个,我的第一个选定项目会在选中时显示一个检查,如果没有则将其删除。但只有我的第一个选定项目显示此更改。我之后选择的任何内容都没有显示一个复选框:/我不知道是什么造成这种情况因为我知道当我选择其他项目时,它们包含在mainActivity.list_items中,就像我的第一个注释是< / p>

0 个答案:

没有答案