在ScrollView中使用RecyclerView,灵活的Recycler项目高度

Ash*_*kan 18 android scrollable android-scrollview android-recyclerview

我想知道有没有可能的方法来使用RecyclerView

在此之前,我在ScrollView中使用具有修复高度的 RecyclerView,但这次我不知道项目的高度.

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

更新: 一些解决方案学习如何自己滚动RecyclerView但我想展示这个扩展.

Bhu*_*aba 14

如果您只想滚动,那么您可以使用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>
Run Code Online (Sandbox Code Playgroud)


Ash*_*kan 12

我在世界各地寻找回答这个问题,但我没有找到这个流行问题的直接答案.最后我把一些技巧混合起来解决了这个问题!

我的问题开始于我的老板让我在ScrollView中使用RecyclerView,并且你知道我们不能在彼此内部使用两个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
        }
    });
Run Code Online (Sandbox Code Playgroud)

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

对于那些有接口问题的朋友我留下接口代码,应该写在Adapter中.

public interface DynamicHeight {
    void HeightChange (int position, int height);
}
Run Code Online (Sandbox Code Playgroud)

第2步: 到目前为止我们找到了物品高度,现在我们要将高度设置为我们的RecyclerView.首先我们应该计算物品高度的总和.在这一步中,我们通过BitMap 首先实现最后一步的接口并在下面的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;
}
Run Code Online (Sandbox Code Playgroud)

第3步: 现在我们得到您的RecyclerView高度的总和.对于最后一步,我们应该将我们在最后一步中编写的接口发送到适配器,并使用以下代码:

reviewRecyclerAdapter = new ReviewRecyclerAdapter(activity, reviewList, review_recyclerView, this);
Run Code Online (Sandbox Code Playgroud)

当你实现接口,你应该与你的情况下,我用它发送给你这个.

好好享受


iDe*_*per 9

将此行用于您的recyclerview:

 android:nestedScrollingEnabled="false"
Run Code Online (Sandbox Code Playgroud)

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


Ami*_*mit 8

[已解决]我对Horizo​​ntal recycleview也有同样的问题.更改Gradle repo for recycleview

 compile 'com.android.support:recyclerview-v7:23.2.1'
Run Code Online (Sandbox Code Playgroud)

写这个: linearLayoutManager.setAutoMeasureEnabled(true);

修复了更新中与各种measure-spec方法相关的错误

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

我发现了23.2.1库的问题:当item为match_parent回收视图填充完整项目时,请随时查看 min height or "wrap_content".

谢谢


Mic*_*łek 6

要解决一扔中RecyclerView必须覆盖canScrollVerticallyLinearLayoutManager:

linearLayoutManager = new LinearLayoutManager(context) {
 @Override
 public boolean canScrollVertically() {
  return false;
 }
};
Run Code Online (Sandbox Code Playgroud)