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

Android仿天貓橫向滑動指示器功能的實現(xiàn)

 更新時間:2022年08月08日 10:11:26   作者:xiangzhihong8  
這篇文章主要介紹了Android仿天貓橫向滑動指示器,Android開發(fā)中會有很多很新奇的交互,比如天貓商城的首頁頭部的分類,使用的是GridLayoutManager+橫向指示器實現(xiàn)的,需要的朋友可以參考下

Android開發(fā)中會有很多很新奇的交互,比如天貓商城的首頁頭部的分類,使用的是GridLayoutManager+橫向指示器實現(xiàn)的,效果如下圖。

那對于這種效果要如何實現(xiàn)呢?最簡單的方式就是使用RecyclerView+GridLayoutManager,我們知道RecyclerView可以實現(xiàn)九宮格,接下來就是通過RecyclerView控制指示器的顯示位置,邏輯實現(xiàn)如下:

  1. 計算出RecyclerView劃出屏幕的距離w1和剩余寬度w2的比例y,y = w1 / (總寬度w3 - 可使視區(qū)域?qū)挾葁4)
  2. 計算出指示器該移動的距離w5 = y * (指示器的總寬度w6 - 滑塊寬度w7)
  3. 指示器布局,并控制位置

首先,我們創(chuàng)建兩個drawable文件,分別用于表示指示器的默認背景和選中的背景。
indicator_bg_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp" />
    <solid android:color="@color/gray_9" />
</shape>

indicator_bg_select.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <solid android:color="@color/red"/>
</shape>

然后,我們再添加一個布局,上面是RecyclerView,下面是指示器。

 <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/recycler_view"
         android:layout_width="0dp"
         android:layout_height="@dimen/dp_120"
         android:layout_marginBottom="@dimen/dp_10"/>

  <RelativeLayout
        android:id="@+id/rl_indicator"
        android:layout_width="60dp"
        android:layout_height="4dp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/indicator_bg_normal">
        <View
            android:id="@+id/main_line"
            android:layout_width="30dp"
            android:layout_height="4dp"
            android:layout_centerVertical="true"
            android:background="@drawable/indicator_bg_select"/>
  </RelativeLayout>

接下來,就是通過監(jiān)聽RecyclerView的橫向滾動的距離,來判斷指示器顯示的位置,代碼如下。

 rvMenu.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int range=0;
                int temp = rvMenu.computeHorizontalScrollRange();
                if (temp > range) {
                    range = temp;
                }
                //滑塊的偏移量
                int offset = rvMenu.computeHorizontalScrollOffset();
                //可視區(qū)域長度
                int extent = rvMenu.computeHorizontalScrollExtent();
                //滑出部分在剩余范圍的比例
                float proportion = (float) (offset * 1.0 / (range - extent));
                //計算滾動條寬度
                float transMaxRange = rlIndicator.getWidth() - mainLine.getWidth();
                //設(shè)置滾動條移動
                mainLine.setTranslationX(transMaxRange * proportion);
            }
        });

到此這篇關(guān)于Android仿天貓橫向滑動指示器功能的實現(xiàn)的文章就介紹到這了,更多相關(guān)Android橫向滑動指示器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論