【问题标题】:RecyclerView height wrap_content calculation is incorrectRecyclerView高度wrap_content计算不正确
【发布时间】:2016-11-13 14:27:39
【问题描述】:

我想要我的 RecyclerViewwrap_content。我不想在 RecyclerView 内进行任何滚动,它应该调整到内部孩子的高度。我不希望我的父 ScrollView 滚动我的活动内容。

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <!-- scrolls a little bit as RecyclerView goes slightly down beyond the screen -->
  <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <!-- still scrolls inside -->
      <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

填充 RecyclerView:

myAdapter = new MyAdapter();
layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(myAdapter);

我使用 RecyclerView 库,应该修复 wrap_content 问题:

dependencies {
    compile 'com.android.support:recyclerview-v7:25.0.0'
}

基本上 RecyclerView 的高度计算对我来说并不适用。 RecyclerView 仍然有它自己的滚动,并且 ScrollView 也有一点滚动。如果我尝试将一些可笑的 RecyclerView 高度设置为 1000dp,使其大于项目的总高度,则滚动会根据需要工作,例如RecyclerView 不滚动,ScrollView 滚动带有所有 RecyclerView 项的活动。

那么我做错了什么? :)

【问题讨论】:

  • 这在 v25 之前有效吗?
  • 请通过此链接找到解决方案enter link description here
  • @natario afaik >v23 中存在错误,但已修复且 wrap_content 应按预期工作。
  • @KiranNemani 我不想在 RecyclerView 中禁用滚动。我希望它包装它的内在孩子的内容。

标签: android android-recyclerview android-scrollview android-wrap-content


【解决方案1】:

我只需要使用 android.support.v4.widget.NestedScrollView 而不是 ScrollView。

【讨论】:

    【解决方案2】:

    在这种情况下,您需要升级您的 recyclerview gradle 版本,如果您使用的是 23.0.1,请将其升级为 23.1.1 或更高版本。在最新的 gradle 更新中,google 已将 wrap_content 属性提供给 recyclerview。

    【讨论】:

    • 默认recyclerview带有滚动功能。我从您的问题中了解到的是您的 recyclerview 不应该滚动,并且您需要使用 wrap_content 选项调整 recyclerview 的大小。是吗 ?最近我在 recyclerview 中遇到了同样的情况 wrap_content 问题,那次我升级了我的 recyclerview gradle 版本。如果您不满意,请告诉我,我会推荐更好的。
    • 嗯,是的,我不希望 recyclerview 滚动,但不是字面意思,我希望它是所有子项的高度(例如 wrap_content),但问题是,包装内容是由于某种原因不为我工作。我从 http 获取项目,将它们填充到适配器中,调用 notifyDataSetChanged()。而且... RecyclerView 不是它的全高,我可以在其中滚动。 Gradle 和 Support 库都是最新的。
    猜你喜欢
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 2011-09-30
    相关资源
    最近更新 更多