吸頂&滑動不卡頓&刷新&狀態(tài)欄CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+Toolbar

效果圖:

image.png

image.png

布局分布:*

image.png

使用第三方:
刷新:

implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'

狀態(tài)欄:StatusBarHeightView

AppBarLayout得behavior(CustomBehavior)


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.appbar.AppBarLayout;

import java.lang.reflect.Field;

/**
 *
 * @describe  自定義behavior  以解決滑動抖動
 *
 */

public class CustomBehavior extends  AppBarLayout.Behavior {
    private OverScroller mScroller;
    public CustomBehavior() {
    }

    public CustomBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
        getParentScroller(context);
    }

    /**
     * 反射獲得滑動屬性。
     *
     * @param context
     */
    private void getParentScroller(Context context) {
        if (mScroller != null) return;
        mScroller = new OverScroller(context);
        try {
            Class<?> reflex_class = getClass().getSuperclass().getSuperclass();//父類AppBarLayout.Behavior  父類的父類   HeaderBehavior
            Field fieldScroller = reflex_class.getDeclaredField("mScroller");
            fieldScroller.setAccessible(true);
            fieldScroller.set(this, mScroller);
        } catch (Exception e) {}
    }
    //fling上滑appbar然后迅速fling下滑recycler時, HeaderBehavior的mScroller并未停止, 會導(dǎo)致上下來回晃動
    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
        if(mScroller!=null){ //當(dāng)recyclerView 做好滑動準備的時候 直接干掉Appbar的滑動
            if (mScroller.computeScrollOffset()) {
                mScroller.abortAnimation();
            }
        }

        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    }

    @Override
    public boolean onTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent e) {

        switch (e.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                break;
        }



        return super.onTouchEvent(parent,child,e);
    }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white_fc">

    <com.dsy.jxih.tools.status.StatusBarHeightView
        android:id="@+id/status"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/zong_db"
        app:use_type="use_padding_top" />

    <LinearLayout
        android:id="@+id/llTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/status"
        android:background="@color/zong_db"
        android:orientation="vertical"
        android:paddingTop="@dimen/dp_7"
        android:paddingBottom="@dimen/dp_7">

        <TextView
            android:id="@+id/tvSearch"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_33"
            android:layout_centerInParent="true"
            android:layout_marginLeft="@dimen/dp_10"
            android:layout_marginRight="@dimen/dp_10"
            android:background="@drawable/hui_20_circle_white_shape_bg"
            android:drawableRight="@mipmap/search_hui"
            android:drawablePadding="@dimen/dp_5"
            android:gravity="center"
            android:paddingLeft="@dimen/dp_15"
            android:paddingRight="@dimen/dp_15"
            android:text="輸入搜索關(guān)鍵詞"
            android:textColor="@color/hui_ffc"
            android:textSize="@dimen/sp_14" />

    </LinearLayout>

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/sRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/llTitle"
        android:background="@color/white_fc">

        <com.dsy.jxih.view.MyHeaderView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/zong_db" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <androidx.coordinatorlayout.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <com.google.android.material.appbar.AppBarLayout
                    android:id="@+id/abLay"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:elevation="@dimen/dp_0"
                    app:layout_behavior=".view.CustomBehavior">

                    <com.google.android.material.appbar.CollapsingToolbarLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/white_fc"
                        app:elevation="@dimen/dp_0"
                        app:expandedTitleGravity="center"
                        app:layout_scrollFlags="scroll|exitUntilCollapsed"
                        app:titleEnabled="false">

                        <View
                            android:id="@+id/bannerBgView"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/dp_70"
                            android:background="@color/zong_db" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">

                            <com.dsy.jxih.view.banner.MZBannerView
                                android:id="@+id/bannerView"
                                android:layout_width="match_parent"
                                android:layout_height="@dimen/dp_133"
                                android:layout_centerInParent="true"
                                android:layout_marginTop="@dimen/dp_7"
                                android:scaleType="centerCrop"
                                app:canLoop="true"
                                app:layout_constraintTop_toTopOf="parent"
                                app:open_mz_mode="true" />

                            <androidx.recyclerview.widget.RecyclerView
                                android:id="@+id/rvMenuList"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginTop="@dimen/dp_12"
                                android:layout_marginBottom="@dimen/dp_16"
                                android:scrollbars="none" />

                            <LinearLayout
                                android:id="@+id/llGridLay"
                                android:layout_width="match_parent"
                                android:layout_height="@dimen/dp_150"
                                android:layout_marginLeft="@dimen/dp_10"
                                android:layout_marginRight="@dimen/dp_10"
                                android:layout_marginBottom="@dimen/dp_16"
                                android:orientation="horizontal">

                                <androidx.cardview.widget.CardView
                                    android:layout_width="@dimen/dp_0"
                                    android:layout_height="match_parent"
                                    android:layout_marginRight="@dimen/dp_2_5"
                                    android:layout_weight="1"
                                    app:cardCornerRadius="@dimen/dp_5"
                                    app:cardElevation="@dimen/dp_0">

                                    <ImageView
                                        android:id="@+id/ivLeft"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:adjustViewBounds="true"
                                        android:scaleType="centerCrop"
                                        android:src="@mipmap/ic_image_2" />

                                </androidx.cardview.widget.CardView>

                                <LinearLayout
                                    android:layout_width="@dimen/dp_0"
                                    android:layout_height="match_parent"
                                    android:layout_marginLeft="@dimen/dp_2_5"
                                    android:layout_weight="1"
                                    android:orientation="vertical">

                                    <androidx.cardview.widget.CardView
                                        android:layout_width="match_parent"
                                        android:layout_height="@dimen/dp_0"
                                        android:layout_marginBottom="@dimen/dp_2_5"
                                        android:layout_weight="1"
                                        app:cardCornerRadius="@dimen/dp_5"
                                        app:cardElevation="@dimen/dp_0">

                                        <ImageView
                                            android:id="@+id/ivTop"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:scaleType="centerCrop"
                                            android:src="@mipmap/ic_image_7" />

                                    </androidx.cardview.widget.CardView>

                                    <androidx.cardview.widget.CardView
                                        android:layout_width="match_parent"
                                        android:layout_height="@dimen/dp_0"
                                        android:layout_marginTop="@dimen/dp_2_5"
                                        android:layout_weight="1"
                                        app:cardCornerRadius="@dimen/dp_5"
                                        app:cardElevation="@dimen/dp_0">

                                        <ImageView
                                            android:id="@+id/ivBottom"
                                            android:layout_width="match_parent"
                                            android:layout_height="match_parent"
                                            android:scaleType="centerCrop"
                                            android:src="@mipmap/ic_image_7" />

                                    </androidx.cardview.widget.CardView>

                                </LinearLayout>

                            </LinearLayout>

                        </LinearLayout>

                    </com.google.android.material.appbar.CollapsingToolbarLayout>

                    <com.google.android.material.tabs.TabLayout
                        android:id="@+id/tabLay"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/dp_35"
                        android:background="@color/white_fc"
                        android:elevation="@dimen/dp_0"
                        app:tabBackground="@android:color/transparent"
                        app:tabIndicator="@drawable/indicator"
                        app:tabIndicatorFullWidth="false"
                        app:tabMode="scrollable"
                        app:tabPadding="-1dp"
                        app:tabRippleColor="@android:color/transparent"
                        app:tabSelectedTextColor="@color/red_f3"
                        app:tabTextAppearance="@style/TabLayoutTextSize"
                        app:tabTextColor="@color/black_3" />

                </com.google.android.material.appbar.AppBarLayout>

                <androidx.core.widget.NestedScrollView
                    android:id="@+id/scroView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/white_fc"
                    android:paddingTop="@dimen/dp_10"
                    app:layout_anchorGravity="bottom"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/rvList"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="@dimen/dp_5"
                        android:layout_marginRight="@dimen/dp_5"
                        android:nestedScrollingEnabled="false" />

                </androidx.core.widget.NestedScrollView>

            </androidx.coordinatorlayout.widget.CoordinatorLayout>

        </LinearLayout>

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white_fc" />

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>


</RelativeLayout>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌峰搪,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異赖捌,居然都是意外死亡,警方通過查閱死者的電腦和手機矮烹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門越庇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人奉狈,你說我怎么就攤上這事卤唉。” “怎么了仁期?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵桑驱,是天一觀的道長竭恬。 經(jīng)常有香客問我,道長熬的,這世上最難降的妖魔是什么痊硕? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮押框,結(jié)果婚禮上岔绸,老公的妹妹穿的比我還像新娘。我一直安慰自己橡伞,他們只是感情好亭螟,可當(dāng)我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著骑歹,像睡著了一般预烙。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上道媚,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天扁掸,我揣著相機與錄音,去河邊找鬼最域。 笑死谴分,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的镀脂。 我是一名探鬼主播牺蹄,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼薄翅!你這毒婦竟也來了沙兰?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤翘魄,失蹤者是張志新(化名)和其女友劉穎鼎天,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體暑竟,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡斋射,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了但荤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片罗岖。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖腹躁,靈堂內(nèi)的尸體忽然破棺而出桑包,到底是詐尸還是另有隱情,我是刑警寧澤潜慎,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布捡多,位于F島的核電站蓖康,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏垒手。R本人自食惡果不足惜蒜焊,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望科贬。 院中可真熱鬧泳梆,春花似錦、人聲如沸榜掌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽憎账。三九已至套硼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間胞皱,已是汗流浹背邪意。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留反砌,地道東北人雾鬼。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像宴树,于是被迫代替她去往敵國和親策菜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,779評論 2 354