position of ViewPager item with Data Binding

让人想犯罪 __ 提交于 2019-12-19 08:13:48

问题


I have implemented ViewPager with use of android Data Binding, it is working perfect with data and click events.

I have created interface for click event

public interface ItemClickListener {
    public void onItemClick();
}

Here is my 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 am 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 is my 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 and controls.

   </LinearLayout>
</layout>

Now problem is that i want to pass position of clicked item, how to pass that position from xml? As i have called my method from xml how to pass position?


回答1:


You can pass view in interface

 public interface ItemClickListener {
     public void onItemClick(View view);
  }

Than set position in tag which you already doing

binding.getRoot().setTag(position);

And than you can get position from that view param like this

@Override
public void onItemClick(View view){
  int position=(int)view.getTag();
}

For more detail info you can refer http://developers-club.com/posts/267735/




回答2:


You can pass parameters in the lambda expression, like this:

android:onClick="@{() -> handler.onItemClick(rootItemView.getTag().toString())}" 

And of course you'll need to edit your interface method and its implementations:

public void onItemClick(String data);



回答3:


Both the answer helped me and both are working, i have found another way also, however its bit lengthy.

I have taken one more Integer variable in my list_item.xml and setting its value from adapter.

<data>
    <variable
        name="position"
        type="Integer"/>
</data>

and passing it in onItemClick

android:onClick="@{() -> handler.onItemClick(position)}"

then interface will look like this

public interface ItemClickListener {
    public void onItemClick(int position);
}

will be setting value of position from instantiateItem()

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    ListItemBinding binding = DataBindingUtil.inflate(mLayoutInflater, R.layout.list_item, container, false);
    binding.setHandler(itemClickListener);
    binding.setPosition(position);
    binding.getRoot().setTag(position);
    container.addView(binding.getRoot());
    return binding.getRoot();
}


来源:https://stackoverflow.com/questions/39463100/position-of-viewpager-item-with-data-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!