在Android6.0以下的系統(tǒng)上,使用Scrollview嵌套RecycleView時顯示是正常的,而在6.0系統(tǒng)上,卻出現(xiàn)了高度顯示不正常的問題.為此我在網(wǎng)上查了好幾種方法.
1.重寫RecycleView,我是用這種方法解決了問題
/**
* 適配6.0 scrollv嵌套recyclev顯示不全
*/
public class InnerRecycleView extends RecyclerView {
public InnerRecycleView(Context context) {
super(context);
}
public InnerRecycleView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
setNestedScrollingEnabled(false);
}
}
2.在RecycleView外面包含一層RelativeLayout布局
這個方法是在stackoverflow上找到的:
http://stackoverflow.com/questions/27083091/recyclerview-inside-scrollview-is-not-working
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_goods"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
這里要注意的關(guān)鍵屬性是:
android:descendantFocusability="blocksDescendants"
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.Must be one of the following constant values.

api.jpg
該屬性是當(dāng)view獲取焦點(diǎn)時,定義viewGroup和其子控件兩者之間的關(guān)系.
屬性的值有三種:
beforeDescendants: viewgroup會優(yōu)先其子類控件而獲取到焦點(diǎn)
afterDescendants: viewgroup只有當(dāng)其子類控件不需要獲取焦點(diǎn)時才獲取焦點(diǎn)
blocksDescendants: viewgroup會覆蓋子類控件而直接獲得焦點(diǎn)