问题
I read a lot about ListSelector and selectors. But i cant figure out how to have the Item of a ListView having a different color after it got pressed.
In a Dual Fragment Layout the User clicks in Left ListView, data get loaded and presented in the right Fragment. To Keep the User Informed which item selected i want this to be highlighted until next item in the Left ListView gets pressed.
Do i need a custom State there ? How does the GMail App do this ?
thx
回答1:
Right, You need additional state there. I'm not sure, how GMail app is doing it, but default android Settings uses android:state_activated="true"
state in their Selector for the list on tablets. Don't forget to set list android:choiceMode="singleChoice"
.
回答2:
Did you try this selector?
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true"
android:drawable="@android:color/transparent" />
<item android:state_selected="true"
android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:state_selected="false"
android:drawable="@android:color/transparent" />
<item android:state_selected="false"
android:drawable="@color/message_item_read" />
Selector must be applyed to the item background, not to the listSelector.
回答3:
My solution for Android > 1.6 is:
in Activity:
public static int POS = -1;
...
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
...
POS = arg2;
...
}
in Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
...
int pos = MyActivity.POS;
if (pos == position){
convertView.setBackgroundResource(R.drawable.lv_yellow);
}else{
convertView.setBackgroundResource(R.drawable.lv_empty);
}
...
}
回答4:
I was suffering with this issue myself... after working on it for long I found the solution and it's working for me. First of all create list_view_selector.xml under your drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:drawable="@drawable/gradient_bg_listview" />
<item android:state_selected="true"
android:drawable="@drawable/gradient_bg_hover_listview" />
<item android:state_activated="true"
android:drawable="@drawable/gradient_bg_hover_listview" />
</selector>
Then the layout, which you use to fetch your list data you have to define list_view_selector. In my case I have defined like this:
//This is the adapter where I defined my customlistview
SimpleAdapter adapter1 = new SimpleAdapter(
this,
list1,
R.layout.customlistview,
new String[] {"practice_technique","blank"},
new int[] {R.id.text1,R.id.text2}
);
//This is my customlistview where I defined list_view_selector
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/list_view_selector" >
<TextView android:id="@+id/text1"
style="@style/ListViewItemTextStyle"
android:layout_width="fill_parent"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:layout_height="fill_parent"
/>
<TextView android:id="@+id/text2"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="#616162"
android:paddingLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
Then at last onListItemclick you have to write something like this:
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
HashMap<String, String> val = list.get(position);
selectedTech = val.get("practice_technique");
//below is the key line
v.setSelected(true);
startSelectedPage();
}
回答5:
Write your own ListAdapter to do this. Specifically I would extend ArrayAdapter, and put some code in this method:
View getView(int position, View convertView, ViewGroup parent)
Essentially, this code will detect if the value has been clicked, and change the background color of the view. Now, how can you tell that a view has been clicked before? I suggest that you make your ArrayAdapter
extend OnClickListener
, and set the ListView's setOnItemClickListener
to the custom ArrayAdapter
. You can figure out which item was clicked by something like this:
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//Note that position is the item which was clicked.
}
来源:https://stackoverflow.com/questions/13474760/listselector-color-after-pressed-an-item