ViewPager项目与数据绑定的位置 [英] position of ViewPager item with Data Binding

查看:244
本文介绍了ViewPager项目与数据绑定的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Android Data Binding实现了 ViewPager ,它与数据和点击事件完美结合。

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();
}

这是我的 instantiateItem() 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();
}

我正在发送 itemClickListener 在适配器构造函数中。

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;
}

这是我的 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>

现在的问题是,我想通过点击的位置,从xml传递那个位置?因为我已经从xml调用我的方法如何传递位置?

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?

推荐答案

您可以在界面

 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();
}

有关更多详细信息,您可以参考 http://developers-club.com/posts/267735/

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

这篇关于ViewPager项目与数据绑定的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆