欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android仿微博個人詳情頁滾動到頂部的實例代碼

 更新時間:2019年05月26日 14:02:00   作者:xing_star  
這篇文章主要介紹了Android仿微博個人詳情頁滾動到頂部的實例代碼,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒家,需要的朋友可以參考下

個人詳情頁滑動到頂部

最近產品提了個新需求,需要實現點擊App內的某個按鈕跳轉到個人詳情頁并且滑動到頂部,個人詳情頁的頁面交互稍微復雜,技術角度上包含了狀態(tài)欄顏色變換,view滑動聯(lián)動等問題,技術實現上采用了Google出的CoordinatorLayout那套組件,由于App的個人詳情頁跟微博的相似,這里就拿微博為例來描述。微博默認的效果以及手動滑動到頂部的效果圖如下。

個人詳情頁技術實現分析:

先看看xml布局的偽代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff6f6f6">
    <android.support.design.widget.CollapsingToolbarLayout
      android:id="@+id/toolbar_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:contentScrim="@color/transparent"
      app:layout_scrollFlags="scroll|exitUntilCollapsed"
      app:toolbarId="@+id/toolbar">
      <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:visibility="invisible"
        android:background="@color/white"
        app:layout_collapseMode="pin">
      </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
    <android.support.design.widget.TabLayout
      android:id="@+id/gift_tab"
      style="@style/CustomerTabLayout"
      android:layout_width="match_parent"
      android:layout_height="45dp"
      app:tabGravity="center"
      app:tabMode="scrollable" />
    <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="#f2f2f2" />
  </android.support.design.widget.AppBarLayout>
  <android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>

整個結構上分為兩部分,AppBarLayout(里面包含TabLayout),ViewPager,根節(jié)點是CoordinatorLayout。上下滑動會引起AppBarLayout的聯(lián)動,懸浮在頂部,或者是跟著viewPager一起滑動以及視差效果之類的。目前我們要實現的是,在進入當前頁面時,強制讓AppBarLayout滑動到頂部,使toolbar懸浮固定不動。那么該怎么做呢,一種思路是在onCreate()方法中,發(fā)post任務,頁面渲染結束后,執(zhí)行post任務,post的任務是調用AppBarLayout的API方法,讓AppBarLayout往上滑。

appBarLayout.post(() -> {
  //...具體的滑動邏輯
});

操作AppBarLayout滑動的是對應的Behavior。在CoordinatorLayout這套組件里面體現的淋漓盡致。感興趣的可以好好分析下CoordinatorLayout是如何完成事件分發(fā)的,如何讓子view相互聯(lián)動的。

這里具體的滑動邏輯偽代碼為:

CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
if (behavior instanceof AppBarLayout.Behavior) {
  AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
  if (mCollapsingHeight != 0) {
    appBarLayoutBehavior.setTopAndBottomOffset(-Math.abs(mCollapsingHeight));
  }
}

我們將appBarLayout向上滑動了mCollapsingHeight的高度,那么這個高度值是如何計算出來的呢。這個值,實際上是在最開始做個人詳情頁這個需求就已經得出的值。

mCollapsingHeight = appBarLayout.getHeight() - toolbar.getHeight() - DisplayUtils.dip2px(46);

這個值需要結合頁面布局來計算,我們的頁面布局兩部分中,最上面的是appBarLayout,規(guī)定的是距離靠近toolbar的高度就產生漸變,toolbar開始固定位置,那么就需要按照這個公式計算mCollapsingHeight。

完整的代碼如下:

private void initView() {
  appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
    mCollapsingHeight = appBarLayout.getHeight() - toolbar.getHeight() - DisplayUtils.dip2px(46);
  });
  if (scrollType != 0) {
    appBarLayout.post(() -> {
      CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
      if (behavior instanceof AppBarLayout.Behavior) {
        AppBarLayout.Behavior appBarLayoutBehavior = (AppBarLayout.Behavior) behavior;
        if (mCollapsingHeight != 0) {
          appBarLayoutBehavior.setTopAndBottomOffset(-Math.abs(mCollapsingHeight));
        }
      }
    });
  }
}

個人詳情頁相關:

在Github上找到了一個仿微博個人詳情頁的開源項目,https://github.com/whisper92/WeiboProfile,技術實現上采用的是ScrollView,ListView,部分代碼可以看看。

總結

以上所述是小編給大家介紹的Android仿微博個人詳情頁滾動到頂部的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!

相關文章

  • Android編程實現通話錄音功能的方法

    Android編程實現通話錄音功能的方法

    這篇文章主要介紹了Android編程實現通話錄音功能的方法,結合實例形式較為詳細的分析了Android廣播接收機制實現錄音功能的操作技巧,需要的朋友可以參考下
    2017-06-06
  • Kotlin集合List Set Map使用介紹詳解

    Kotlin集合List Set Map使用介紹詳解

    集合是可變數量(可能為0)的一組條目,kotlin標準庫提供一個整套用于集合管理的工具,各種集合對于解決問題都具有重要意義,并且經常用到。kotlin中的集合與Java基本類似
    2022-09-09
  • Android 獲取手機信息實例詳解

    Android 獲取手機信息實例詳解

    這篇文章主要介紹了Android 獲取手機信息實例詳解的相關資料,這里附有實例代碼及實現效果圖,需要的朋友可以參考下
    2017-01-01
  • Android實現商品展示效果

    Android實現商品展示效果

    這篇文章主要為大家詳細介紹了Android實現商品展示效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Android圖片壓縮以及優(yōu)化實例

    Android圖片壓縮以及優(yōu)化實例

    本篇文章主要介紹了Android圖片壓縮以及優(yōu)化實例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Android狀態(tài)欄微技巧(推薦)

    Android狀態(tài)欄微技巧(推薦)

    這篇文章主要介紹了Android狀態(tài)欄微技巧的相關資料,非常不錯,具有參考借鑒價值,需要的朋友一起學習吧
    2016-12-12
  • Android實現長按back鍵退出應用程序的方法

    Android實現長按back鍵退出應用程序的方法

    這篇文章主要介紹了Android實現長按back鍵退出應用程序的方法,實例分析了Android按鈕事件的操作技巧,需要的朋友可以參考下
    2015-05-05
  • Android性能優(yōu)化全局異常處理詳情

    Android性能優(yōu)化全局異常處理詳情

    這篇文章主要介紹了Android性能優(yōu)化全局異常處理詳情,文章圍繞主題展開詳細的內容協(xié)商,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-08-08
  • Android?Settings?跳轉流程方法詳解

    Android?Settings?跳轉流程方法詳解

    這篇文章主要為大家介紹了Android?Settings跳轉流程方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • Android 優(yōu)化之存儲優(yōu)化的實現

    Android 優(yōu)化之存儲優(yōu)化的實現

    這篇文章主要介紹了Android 優(yōu)化之存儲優(yōu)化的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07

最新評論