android:滚动视图中的 RecyclerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25515904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:31:21  来源:igfitidea点击:

android: RecyclerView inside a ScrollView

androidviewpagerindicatorandroid-recyclerview

提问by nomongo

I have a RecyclerViewwrapped in a LinearLayoutand it works perfectly as expected. I can see all the data in the RecyclerViewas populated. So far so good.

我有一个RecyclerView包裹在 a 中LinearLayout,它按预期完美运行。我可以看到RecyclerView填充的所有数据。到现在为止还挺好。

When I wrap the LinearLayoutin a ScrollView, the RecyclerViewgoes blank. I do not see anything inside RecyclerView. Why? How to make this work.

当我将LinearLayouta包裹起来时ScrollView,它RecyclerView变为空白。我看不到里面的任何东西RecyclerView。为什么?如何使这项工作。

The page is one of the tabs in a ViewPagerIndicator, so everything in that tab needs to be in a ScrollView.

该页面是 a 中的选项卡之一ViewPagerIndicator,因此该选项卡中的所有内容都需要在ScrollView.

Thanks for all the help.

感谢所有的帮助。

回答by Sathesh

Set this property for the ScrollView,

为 ScrollView 设置此属性,

 android:fillViewport="true"

ScrollView will extend itself to fill the contents

ScrollView 将自身扩展以填充内容

回答by sergej shafarenka

After checking implementation, the reason appears to be the following. If RecyclerViewgets put into a ScrollView, then during measure step its height is unspecified (because ScrollViewallows any height) and, as a result, gets equal to minimum height (as per implementation) which is apparently zero.

检查实施后,原因似乎如下。如果RecyclerView被放入 a ScrollView,那么在测量步骤中,它的高度是未指定的(因为ScrollView允许任何高度),因此,它等于最小高度(根据实现),这显然为零。

You have couple of options for fixing this:

您有几个选项可以解决这个问题:

  • Set a certain height to RecyclerView
  • Set ScrollView.fillViewportto true
  • Or keep RecyclerViewoutside of ScrollView. I my opinion, this is the best option by far. If RecyclerViewheight is not limited - which is the case when it's put into ScrollView- then all Adapter's views have enough place vertically and get created all at once. There is no view recycling anymore which kinda breaks the purpose of RecyclerView.
  • 设置一定的高度 RecyclerView
  • 设置ScrollView.fillViewporttrue
  • 或保持RecyclerViewScrollView. 我认为,这是迄今为止最好的选择。如果RecyclerView高度不受限制——当它被放入时就是这种情况ScrollView——那么所有适配器的视图都有足够的垂直位置并一次性创建。不再有视图回收,这有点违反RecyclerView.

回答by neustart47

Nothing helped me except this:

除了这个,没有什么能帮助我:

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) {

    }
});

I got this answer there. Thank you Piyush Guptafor that.

我在那里得到了这个答案。感谢Piyush Gupta

回答by iDeveloper

Hope this helps :

希望这可以帮助 :

Add this line to your recyclerView xml :

将此行添加到您的 recyclerView xml :

android:nestedScrollingEnabled="false"

Try it ,recyclerview will be smoothly scrolled with flexible height inside scrollview .

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