pra*_*kla 1 android android-recyclerview
我想要处理回收器视图中的事件 - :
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private ClickListener clickListener;
private GestureDetector gestureDetector;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener){
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null)
{
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
Log.i("TAG", "Radhe handling LongPress ");
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(e));
{
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
public static interface ClickListener{
public void onClick(View view, int position);
public void onLongClick(View view, int position);
}
Run Code Online (Sandbox Code Playgroud)
我还有以下代码 - :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
filterAdapter = new FilterAdapter(getActivity(), getData());
recyclerView.setAdapter(filterAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
Log.i("TAG", "Radhe child Clicked ");
Toast.makeText(getActivity(),"CLick",Toast.LENGTH_SHORT).show();
}
@Override
public void onLongClick(View view, int position) {
Log.i("TAG", "Radhe child Long Clicked ");
Toast.makeText(getActivity(),"Long CLick",Toast.LENGTH_SHORT).show();
}
}));
return layout;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行它时: - 和
有人能说出为什么会这样吗?我不知道它的原因.提前致谢.
当你打电话onClick时onInterceptTouchEvent需要return true显示已经消耗的事件,而不是继续为它发起事件.
Javadoc addOnItemTouchListener说:
一旦侦听器从RecyclerView.OnItemTouchListener.onInterceptTouchEvent(RecyclerView,MotionEvent)返回true,将为每个传入的MotionEvent调用其RecyclerView.OnItemTouchListener.onTouchEvent(RecyclerView,MotionEvent)方法,直到手势结束.
来自onInterceptTouchEvent手段的虚假
继续当前行为并继续观察手势中的未来事件.
只是为了澄清你现有的代码(但是我不确定你试图用if语句中的gestureDetector.onTouchEvent(e)实现什么,请确保你不会意外地使用滚动事件):
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1743 次 |
| 最近记录: |