android - position of ViewPager item with Data Binding -
i have implemented viewpager
use of android data binding, working perfect data , click events.
i have created interface
click event
public interface itemclicklistener { public void onitemclick(); }
here instantiateitem()
of pageradapter
@override public object instantiateitem(viewgroup container, final int position) { listitembinding binding = databindingutil.inflate(mlayoutinflater, r.layout.list_item, container, false); binding.sethandler(itemclicklistener); binding.getroot().settag(position); container.addview(binding.getroot()); return binding.getroot(); }
i sending itemclicklistener
in adapter constructor.
public matchlistadapter(context context, itemclicklistener itemclicklistener) { mcontext = context; mlayoutinflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); this.itemclicklistener= itemclicklistener; }
here list_item.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <data> <variable name="handler" type="com.example.itemclicklistener" /> </data> <linearlayout android:id="@+id/root_viewitem" android:layout_width="match_parent" android:layout_height="match_parent" android:onclick="@{() -> handler.onitemclick()}" android:orientation="vertical"> //rest views , controls. </linearlayout> </layout>
now problem want pass position of clicked item, how pass position xml? have called method xml how pass position?
you can pass view in interface
public interface itemclicklistener { public void onitemclick(view view); }
than set position in tag doing
binding.getroot().settag(position);
and can position view param this
@override public void onitemclick(view view){ int position=(int)view.gettag(); }
for more detail info can refer http://developers-club.com/posts/267735/
Comments
Post a Comment