【问题标题】:Use RecyclerView inside ScrollView with flexible Recycler item height在具有灵活 Recycler 项目高度的 ScrollView 中使用 RecyclerView
【发布时间】:2015-09-06 06:38:09
【问题描述】:

我想知道有没有办法使用RecyclerView

在此之前,我在 ScrollView 中使用了带有 fixed height 的 RecyclerView,但这次我不知道项目的高度。

提示:在问这个问题之前,我阅读了所有关于堆栈问题的问题和解决方案。

更新: 一些解决方案展示了如何自行滚动 RecyclerView,但我想展示它展开。

【问题讨论】:

  • 你的问题是什么?
  • @OmarHassan 我应该在 ScrollView 中使用多个 RecyclerView。就这样
  • 以上没有帮助我......只有那个答案在这里发布:Recyclerview inside scrollview
  • @Ester 或者你到底想做什么?我大约两周后解决我的问题。 (当时我不是专家)
  • 只是想帮助其他有类似问题的人......就像我一样......

标签: android android-scrollview android-recyclerview scrollable


【解决方案1】:

如果你只想滚动,那么你可以使用 NestedScrollView 而不是 ScrollView 所以你可以修改你的代码如下:

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

            //design your content here with RecyclerView 

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

【讨论】:

  • NestedScrollView 而不是 ScrollView 解决了我的问题,谢谢!
  • 这应该是公认的答案。没有 ConstraintLayout,没有自定义子类。这只是按预期工作。
【解决方案2】:

我在世界各地搜索这个问题的答案,但我没有找到这个热门问题的任何直接答案。最后我混合了一些技巧并解决了这个问题!

我的问题开始于我的老板让我在 ScrollView 中使用 RecyclerView,而您知道我们不能使用两个 Scrollable 对象除非我们为 RecyclerView 设置或知道 fix item height。这就是答案:

第 1 步: 首先你应该找到你的 RecyclerView 灵活的项目高度,这可以从你的 RecyclerView>onBindViewHolder

holder.itemView.post(new Runnable() {
        @Override
        public void run() {

            int cellWidth = holder.itemView.getWidth();// this will give you cell width dynamically
            int cellHeight = holder.itemView.getHeight();// this will give you cell height dynamically

            dynamicHeight.HeightChange(position, cellHeight); //call your iterface hear
        }
    });

使用此代码,您可以在它们构建时找到您的项目高度,并通过界面将项目高度发送到您的活动。

对于接口有问题的朋友,我在下面留下接口代码,应该写在适配器中。

public interface DynamicHeight {
    void HeightChange (int position, int height);
}

第 2 步: 到目前为止,我们找到了我们的项目高度,现在我们想为我们的 RecyclerView 设置高度。首先我们应该计算项目高度的总和。在这一步中,我们通过 BitMap 执行此操作 第一步 实现 最后一步 interface 并在您的 ActivityFragment 中编写这些代码来定义您的 RecyclerView

@Override
public void HeightChange(int position, int height) {
    itemHeight.put(position, height);
    sumHeight = SumHashItem (itemHeight);

    float density = activity.getResources().getDisplayMetrics().density;
    float viewHeight = sumHeight * density;
    review_recyclerView.getLayoutParams().height = (int) sumHeight;

    int i = review_recyclerView.getLayoutParams().height;
}

int SumHashItem (HashMap<Integer, Integer> hashMap) {
    int sum = 0;

    for(Map.Entry<Integer, Integer> myItem: hashMap.entrySet())  {
        sum += myItem.getValue();
    }

    return sum;
}

第 3 步: 现在我们有了 RecyclerView 高度的总和。对于最后一步,我们应该将我们在上一步中编写的接口发送给适配器,并使用如下代码:

reviewRecyclerAdapter = new ReviewRecyclerAdapter(activity, reviewList, review_recyclerView, this);

当您实现接口时,您应该将它与我使用的上下文一起发送给您的this

好好享受吧

【讨论】:

  • 我想不通...遇到一些错误..您能否提供适配器类和 recyclerview 实现的完整示例
  • @HetalUpadhyay 哪个部分有错误?这周我没有足够的时间来做这件事。但如果你不着急,我会为你做的
  • 实际上我没有收到任何错误,但是当我尝试使用水平回收器查看相同的代码时,它什么也不显示......所以如果你能提供一个在这两种情况下都能正常工作的代码,我将不胜感激水平和垂直滚动
  • @HetalUpadhyay 你能解释一下你想做什么吗?因为我有一些代码来计算有多少项目应该是固定宽度的水平形式
  • @HetalUpadhyay 好的,知道了。我试着在星期五做。给我发电子邮件:ashkan.hematian@gmail.com
【解决方案3】:

将此行用于您的回收站视图:

 android:nestedScrollingEnabled="false"

试试看,recyclerview 会平滑滚动,高度灵活

【讨论】:

  • 仅供记录,可从 API 21 获得
【解决方案4】:

[已解决] 我对水平回收视图有同样的问题。 为 recycleview 更改 Gradle 存储库

 compile 'com.android.support:recyclerview-v7:23.2.1'

写这个:linearLayoutManager.setAutoMeasureEnabled(true);

修复了更新中与各种测量规范方法相关的错误

查看http://developer.android.com/intl/es/tools/support-library/features.html#v7-recyclerview

我发现 23.2.1 库存在问题: 当 item 为 match_parent recycle view 填充完整 item 以查看时,请始终使用min height or "wrap_content".

谢谢

【讨论】:

    【解决方案5】:

    要修复 RecyclerView 中的 fling,您必须在 LinearLayoutManager 中覆盖 canScrollVertically

    linearLayoutManager = new LinearLayoutManager(context) {
     @Override
     public boolean canScrollVertically() {
      return false;
     }
    };
    

    【讨论】:

    • 要添加到这个答案,你还需要使用 NestedScrollView 这个答案单独在 Android 7+ 上不起作用:\
    【解决方案6】:

    我完全同意 Ashkan 的解决方案,非常感谢(投票 +1!),但有一件事......

    如果 RecyclerView 确实在 ScrollView/NestedScrollView 内正确滚动,但它没有抛出(拦截它),那么解决方案是扩展 RecyclerView 并覆盖 OnInterceptTouchEvent 和 OnTouchEvent。如果您不需要任何点击操作,而只想展示具有工作状态的项目,那么只需在两者中都返回 false,如下所示:

    public class InterceptingRecyclerView extends RecyclerView {
    
        public InterceptingRecyclerView(Context context) {
            super(context);
        }
    
        public InterceptingRecyclerView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public InterceptingRecyclerView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent e) {
            return false;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent e) {
            return false;
        }
    }
    

    或者实际上它应该被称为 NonIntercepting... ;) 就这么简单。

    【讨论】:

      【解决方案7】:

      如果为 RecyclerView 设置固定高度对某些人(比如我)不起作用,以下是我添加到固定高度解决方案中的内容:

         mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
          @Override
          public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
              int action = e.getAction();
              switch (action) {
              case MotionEvent.ACTION_MOVE:
                  rv.getParent().requestDisallowInterceptTouchEvent(true);
                  break;
          }
          return false;
      }
      
      @Override
      public void onTouchEvent(RecyclerView rv, MotionEvent e) {
      
      }
      
      @Override
      public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
      
          }
      });
      

      【讨论】:

      • 我还没有测试它,但我认为你的代码想自己滚动 RecyclerView。我今天会试试。 tnx 亲爱的朋友
      • 我没有帮助,正如我之前所说,它有助于以固定高度自行滚动 RecyclerView。这不是我的目标。
      【解决方案8】:

      我通过使用NestedScrollView 而不是ScrollView 解决了这个问题。

      对于RecyclerView

      <android.support.v7.widget.RecyclerView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
      

      在代码中

      recyclerView.setHasFixedSize(true);
      recyclerView.setNestedScrollingEnabled(false);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-29
        • 1970-01-01
        • 1970-01-01
        • 2017-07-06
        • 2018-02-12
        • 1970-01-01
        相关资源
        最近更新 更多