Android 6.0使用Scrollview嵌套RecycleView時顯示不全問題

在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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評論 25 709
  • 出現(xiàn)嵌套問題,主要是事件的分發(fā)機(jī)制及事件沖突,以下列舉各種沖突及相關(guān)的解決辦法。 一、ScrollView嵌套Li...
    黃海佳閱讀 1,200評論 6 11
  • EnglishVersion ->_->:https://github.com/jiang111/awesome-...
    聶順閱讀 1,317評論 0 4
  • 滄海生紅日, 云霞動遠(yuǎn)空。 水光波淼淼, 松柏色盈盈。 一夢關(guān)山遠(yuǎn), 只身渭水行。 扶欄凝視久, 埋沒萬方雄。
    李緩之閱讀 518評論 26 63
  • kabin閱讀 171評論 0 1

友情鏈接更多精彩內(nèi)容