Android的Recyclerview多个项目的onclick [英] Android Recyclerview Multiple onclick items

查看:204
本文介绍了Android的Recyclerview多个项目的onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个recyclerview,其中每个项目是一个简单的TextView +复选框

I am trying to have a recyclerview where each item is a simple TextView + Checkbox

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/title"
    android:layout_width="0dp"
    android:layout_weight="5"
    android:layout_height="wrap_content"
    />
<CheckBox
    android:id="@+id/chk"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content" />
</LinearLayout>

我想实现是(联合国)选中复选框,如果点击它。但是,如果单击TextView的,然后做对整个项目的纹波动画和启动片段过渡。

What I am trying to achieve is to (un)check the checkbox if it is clicked. But if the textview is clicked, then do the ripple animation on the entire item and start a fragment transition.

我无法在网上找到一个例子,我发现最接近的是 RecyclerView的onClick

I couldn't find an example online and the closest that I found was RecyclerView onClick

我设法得到的onclick然而,功能,在code

I managed to get the onclick to function however, in the code

@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
  mListener.onItemClick(childView, view.getChildPosition(childView));
  return true;
}
return false;

}

我的问题是,childView返回是的LinearLayout而不是TextView的或复选框。我因此无法确定是否点击了在TextView的或做的复选框

The problem I have is that the childView returned is the linearlayout and not the textview or checkbox. I am thus unable to determine whether the click was done on the textview or the checkbox.

有没有办法,我失去了一些东西?理想情况下,如果有人对如何实现这个工作的例子,那将是巨大的。

Is there something that I am missing? Ideally if someone has a working example of how to implement this, that would be great.

感谢。

推荐答案

在这里你去:

您列表项的xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="?attr/selectableItemBackground"
android:layout_height="72dp">

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:text="My Text"
    android:layout_gravity="left|center_vertical"
    android:paddingLeft="16dp"
    android:layout_height="wrap_content" />

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:layout_gravity="right|center_vertical" />


(我用了一个的FrameLayout 作为根,但一个的LinearLayout 应可能正常工作。)

(I've used a FrameLayout as root but a LinearLayout should possibly work as well.)

而在适配器你的 RecyclerView 使用这样的:

            @Override
            public YourViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
                View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);

                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.w("Test", "Parent clicked");
                    }
                });
                return new YourViewHolder(itemView);

            }

            @Override
            public void onBindViewHolder(YourViewHolder viewHolder, int i) {

                viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.w("Test", "Checkbox clicked");
                    }
                });
            }

这篇关于Android的Recyclerview多个项目的onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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