Change Selection in a ListView from Orange to Green

后端 未结 5 1177
半阙折子戏
半阙折子戏 2020-12-24 02:27

How do I do this per selected list item.

I tried adding this to android:background



        
5条回答
  •  悲&欢浪女
    2020-12-24 03:15

    I was also facing your problem where the color change applies to the whole list.

    But now its working for me now:

    My main page containing the listview (noticed I did not apply the listselector here):

     
    

    Instead applied the listselector to getView, its like tell that particular row (convertView) to use the listselector.xml in drawable foldable when its state changes, e.g. pressed, focused...:

    @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                if(convertView == null){
                    LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                    convertView = li.inflate(R.layout.control_list, null);
                    viewHolder = new ViewHolder();
    
                    viewHolder.topText = (TextView) convertView.findViewById(R.id.toptext);
                    viewHolder.bottomText = (TextView) convertView.findViewById(R.id.bottomtext);
                    convertView.setTag(viewHolder);
    
                }
                else{
                    viewHolder = (ViewHolder) convertView.getTag();
                }
    
    
                        //each convertView will be assigned the listselector
                convertView.setBackgroundResource(R.drawable.listselector);
    
    
                viewHolder.topText.setText(testSchemes[position]);
    
                //viewHolder.vText.setText(testDetails[position]);
                return convertView;
            }
    

    And finally my listselector.xml:

    
    
    
        
    
        
        
    
        
    
    
    
    
    

    This is what the above code does: All entries when first loaded are lightgreen. Scrolling up and down using cursor highlights the selected entry orange. Pressing a row turns it to darkgreen.

提交回复
热议问题