Android 巢狀布局

2021-07-25 04:23:50 字數 1234 閱讀 2333

android 巢狀布局

簡介:

1. nestedscrollingparent介面

① onstartnestedscroll(view child, view target, int nestedscrollaxes):當nestedchild想要進行巢狀滾動時,會呼叫nestedparent的這個方法。這個芳法用於指示是否支援巢狀滾動,比如我們只想支援垂直方向上的巢狀滾動,可以在nestedparent中這樣實現這個方法:
@override

public

boolean

onstartnestedscroll(view child, view target, int nestedscrollaxes)

return

false;

}

② onnestedprescroll(view target, int dx, int dy, int consumed)

當我們滾動nestedchild時,nestedchild進行實際的滾動前,會先呼叫nestparent的這個方法。nestedparent在這個方法中可以把子view想要滾動的距離消耗掉一部分或是全部消耗,比如我們的例子中,當我們向上滾動nestedchild時,nestparent會搶在它前頭先滾動,直到imageview完全隱藏,才讓nestedchild開始滾動

在onnestedprescroll()方法中,引數dy代表了本次nestedscrollview想要滑動的距離。若我們向上滑動nestedscrollview,dy就是正的,向下就是負的。getscorlly()會返回parentview的mscrolly引數,為正則表示當前parentview的內容已經向上滾動了一段距離,否則表示向下滾動過一段距離。

nestedscrollingchild介面

① startnestedscroll(int axes)

開始沿著引數中指定的方向(水平 or 垂直)進行巢狀滾動

② dispatchnestedprescroll(…)

這個方法會呼叫nestedparent的onnestedprescroll()方法。這樣就使得nestedparent有機會搶在nestedscroll之前消耗滾動事件

文章推薦:

Android 布局巢狀

本來用relativelayout的 布局方式就很靈活,但是還是有寫需求達不到。只能通過布局巢狀的方式來實現。首先activity main採用的是relativelayout 布局方式,然後通過include標籤引入乙個linearlayout的布局檔案。建乙個xml檔案,取名twobtn.xml...

android布局學習 巢狀布局

採用巢狀的方式來實現複雜的布局,通過2個示例來介紹巢狀布局的方法。示例1 將activity介面分成上 下2部分,然後上部分是用橫向的 水平 布局,裡面有4個textview 下部分則是用縱向的 垂直 布局,也放有4個textview。要實現這樣的布局必須要使用到巢狀布局。實現步驟 1 首先,最外層...

Android新手之旅 10 巢狀布局

想在介面上放四個按鈕,以2 2的形式存在,卻沒有辦法快速實現。嘗試了一下,可以用巢狀的方式,乙個linearlayout巢狀兩個linearlayout,外層的linearlayout用縱向布局,而內部的用橫向布局。xml檔案 android orientation vertical android...