android - Unable to apply a custom listSelector to ListView -
i have gone on several answers now android listselector not visible in custom listview listview item background via custom selector
and followed point (currently using approach) http://cyrilmottier.com/2011/08/08/listview-tips-tricks-3-create-fancy-listviews/
i have set drawselectorontop
true
however, cannot listselector
work. basically, @ wit's end. guess has way list item made? here xml:
list_entry.xml
<?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="wrap_content" android:padding="5dp"> <imagebutton android:id="@+id/contextmenuicon" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_action_overflow" android:background="@null"/> <textview android:id="@+id/notetitle" android:layout_alignparentleft="true" android:layout_toleftof="@id/contextmenuicon" android:layout_alignbottom="@id/contextmenuicon" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_alignparenttop="true" android:textisselectable="false" android:ellipsize="end" android:singleline="true" android:textsize="20sp" android:textcolor="@android:color/black"/> <textview android:id="@+id/notecreationdate" android:layout_alignparentleft="true" android:layout_alignparentbottom="true" android:layout_below="@id/notetitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="18sp" android:textcolor="#808080"/> <textview android:id="@+id/noteprivacy" android:layout_alignparentright="true" android:layout_alignparentbottom="true" android:layout_below="@id/contextmenuicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="18sp" android:textcolor="#808080" /> </relativelayout>
list_selector_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:endcolor="#ffc579" android:startcolor="#fb9d23" android:angle="90"></gradient> </shape>
list_selector_focussed.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:endcolor="#f7ddb8" android:startcolor="#f5c98c" android:angle="90"></gradient> </shape>
list_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/list_selector_pressed" /> <item android:state_focused="true" android:drawable="@drawable/list_selector_focused" /> <item android:drawable="@android:color/transparent" /> </selector>
please tell me how create list selector work!
you using imagebutton within list item. when ever click item, imagebutton focus.
but in case set imagebutton ground
android:background="@null"
it create cannot see focus. can see problem if remove line.
to focus list item, can utilize code in root view of xml.
android:descendantfocusability="blocksdescendants"
<?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="wrap_content" android:descendantfocusability="blocksdescendants" android:padding="5dp"> <imagebutton android:id="@+id/contextmenuicon" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_action_overflow" android:background="@null"/> <textview android:id="@+id/notetitle" android:layout_alignparentleft="true" android:layout_toleftof="@id/contextmenuicon" android:layout_alignbottom="@id/contextmenuicon" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_alignparenttop="true" android:textisselectable="false" android:ellipsize="end" android:singleline="true" android:textsize="20sp" android:textcolor="@android:color/black"/> <textview android:id="@+id/notecreationdate" android:layout_alignparentleft="true" android:layout_alignparentbottom="true" android:layout_below="@id/notetitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="18sp" android:textcolor="#808080"/> <textview android:id="@+id/noteprivacy" android:layout_alignparentright="true" android:layout_alignparentbottom="true" android:layout_below="@id/contextmenuicon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="18sp" android:textcolor="#808080" /> </relativelayout>
i create quick test xml , work normal.
public class mainactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction().add(r.id.container, new placeholderfragment()).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { homecoming true; } homecoming super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); listview list = (listview)rootview.findviewbyid(r.id.list); arraylist<myitem> listitem = genlistitem(20); myadapter adapter = new myadapter(mainactivity.this, android.r.layout.simple_list_item_1, listitem); list.setadapter(adapter); homecoming rootview; } private arraylist<myitem> genlistitem(int size) { arraylist<myitem> listitem = new arraylist<mainactivity.myitem>(); (int = 0; < size; i++) { myitem item = new myitem(); item.setname("item " + i); listitem.add(item); } homecoming listitem; } } public static class myitem{ string name; /** * @return name */ public string getname() { homecoming name; } /** * @param name name set */ public void setname(string name) { name = name; } } public class myadapter extends arrayadapter<myitem>{ private layoutinflater inflator; private context mcontext; public myadapter(context context, int resource, list<myitem> objects) { super(context, resource, objects); mcontext = context; inflator = ((activity) context).getlayoutinflater(); } @override public view getview(final int position, view convertview, viewgroup parent) { final viewholder holder; if (convertview == null) { convertview = inflator.inflate(r.layout.list_entry, null); holder = new viewholder(); holder.name = (textview) convertview.findviewbyid(r.id.notetitle); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } myitem item = getitem(position); holder.name.settext(item.getname()); homecoming convertview; } class viewholder { public textview name; } } }
android android-listview
No comments:
Post a Comment