获取列表视图的背景颜色

时间:2016-01-18 07:17:39

标签: java android listview colors

我需要多次更改listview的背景颜色,但我不知道如何在点击之前获取listview的颜色。

我有这段代码:

if(colores.get(i).equals("0"))
                {
                    listView.getChildAt(i).setBackgroundColor(Color.GREEN);
                }
                else if(colores.get(i).equals("1"))
                {
                    listView.getChildAt(i).setBackgroundColor(Color.RED);
                }
                else if(colores.get(i).equals("2"))
                {
                    listView.getChildAt(i).setBackgroundColor(Color.YELLOW);
                }
                else
                {
                    listView.getChildAt(i).setBackgroundColor(Color.WHITE);
                }

我可以更改颜色,但是当我点击列表视图中的一个项目时我还需要更改颜色,但我不知道如何知道它之前的颜色。

任何人都可以帮助我吗?谢谢! :)

1 个答案:

答案 0 :(得分:0)

执行此操作:更改颜色然后创建处理程序。此外,将View视图设置为最终的View视图。你可以根据它是什么子项或者换句话说项目的位置来执行if / else语句或switch语句。

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
            view.setBackgroundColor(Color.RED); //on click change to red

            Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    view.setBackgroundColor(Color.WHITE); //after click changes to white
                }
            };

            handler.postDelayed(runnable, 200); //change to white after 200ms
        }
    });

    //DO THIS BELOW FOR SPECIFIC ITEM IN LISTVIEW

private final String currentColor_P1; //current background color for item at position 1

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
            if (position == 1) {
                view.setBackgroundColor(Color.BLUE);
            } else if (position == 2) {
                view.setBackgroundColor(Color.YELLOW);
            } else {
                view.setBackgroundColor(Color.RED);
            }

            //OR check for position and check for the known color that you have set for that position
            if(position == 1 && currentColor_P1 == BLUE) {
                 view.setBackgroundColor(Color.YELLOW);

            } else if(position == 1 && currentColor_P1 == RED) {
                 view.setBackgroundColor(Color.YELLOW);
            }

            //Change color to whatever after clicked and after initial color change happened
            Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    view.setBackgroundColor(Color.WHITE);

                    //or 
                    if(position == 1){
                         view.setBackgroundColor(Color.BLUE)
                         currentColor_P1 = "BLUE"; //now you set the current backgound for item at position 1 to "blue"
                    }



                }
            };

            handler.postDelayed(runnable, 200);
        }
    });