简体   繁体   中英

Setting Background Color to Listview Item

I'm building something like a notepad app. One of the features is to enable users select multiple notes and delete them at once using longclicks to start the selection process. Everything works well so far however, there is no physical indication of the selected items (background colour). Using the getChildAt(position) method as shown below does help me achieve this but the problem here is when I scroll down to reveal items not contained in the initial screen (depending on the number of notes revealed), the color assignment is misplaced. For example, if I scroll down to show two items not contained in the screen while selecting items, if I tap on a note, it gets selected but background color is set to the note that is two positions beneath it. Same happens if I scroll to reveal 3 notes and so on, notes beneath them (depending on the number of notes revealed during scroll) gets the color and not the selected note.

Any help would be so much appreciated.. I just started working on the android platform after just having been introducted to Java in my current year of university. I'm pretty sure having to use a custom adapter will work but I'm trying to keep this as simple as possible for me. Beneath is a snippet from my project.

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_entries);  
//SOME CODES HERE

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, theNamesOfFiles);


        entriesView = (ListView) findViewById(R.id.listEntriesView);
        entriesView.setAdapter(adapter);
        entriesView.setOnItemClickListener(this);
        entriesView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        entriesView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

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

                MenuInflater inflater = mode.getMenuInflater();
                inflater.inflate(R.menu.context_menu, menu);
                return true;
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                String a = entriesView.getItemAtPosition(position).toString();
//This if statement is necessary because should the user de-selects an item, the system should remove it from the arraylist so they don't get passed to the delete method
                if (selectedMenuItems.contains(a)) {
                    int b = selectedMenuItems.indexOf(a);
                    selectedMenuItems.remove(b);
                    itemSelectedCount = selectedMenuItems.size();
                    mode.setTitle(itemSelectedCount + " Item(s) Selected");
                    View view = entriesView.getChildAt(position);
                    view.setBackgroundColor(Color.TRANSPARENT);
                }
                else {
                    selectedMenuItems.add(entriesView.getItemAtPosition(position).toString());
                    itemSelectedCount = selectedMenuItems.size();
                    mode.setTitle(itemSelectedCount + " Item(s) Selected");
                 // entriesView.getChildAt(position).setBackgroundColor(Color.LTGRAY);
                    View view = entriesView.getChildAt(position);
                    view.setBackgroundColor(Color.LTGRAY);
                }
                mode.invalidate();
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {

                int id = item.getItemId();
                switch (id) {
                    case(R.id.delete_menu_button):
                        deleteMethod();
                        selectedMenuItems.clear();
                        mode.finish();
                    break;
                    case(R.id.edit_menu_button1):
                        callEditActiity();
                        selectedMenuItems.clear();
                        mode.finish();
                    break;
                    default:
                        return false;
                }
                return true;
            }


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

                if (itemSelectedCount == 1){
                    MenuItem item = menu.findItem(R.id.edit_menu_button1);
                    item.setVisible(true);
                    return true;
                } else {
                    MenuItem item = menu.findItem(R.id.edit_menu_button1);
                    item.setVisible(false);
                    return true;
                }
            }


            @Override
            public void onDestroyActionMode(ActionMode mode) {
                selectedMenuItems.clear();

            }
        });
    }

The activity's XML file

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listEntriesView">

    </ListView>

You need to make a custom adapter and in your item remember the checked state. Alternatively you can use a SparseBooleanArray to remember the positions of checked items and when you inflate the view, you check if that item/postion is checked and set the background according.

See my other post here for a similar solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM