问题
I'm puzzled with this. I am searching for over 2 hours and cannot understand what is happening.
I have a Listview that I want the items (rows) to be selectable. To achieve this, I have a selector that I set as a background to my row layout.
The ListView is defined this way in the fragment.xml layout file:
<ListView
android:id="@+id/lst_friends"
android:layout_width="match_parent"
android:dividerHeight="1dp"
android:divider="#ff0000"
android:layout_height="0dp"
android:layout_weight="1.0" />
The selector (bg_row_contact.xml) is this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" android:state_selected="false"/>
<item android:drawable="@color/pink" android:state_selected="true"/>
</selector>
My row layout (row_contact.xml) is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_row_contact"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="7dp"
android:gravity="center" >
<ImageView
android:id="@+id/img_lst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/contentDescription_pic" />
</LinearLayout>
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginRight="20dp"
android:layout_marginLeft="10dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>
Now, I have a custom adapter, in which I do this to mark an item as selected when the user clicks on the row, which works perfect:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View from recycle
View row = convertView;
// Handle inflation and make sure not to re-use a header view
if (row == null) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_contact, null);
row.setTag(position);
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(!v.isSelected());
}
});
}
boolean condition = (position==3);
row.setSelected(condition);
}
My problem now is that I want to obviously maintain the selection state when the listview is scrolled. So I have a condition which I check against and set the row to selected or not based on this, as you can see in the
row.setSelected(boolean condition);
The problem is that although the condition is true for some rows, the row's background selector is not working and the row is not highlighed.
I have tried everything, but cannot find what the bleep is going on here.
Any suggestions ?
回答1:
I've been playing around a little and I think I found a solution to your problem. This one works with list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
You can get the selected items via list.getCheckedItemPositions()
. The only weird thing about it is, that in your selector you have to use android:state_activated
instead of android:state_checked
.
Activity code:
ListView list = (ListView) findViewById(R.id.listView);
// dummy values
final String[] values = new String[] { "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "w", "x", "y", "z" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row_contact, values){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// View from recycle
View row = convertView;
// Handle inflation and make sure not to re-use a header view
if (row == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_contact, null);
row.setTag(position);
}
((TextView)row.findViewById(R.id.txt_title)).setText(values[position]);
return row;
}
};
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
The selector (bg_row_contact.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" android:state_activated="false"/>
<item android:drawable="@android:color/black" android:state_activated="true"/>
</selector>
Hope this helps.
来源:https://stackoverflow.com/questions/21761972/listview-and-row-selector-not-working