在android5.0上并没有发现这个问题,是在android6.0模拟器上发现的问题,因为项目在5.0运行没有问题,被我搬到6.0之后就出现了这个问题。参考了网上许多资料,找到了一个可以解决的办法,就是将NestedScrollView的高度设置为match_parent,并加上android:fillViewport="true",这样会过后会发现RecycleView的所有item全部可以显示出来了。但是这并不我想要的,因为我还有与RecycleView同级的控件需要显示,当然NestedScollView只允许有一个子控件,在RecycleView还要嵌套一层父控件(这里我用的是ConstraintLayout)。问题是与RecycleView同级的控件在上述设置以后无法显示了,实在发现不了问题在哪里。由于我就给ConstraintLayout设置了一个paddingBottom,结果终于显示出来了,但是这还不是我想要的,因为最底下的控件和底部之间的距离太大了,而且paddingBottom如果设置小于20dp,底部子控件还是会看不到。后来,感谢主,我通过AndroidStudio的preview(不需要运行模拟器就可以查看布局文件的效果)边看视图边修改布局文件,发现我把NestedScrollView和ConstrintLayout(RecycleView和其平级控件的父控件)的margin统统去掉,结果在preview里面看到了RecycleView的item正常显示了(如果RecycleView正常显示的话应该有9条,否则就只有一条),再给NestedScrollView设置padding一样没有问题,而且底部的子控件也可以正常显示。注意哦,我在NestedScrollView内没有设置fillViewport,也没有将高度定义为march_partent,而是自适应高度,wrap_content。
代码轮廓:
<android.support.v4.widget.NestedScrollView
android:id="@+id/goods_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:id="@+id/goods_constraint_02"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/goods_rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.constraint.ConstraintLayout
android:id="@+id/goods_constraint_shopping"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_margin="10dp"
app:layout_constraintTop_toBottomOf="@id/goods_rv"
app:layout_constraintRight_toRightOf="@id/goods_constraint_02">
<ImageView
android:id="@+id/goods_shopping"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/shopping" />
<TextView
android:id="@+id/goods_num"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@color/cart"
android:gravity="center"
android:text="0"
android:textColor="@android:color/white"
android:textSize="15sp"
app:layout_constraintRight_toRightOf="@id/goods_constraint_shopping"/>
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
有可能我说的还有问题,以上仅提供参考,希望对大家有帮助。