How to change background color when click on Horizontal Listview items in Android

余生长醉 提交于 2019-11-30 22:56:56

For example in this implementation you can use listSelector property in xml. Check out maybe yours also supports similar property or use the one I linked above.

public View selectedRow;//declare this in class

Add this code in onitemSelectListner

if (selectedRow != null) {
    selectedRow.setBackgroundResource(color.transparent);                   
}
selectedRow = view; //view is the onitemSelectListner View
view.setBackgroundResource(R.drawable.list_background); 

this will work 3.0 and above version..

add this onclick listener in getview method..

list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                for(int j=0;j<20;j++)
                {
                    test[j]=0;
                }


                for (int k = 0; k < mAdapter.getCount(); k++) {
                    View item = list.getChildAt(k);
                    if (item != null) {
                        test[k]=0;
                        item.setBackgroundColor(Color.WHITE);
                    }
                    view.setBackgroundColor(Color.BLUE);
                    test[i]=1;

                }
            }

test is a dummy array, use it out of onclicklistner to keep the selected even when view is changed.. like this..

   if (test[position]==1) {             
            relativeLayout.setBackgroundColor(Color.BLUE);
        }
        if(test[position]==0)
            relativeLayout.setBackgroundColor(Color.WHITE);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!