【问题标题】:Recylerview not visible inside scrollview or nestedScrollview在滚动视图或嵌套滚动视图中不可见 Recyclerview
【发布时间】:2016-01-12 05:07:30
【问题描述】:

我想在 NestedScrollView 中放置一个 RecylerView,如下所示

activity_service_menu.xml

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="HELLO" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

ServiceMenuActivity.java

public class ServiceMenuTActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_menu_t);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        rv.setHasFixedSize(true);
        rv.setAdapter(new RvAdapter());
    }

    private static class RvAdapter extends RecyclerView.Adapter<RvAdapter.RvHolder> {

        @Override
        public RvHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View serviceMenuItemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_service_menu, parent, false);
            return new RvHolder(serviceMenuItemView);
        }

        @Override
        public void onBindViewHolder(RvHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 100;
        }

        public static class RvHolder extends RecyclerView.ViewHolder {

            public RvHolder(View itemView) {
                super(itemView);
            }
        }
    }

}

我已将 linearLayout 放在 scrollView 和 nestedScrollView 中。 但是 RecyclerView 是不可见的。如果我将 ScrollView 替换为 FrameLayout 或任何其他布局,则 RecyclerView 是可见的。

我想使用 nestedScrollView 并在滚动 recyclerView 时滚动整个布局。不幸的是 recyclerView 甚至不可见。

【问题讨论】:

  • 你真的需要LinearLayout 作为RecyclerViewNestedScrollView 中的父级吗?
  • 没有。由于 ScrollView 只接受 1 个孩子,我将 LinearLayout 作为包装器。我需要 TextView 下面的 RecyclerView,当 RecyclerView 滚动时,整个视图应该滚动。
  • 你能描述一下你想要什么样的行为吗?比如折叠工具栏或其他任何东西。
  • 不确定它是否有效,但尝试将固定高度设置为 recyclerView 而不是 wrap_content
  • @Apurva:修复了 recyclerView 的高度。对于提出这个问题的人.. 有关在 scrollView 中使用 nestedScrollView 的更多信息:link

标签: android android-layout scrollview android-recyclerview nestedscrollview


【解决方案1】:

按照此示例将了解您在哪里做错了。 主线是:android:fillViewport="true"。

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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


        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">


        <com.app.view.CustomRecyclerView
            android:id="@+id/recycler_movie_suggestion"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </android.support.v7.widget.CardView>

    </LinearLayout>

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

【讨论】:

  • 你先生真了不起。似乎在 NestedScrollView 上设置 fillViewport="true" 可以解决问题。谢谢!
  • 这是唯一对我有用的解决方案,我从相关问题中尝试了每一个,但没有一个有效。
【解决方案2】:

当您在彼此内部使用两个可滚动元素时,您就陷入了困境!您必须计算 Recycler 项目的高度,然后找到整个回收器的高度。看看下面的链接,我完全解释了这个问题。

Use RecyclerView insdie ScrollView with flexible recycler item height

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多