在ScrollView或NestedScrollView中RecyclerView不可见

7
我想在NestedScrollView中放置一个RecyclerView,如下所示:
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不可见。如果我用FrameLayout或其他任何布局替换ScrollView,那么RecyclerView就可见。
我想使用nestedScrollView,当滚动RecyclerView时滚动整个布局。不幸的是,RecyclerView甚至不可见。

你真的需要在NestedScrollView中将RecyclerView的父视图设置为LinearLayout吗? - Vipul Asri
不行。由于 ScrollView 只接受一个子项,我已经将 LinearLayout 作为包装器。我需要在 TextView 下面放置 RecyclerView,当 RecyclerView 滚动时,整个视图应该滚动。 - Narendra
你能基本描述一下你想要的行为吗?比如折叠式工具栏或其他什么的。 - Vipul Asri
不确定是否有效,但可以尝试将recyclerView的高度设置为固定值,而不是wrap_content。 - Apurva
@Apurva:修复recyclerView的高度问题了。对于那些来到这个问题的人们,关于在scrollView中使用nestedScrollView的更多信息,请参考以下链接:link - Narendra
2个回答

14

按照这个示例做将会明白你出了什么错误。主要代码是: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>

2
您真是太棒了。在NestedScrollView上设置fillViewport="true"似乎解决了问题。谢谢! - Pkmmte
这是唯一一个对我有效的解决方案,我尝试了相关问题中的每一个,但都没有起作用。 - UrielUVD

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接