【问题标题】:Can't scroll RecyclerView because OnClick in the ViewHolder无法滚动 RecyclerView,因为 ViewHolder 中的 OnClick
【发布时间】:2017-05-08 02:42:24
【问题描述】:

当我在 ViewHolder 中实现 OnClick 时,片段中的 RecyclerView 无法滚动。

当我实现 OnClick 时,列表是库存。

我使用 v.getParent().requestDisallowInterceptTouchEvent(true);因为 AppBarLayout 窃取了我的垂直滚动而获得焦点。

JAVA:

private void initRecycleView() {
    autoCompleteAdapter = new AutoCompleteSearchAdapter(getActivity(), this);
    autoCompleteList.setLayoutManager(new LinearLayoutManager(getActivity()));
    autoCompleteList.setAdapter(autoCompleteAdapter);
    autoCompleteList.setOnTouchListener(new View.OnTouchListener() {
      @Override public boolean onTouch(View v, MotionEvent event) {
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
      }
    });
  }

XML:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    >

  <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="10dp"
      android:theme="@style/AppTheme.AppBarOverlay"
      >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="78dp"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        />

    <include layout="@layout/widget_search_bar"/>

  </android.support.design.widget.AppBarLayout>

  <FrameLayout
      android:id="@+id/fragment_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/cardview_dark_background"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      />
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 你试过不使用 setOnTouchListener 吗?
  • 如果我实现 parentView.setOnTouchListener 它将滚动但我如何从中获取 OnClick 行为?
  • 你要获取recyclerview的itemClick。我说的对吗?
  • 是的,我想从每个不是 HEADER 类型的项目中获得项目点击。
  • 什么OnClick?您的代码中没有OnClick

标签: android onclick android-recyclerview touch-event


【解决方案1】:

您可以为每个itemViews 实现setOnItemClickListener 接口,因为我在此处提供了答案,包括对该过程的解释。 RecyclerView - get Position inside Activity rather than RecyclerViewAdapter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多