在ScrollView中使用RecyclerView,并设置可调整的Recycler项目高度



我想知道是否有任何可能的方法使用RecyclerView?

在此之前,我在ScrollView中使用了固定高度的recycleview,但这次我不知道项目的高度。

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

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

如果你只想滚动,那么你可以使用to 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>

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

我的问题开始时,我的老板要求我使用RecyclerView内一个ScrollView,如你所知,我们不能使用两个Scrollable对象在彼此除了当我们设置或知道修复项目高度为我们的RecyclerView。这就是答案:

步骤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。首先,我们应该计算项目高度的总和。在这一步中,我们通过BitMapfirst 实现 last step 接口,并在定义RecyclerViewActivityFragment中编写以下代码:

@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:

 android:nestedScrollingEnabled="false"

试一下,recyclerview将平滑滚动,高度灵活

[RESOLVED]我对水平回收视图也有同样的问题。修改Gradle repo

 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库的问题:当项目为match_parent回收视图时,请始终使用min height or "wrap_content".

谢谢

要修复RecyclerView中的fling,必须在LinearLayoutManager中重写canScrollVertically:

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

我完全同意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…;) 就这么简单。

如果为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) {
    }
});

我用NestedScrollView代替ScrollView解决了这个问题。

对于RecyclerView:

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

和Code

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

当我将recycleview放入flexboxlayout中,就像

<androidx.core.widget.NestedScrollView
  <LinearLayout
    <com.google.android.flexbox.FlexboxLayout
      <androidx.recyclerview.widget.RecyclerView />
    </com.google.android.flexbox.FlexboxLayout>
  </LinearLayout>
</androidx.core.widget.NestedScrollView>

android:nestedScrollingEnabled="false"mRecyclerView.setHasFixedSize(true);不工作。

用这段代码我就得到了需要的

recyclerView.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) {
            }
        });

最新更新