android – 在scrollview或nestedScrollview中看不到Recylerview

前端之家收集整理的这篇文章主要介绍了android – 在scrollview或nestedScrollview中看不到Recylerview前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在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不可见.如果我用FrameLayout或任何其他布局替换ScrollView,则可以看到RecyclerView.

我想使用nestedScrollView并在滚动recyclerView时滚动总布局.不幸的是,甚至看不到recyclerView.

解决方法

按照这个样本将了解你做错的地方.
主线是: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>

猜你在找的Android相关文章