商品詳情頁RecyclerView與TabLayout的聯(lián)動定位

堅持本心

要考慮兩方面的交互:
1.RecyclerView滾動到一定位置硼一,TabLayout需要指示到對應(yīng)的選項
2.TabLayout點擊對應(yīng)的選項菜單遵倦,RecyclerView要滾動到指定位置

  • 由于系統(tǒng)原生TabLayout無法滿足指示器圓角且和字一樣寬的需求,所以我的TabLayout使用的是 MagicIndicator

1.監(jiān)聽RecyclerView的滾動事件

        //滾動顯示頂部菜單欄额嘿,onScrollStateChanged執(zhí)行優(yōu)先于onScrolled方法
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_IDLE://滾動停止
                        if (isClickTab)myHandler.sendEmptyMessage(1);
                        else myHandler.sendEmptyMessage(0);
                        break;
                    case RecyclerView.SCROLL_STATE_DRAGGING://手指 拖動
                        break;
                    case RecyclerView.SCROLL_STATE_SETTLING://慣性滾動
                        break;
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                scrollHeight += dy; //滑動的距離
                //滾動距離和導(dǎo)航欄高度算出透明度瘸恼,實現(xiàn)上滑隱藏,下滑漸現(xiàn)
                float alp = (float) scrollHeight / (float) DpPxUtils.dp2px(80);
                mBinding.layoutMenu.setAlpha(alp);
            }
        });

當(dāng)RecyclerView滾動停止時:我們使用Handler來進行通知TabLayout來選定對應(yīng)選項册养。同時我們還通過isClickTab參數(shù)來屏蔽掉點擊TabLayout導(dǎo)致的滾動东帅,其中的myHandler為:

    class MyHandler extends Handler {
        private WeakReference<Activity> reference;
        public MyHandler(Activity activity) {
            reference = new WeakReference<Activity>(activity);
        }
        @Override
        public void handleMessage(Message msg) {
            if (reference.get() != null) {
                switch (msg.what) {
                    case 0:
                        //停止位置為【商品】
                        if (scrollHeight <= topBannerAndInfoHeight) {
                            mBinding.magicIndicator.onPageSelected(0);
                        //停止位置為【詳情】
                        } else if (scrollHeight > topBannerAndInfoHeight && scrollHeight <= topGoodsPicHeight) {
                            mBinding.magicIndicator.onPageSelected(1);
                        //停止位置為【喜歡】
                        } else {
                            mBinding.magicIndicator.onPageSelected(2);
                        }
                        break;
                    case 1://如果是點擊的tab,不重新選擇選項卡
                        isClickTab=false;
                        break;
                }
            }
        }
    }

這里我們使用弱引用的方式創(chuàng)建Handler 對象,避免內(nèi)存泄露球拦。其中的

  • magicIndicator:頂部指示器靠闭。
  • topBannerAndInfoHeight:商品高度
  • topGoodsPicHeight:詳情高度

2.點擊TabLayout讓RecyclerView滾動到指定位置

        //tabLayout和RecyclerView聯(lián)動事件
        String[] names = new String[]{"商品", "詳情", "喜歡"};
        TabCreateUtils.setWhiteTab(_mActivity, magicIndicator, names, index -> {
            if (scrollHeight == 0) return;
            isClickTab=true;
            switch (index) {
                case 0://商品
                    mBinding.recyclerView.smoothScrollToPosition(0);
                    break;
                case 1://詳情
                    int y = topBannerAndInfoHeight - scrollHeight;
                    mBinding.recyclerView.smoothScrollBy(0, y);
                    break;
                case 2://推薦
                    mBinding.recyclerView.smoothScrollToPosition(3);
                    break;
            }
        });

由于magicIndicator創(chuàng)建指示器包含了固定的大量代碼,我封裝了一個工具類來創(chuàng)建坎炼,并把點擊事件傳遞出來愧膀。TabCreateUtils工具類如下:

//菜單指示器創(chuàng)建工具類
public class TabCreateUtils {
    public interface onTitleClickListener{
        void onTitleClick(int index);
    }
    public static void setWhiteTab(Context context,MagicIndicator magicIndicator, String[] tabNames ,onTitleClickListener listener) {
        FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return tabNames == null ? 0 : tabNames.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SelectBigPagerTitleView colorTransitionPagerTitleView = new SelectBigPagerTitleView(context);
                colorTransitionPagerTitleView.setNormalColor(ContextCompat.getColor(context, R.color.white));
                colorTransitionPagerTitleView.setSelectedColor(ContextCompat.getColor(context, R.color.white));
                colorTransitionPagerTitleView.setText(tabNames[index]);
                colorTransitionPagerTitleView.setOnClickListener(view -> {
                    mFragmentContainerHelper.handlePageSelected(index);
                    if (listener!=null)listener.onTitleClick(index);
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                indicator.setColors(ContextCompat.getColor(context, R.color.white));
                indicator.setRoundRadius(3);
                return indicator;
            }
        });
        commonNavigator.setAdjustMode(true);
        magicIndicator.setNavigator(commonNavigator);
        mFragmentContainerHelper.attachMagicIndicator(magicIndicator);
    }
}

其中SelectBigPagerTitleView就是讓選中的字體變大,代碼如下:

public class SelectBigPagerTitleView extends ColorTransitionPagerTitleView {
    public SelectBigPagerTitleView(Context context) {
        super(context);
    }
    @Override
    public void onSelected(int index, int totalCount) {
       setTextSize(16);
    }
    @Override
    public void onDeselected(int index, int totalCount) {
        setTextSize(14);
    }
}

布局文件也很簡單谣光,就是頂部一坨菜單,底部一坨操作檩淋,中間就是RecyclerView


image.png

最后的效果圖:


滾動到指定位置, tab變

原文鏈接:http://www.reibang.com/p/5df93040e126

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末萄金,一起剝皮案震驚了整個濱河市蟀悦,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌氧敢,老刑警劉巖熬芜,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異福稳,居然都是意外死亡涎拉,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門的圆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來鼓拧,“玉大人,你說我怎么就攤上這事越妈〖玖” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵梅掠,是天一觀的道長酌住。 經(jīng)常有香客問我店归,道長,這世上最難降的妖魔是什么酪我? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任消痛,我火速辦了婚禮,結(jié)果婚禮上都哭,老公的妹妹穿的比我還像新娘秩伞。我一直安慰自己,他們只是感情好欺矫,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布纱新。 她就那樣靜靜地躺著,像睡著了一般穆趴。 火紅的嫁衣襯著肌膚如雪脸爱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天未妹,我揣著相機與錄音簿废,去河邊找鬼。 笑死教寂,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的执庐。 我是一名探鬼主播酪耕,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼轨淌!你這毒婦竟也來了迂烁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤递鹉,失蹤者是張志新(化名)和其女友劉穎盟步,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體躏结,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡却盘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了媳拴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片黄橘。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖屈溉,靈堂內(nèi)的尸體忽然破棺而出塞关,到底是詐尸還是另有隱情,我是刑警寧澤子巾,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布帆赢,位于F島的核電站小压,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏椰于。R本人自食惡果不足惜怠益,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望廉羔。 院中可真熱鬧溉痢,春花似錦、人聲如沸憋他。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽竹挡。三九已至镀娶,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間揪罕,已是汗流浹背梯码。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留好啰,地道東北人轩娶。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像框往,于是被迫代替她去往敵國和親鳄抒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容