ListView item won't stay “selected”

后端 未结 10 1294
暗喜
暗喜 2020-12-10 05:50

I want to change the background of a listview item when the user clicks it. Kind of like the Honeycomb settings page (Although I\'m not dealing with just settings so I\'m no

相关标签:
10条回答
  • 2020-12-10 06:49

    This is implementation of sgarman idea:

        package com.mypackage;
    
    import java.util.Vector;
    
    import com.myapp.R;
    import com.myapp.data.Address;
    
    import android.content.Context;
    import android.graphics.Color;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    public class AddressesListAdapter extends BaseAdapter{
        protected Context context;
        protected LayoutInflater mInflater;
        protected int itemResourceId;
        protected Vector<Address> contentItems = new Vector<Address>();
        protected Vector<Boolean> selectedStates;
        private static final String TAG = "myapp";
    
        public AddressesListAdapter(Context context, Vector<Address> contentItems) {
            this.context = context;
            this.contentItems = contentItems;
            mInflater = LayoutInflater.from(context);
            itemResourceId = R.layout.address_list_item;
            selectedStates = new Vector<Boolean>();
            //initial fill
            clearSelectedState();
        }
    
        @Override
        public int getCount() {
            return contentItems.size();
        }
    
        @Override
        public Object getItem(int position) {
            return contentItems.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(itemResourceId, null);
    
                holder = new ViewHolder();
                holder.addressName = (TextView) convertView.findViewById(R.id.addressName);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            Address address = (Address) contentItems.get(position);
            holder.addressName.setText(address.getAddressName());
            holder.addressName.setOnClickListener(new SetFocusListener(position));
    
            //restore saved position from saving vector
            if (selectedStates.get(position)) holder.addressName.setBackgroundColor(Color.BLUE);
            else holder.addressName.setBackgroundColor(Color.TRANSPARENT);
    
            return convertView;
        }
    
        private void clearSelectedState () {
            selectedStates.clear();  
            for (int i = 0 ; i <= contentItems.size(); i++) {
                selectedStates.add(new Boolean(false));
            } 
        }
    
        private class SetFocusListener implements View.OnClickListener {
            private int position;
    
            public SetFocusListener(int position) {
                this.position = position;
            }
    
            @Override
            public void onClick(View v) {
                //clear selected state vector
                clearSelectedState();
                //set selected position
                selectedStates.set(position, new Boolean(true));
                //refresh adapter to redraw focus
                notifyDataSetChanged();
            }
        }
    
        static class ViewHolder {
              TextView addressName;
        }
    }
    

    Th only concern that it may be to costly to setup new listener for every getView() iteration

    0 讨论(0)
  • 2020-12-10 06:49

    Have you tried android:state_selected="true" ?

    0 讨论(0)
  • 2020-12-10 06:52

    When you release your finger from the cell it no longer registers as pressed. What you are going to want to do is actually change the background of the individual row when a users selects is. This means implementing an onItemClick or onItemTouch and flagging the adapter to redraw the row with the new background. If you are already using a custom list adapter you can just implement a check against a boolean in your getView() method. You will also need to keep track which rows are 'selected' and which are not.

    pseudocode:

       public View getView(int pos, View convertView, ViewGroup parent) {
          if(isChecked[pos]) //set background to checked color
       }
    
    0 讨论(0)
  • 2020-12-10 06:52

    If you keep your listView through the whole activity you can do a mListView.isItemChecked(position) in the getView() method. And them set the background color depending on the result.

    0 讨论(0)
提交回复
热议问题