Android: handling clicks on image view inside recycler#39;s view item using touch framework(Android:使用触摸框架处理回收者视图项内的图像视图点击)
问题描述
我正在尝试捕获对 ImageView
的点击,该点击包含在 RecyclerView
项内.我已经实现了 RecyclerView.OnItemTouchListener
并让手势检测器处理正常点击和长按 RecyclerView
项目的运动事件.
I am trying to capture the clicks on ImageView
which is enclosed inside the RecyclerView
item. I have implemented RecyclerView.OnItemTouchListener
and has gesture detector process the motion events for normal clicks and long press on RecyclerView
item.
由于我希望使用相同的触摸框架来处理 RecylcerView
项内的子视图上的触摸事件,因此我也将 View.OnTouchListener
设置为图像视图,覆盖 onTouch
实现返回 true,希望在点击时 ImageView
会使用触摸.
Since I want to have the same touch framework to process touch event on child views inside the RecylcerView
item, I have set View.OnTouchListener
with the image view as well, overriding the onTouch
implementation returning true hoping that the touch shall be consumed by the ImageView
when it gets clicked.
我故意在 onInterceptTouchEvent
中总是从 RecyclerView
返回 false,希望子视图首先处理事件,如果它们都没有消耗,那么 RecyclerView
的项目视图应在 onTouchEvent
处理期间处理点击的手势事件.
I am intentionally returning false in onInterceptTouchEvent
always from the RecyclerView
, hoping the child views process the event first and if none of them consumes then RecyclerView
's item view shall handle the gesture event for clicks during onTouchEvent
handling.
通过上述理解,我能够在点击 ImageView
时接收到触摸事件.
With the above understanding I am able to receive touch events on the ImageView
when clicked on it.
问题是,当我点击ImageView
外的RecyclerView
项时,RecyclerView
的onTouchEvent
根本没有被调用.我对触摸框架的理解是,如果子视图不处理触摸事件,则 RecyclerView
的 onTouchEvent
应该有机会处理相同的事件.然而,这不是我能够实现的.
The problem is that, when I click on the RecyclerView
item outside ImageView
, RecyclerView
's onTouchEvent
is not getting called at all. my understanding of the touch framework is that if child views doesn't process the touch event, the RecyclerView
's onTouchEvent
should get a chance to process the same. However this is not what i am able to achieve.
谁能建议背后发生了什么以及处理上述点击事件的正确方法是什么?
Can anyone suggest what is happening behind and what is the correct way to process click events for the above?
推荐答案
这会解决你的问题:
public class OnItemClickListener implements View.OnClickListener {
private int position;
private OnItemClickCallback onItemClickCallback;
public OnItemClickListener(int position, OnItemClickCallback onItemClickCallback) {
this.position = position;
this.onItemClickCallback = onItemClickCallback;
}
@Override
public void onClick(View view) {
onItemClickCallback.onItemClicked(view, position);
}
public interface OnItemClickCallback {
void onItemClicked(View view, int position);
}
}
获取OnItemClickCallback"接口的实例并将其放入您的活动或片段中:
Get an instance of 'OnItemClickCallback' interface and put it in your activity or fragment:
private OnItemClickListener.OnItemClickCallback onItemClickCallback = new OnItemClickListener.OnItemClickCallback() {
@Override
public void onItemClicked(View view, int position) {
// switch(view.getId()) ...
}
};
然后,将该回调传递给您的 recyclerView:
Then, pass that callback to your recyclerView:
recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(Arrays.asList("1", "2", "3"), onItemClickCallback));
最后,这将是您的适配器:
Finally, this would be your adapter:
public class SimpleStringRecyclerViewAdapter extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {
private List<String> mValues;
private OnItemClickListener.OnItemClickCallback onItemClickCallback;
public SimpleStringRecyclerViewAdapter(List<String> items, OnItemClickListener.OnItemClickCallback onItemClickCallback) {
mValues = items;
this.onItemClickCallback = onItemClickCallback;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mTextView;
public ViewHolder(View view) {
super(view);
mView = view;
mTextView = (TextView) view.findViewById(R.id.txt_title);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mTextView.setText(mValues.get(position));
holder.mView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
holder.mTextView.setOnClickListener(new OnItemClickListener(position, onItemClickCallback));
}
@Override
public int getItemCount() {
return mValues.size();
}
}
这篇关于Android:使用触摸框架处理回收者视图项内的图像视图点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:使用触摸框架处理回收者视图项内的图像视图点击
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01