Change background color of an item in Android ListActivity onListItemClick

后端 未结 6 1039
醉酒成梦
醉酒成梦 2021-02-14 09:12

I know it sounds very simple, and there are questions about this. But none of it could solve my problem. So here we go:

I want to change background color of a list item

6条回答
  •  一生所求
    2021-02-14 09:12

    If you are dealing with ListFragment then this code will be helpful,

      @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            if (view != null) {
                getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                getListView().setDescendantFocusability(ListView.FOCUS_AFTER_DESCENDANTS);
                catagoryValueListView=getListView();
                catagoryValueListView.setOnItemClickListener(new OnItemClickListener() {
    
                 @Override
                 public void onItemClick(AdapterView parent, View view, int position, long id) {
                     if (ColoredView != null)
                         ColoredView.setBackgroundColor(Color.WHITE); //original color
    
                     view.setBackgroundColor(Color.BLUE); //selected color
                     ColoredView = view;
                                      }
            });
        }
    
    }
    

提交回复
热议问题