如何使用onInterceptTouchEvent从RecyclerView中单击特定的视图? [英] How to get specific view clicked from RecyclerView with onInterceptTouchEvent?

查看:405
本文介绍了如何使用onInterceptTouchEvent从RecyclerView中单击特定的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找,但仍然找不到任何东西.我尝试过

I have been looking for, but still I have no found anything. I tried

View child = mirecycler.findChildViewUnder(e.getX(), and.getY());  

并且仅在我的情况下使父级CardView容器行"有效,我需要获取特定的TextViewImageView.

and it worked only to get the parent in my case the CardView container "row" , I need get specific TextView or ImageView.

我想在不影响CardViewonClick的情况下将onClick设置为ImageView.

I want to set onClick to ImageView without affect onClick of CardView.

public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
    View child = rv.findChildViewUnder(e.getX(), e.getY());
    if (child != null && mGestureDetector.onTouchEvent(e)) {
        Toast.makeText(MainActivity.this, "tocaste " + child, Toast.LENGTH_SHORT).show();
        return true;
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}

推荐答案

如果必须使用onInterceptTouchEvent,我不确定我能为您提供帮助.我只在不需要管理特定点击的情况下才使用它,但是也许我的解决方案可以与之配合使用(尽管我没有尝试过)

If you have to use onInterceptTouchEvent I'm not sure I can help you. I only use it when I don't have to manage specific clicks, but maybe my solution can work together with it (Although I didn't tried it)

我将为您提供一个解决方案,使用客户适配器来检测点击的时间点:

I will provide a solution for you to detect the point where the click was made, using a customer adapter:

public class SomeRecyclerAdapter extends RecyclerView.Adapter<SomeRecyclerAdapter.SomeListViewHolder> {

    ....

    @Override
public void onBindViewHolder(final SomeViewHolder holder, final int position) {
    SomeItem item = someList.get(position);

    holder.itemView.setTag(item);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            SomeListItem item = (SomeListItem) holder.itemView.getTag();
            Toast.makeText(context, "Recycle Click " + item.getName(), Toast.LENGTH_SHORT).show();
        }
    });

    holder.vImage.setTag(item);
    holder.vImage
            .setOnClickListener(new CompoundButton.OnClickListener(){

                @Override
                public void onClick(View v) {
                    SomeListItem item = (SomeItem) holder.vImage.getTag();
                    Toast.makeText(context, item.getName() + " Image Clicked", Toast.LENGTH_SHORT).show();
                }
            });
    ....

    public static class SomeListViewHolder extends RecyclerView.ViewHolder{
        protected ImageView vImage;

        public SomeListViewHolder(View itemView) {
            super(itemView);
            vImage = (ImageView) itemView.findViewById(R.id.tem_some_list);
        }

    }

}

我的示例是如何处理回收站项目图像"中的单击. 我已经尝试过该解决方案,并且可以正常工作,这意味着,如果您单击该项目(图像除外),它将打印"Recycle Click ...",如果您单击图像,则将打印"...".图片已点击".

My example is how to handle a click in a Recycler Item Image. I've tried this solution and it works, this meaning that if you click in the item (except the Image) it will print "Recycle Click ...", and if you click on the image it will print " .... Image Clicked".

也许(未经测试,但是您可以做到,让我们知道)您可以将onInterceptTouchEvent用于CardView,将onClickListener用于图像.

Maybe (not tested, but you can do it and let us know) you can use the onInterceptTouchEvent for the CardView, and the onClickListener for the image.

我希望这就是您要寻找的,如果您找到更好的解决方案,请告诉我.

I hope this is what you were looking for and if you find a better solution let me know.

这篇关于如何使用onInterceptTouchEvent从RecyclerView中单击特定的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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