java - Android RecyclerView OnClickListener not reacting -
ok, i'm using recyclerview in project , need implement click listener each list item , icon in each of items.
what tried solution: https://stackoverflow.com/a/24933117/722462 cause looks pretty nice me. couldn't click listener react click events. ideas wrong implementation? files listed below.
fragment_search.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivityfragment"> <android.support.v7.widget.recyclerview android:id="@+id/search_recycler_view" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent"/> <linearlayout android:id="@+id/search_empty" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:orientation="vertical" android:visibility="gone"> <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/icon_search_empty" android:background="@drawable/ic_info_outline_black_48dp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancemedium" android:text="brak wyników wyszukiwania" android:id="@+id/text_search_empty" /> </linearlayout> </relativelayout>
list item:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item_station_layout" android:layout_width="match_parent" android:clickable="true" android:layout_height="48dp"> <imageview android:id="@+id/icon_item_station_fav" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centervertical="true" android:background="@drawable/ic_favorite_border_blue_24dp" android:clickable="true" android:paddingleft="16dp" android:tint="#e3f2fd"/> <textview android:id="@+id/item_station_name" android:layout_width="match_parent" android:layout_height="48dp" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:paddingleft="58dp" android:gravity="start|center_vertical" android:clickable="true" android:ellipsize="end" android:maxlines="1" android:singleline="true" android:textsize="16sp" /> </relativelayout>
and finaly searchadapter.java:
public class searchadapter extends recyclerview.adapter<searchadapter.viewholder> { private list<stationdto> stations; public static class viewholder extends recyclerview.viewholder implements view.onclicklistener { public textview stationname; public imageview favicon; private ihassearchviewholdercallback hassearchviewholdercallback; public viewholder(view itemview, ihassearchviewholdercallback hassearchviewholdercallback) { super(itemview); this.hassearchviewholdercallback = hassearchviewholdercallback; favicon = (imageview) itemview.findviewbyid(r.id.icon_item_station_fav); stationname = (textview) itemview.findviewbyid(r.id.item_station_name); itemview.setonclicklistener(this); favicon.setonclicklistener(this); context ctx = itemview.getcontext().getapplicationcontext(); fonthelper.roboto_regular.on(ctx, stationname); } @override public void onclick(view v) { switch (v.getid()) { case r.id.item_station_layout: hassearchviewholdercallback.onitemlayout(getadapterposition()); break; case r.id.icon_item_station_fav: hassearchviewholdercallback.onfavicon(getadapterposition()); break; case r.id.item_station_name: hassearchviewholdercallback.onstationname(getadapterposition()); break; } } public interface ihassearchviewholdercallback { void onitemlayout(int position); void onfavicon(int position); void onstationname(int position); } } public searchadapter(list<stationdto> stations) { this.stations = stations; } @override public searchadapter.viewholder oncreateviewholder(final viewgroup parent, int viewtype) { view item = layoutinflater .from(parent.getcontext()) .inflate(r.layout.item_station, parent, false); viewholder vh = new viewholder(item, new viewholder.ihassearchviewholdercallback() { @override public void onitemlayout(int position) { toast.maketext(parent.getcontext(), "layout #" + position, toast.length_short).show(); } @override public void onfavicon(int position) { toast.maketext(parent.getcontext(), "fav #" + position, toast.length_short).show(); } @override public void onstationname(int position) { toast.maketext(parent.getcontext(), "name #" + position, toast.length_short).show(); } }); return vh; } @override public void onbindviewholder(viewholder holder, int position) { stationdto dto = stations.get(position); holder.stationname.settext(dto.getname()); } @override public int getitemcount() { return stations.size(); } }
try changing:
@override public void onclick(view v) { switch (v.getid()) { case r.id.item_station_layout: hassearchviewholdercallback.onitemlayout(getadapterposition()); break; case r.id.icon_item_station_fav: hassearchviewholdercallback.onfavicon(getadapterposition()); break; case r.id.item_station_name: hassearchviewholdercallback.onstationname(getadapterposition()); break; } }
to
itemview.setonclicklistener(new view.onclicklistener { @override public void onclick(view v) { // appropriate code in here } });
and same favicon. have feeling problem arising fact onclicklistener on viewholder , not on textview , imageview.
Comments
Post a Comment