简体   繁体   English

将背景颜色设置为Listview项目

[英]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. 功能之一是使用户能够选择多个便笺,并使用longclicks一次删除它们,以开始选择过程。 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. 使用如下所示的getChildAt(position)方法确实可以帮助我实现这一点,但是这里的问题是当我向下滚动以显示初始屏幕中不包含的项目时(取决于所显示的注释数),颜色分配被放错了位置。 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. 如果我滚动以显示3个音符,依此类推,它们下面的音符(取决于滚动过程中显示的音符数量)也会获得颜色,而不是所选音符。

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. 任何帮助将不胜感激..在我刚读大学的那一年,我刚被Java入门后,我才开始在android平台上工作。 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 活动的XML文件

<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. 或者,您可以使用SparseBooleanArray记住已检查项目的位置,并在放大视图时检查是否已选中该项目/位置并根据其设置背景。

See my other post here for a similar solution. 看到我的其他帖子在这里类似的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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