androidX 下的Tablayout+ViewPage2+Fragment使用踩坑記錄

tips: 本文是記錄自己一次開發(fā)中遇到的幾個問題`


登錄注冊.png

思路:Tablayout+ViewPage2+Fragment

1. 基本使用

//控件都是通過butterknife綁定的,就不貼了
mViewPage.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        mFragments.add(LoginFragment.newInstance());
        mFragments.add(RegisterContainerFragment.newInstance());
        mViewPage.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getItemCount() {
                return mFragments.size();
            }
        });

        new TabLayoutMediator(mTabLayout, mViewPage, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                TextView textView = new TextView(mActivity);
                textView.setText(titles[position]);
                textView.setTextSize(17);
                textView.setTextColor(Color.WHITE);
                if (position == 0) {
                    textView.setAlpha(1);
                } else {
                    textView.setAlpha(0.3f);
                }
                tab.setCustomView(textView);
            }
        }).attach();

XML布局如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:scaleType="center"
    tools:background="@drawable/bg_login">

    <TextView
        android:id="@+id/appName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="104dp"
        android:text="@string/app_name"
        android:textColor="@color/white"
        android:textSize="36sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorHeight="0dp"
        app:tabPaddingEnd="40dp"
        app:tabPaddingStart="40dp"
        app:tabSelectedTextColor="@color/white"
        app:tabTextColor="@color/white" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginTop="50dp"
        android:layout_marginEnd="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

LoginFragment的XML布局如下

<?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="wrap_content"
    android:background="@color/transparent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="253dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/cl"
            android:layout_width="match_parent"
            android:layout_height="223dp"
            android:alpha="0.6"
            android:background="@drawable/shape_blue01_solid_corner29"
            android:paddingStart="20dp"
            android:paddingEnd="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/et_account"
                style="@style/style_edit_login_register"
                android:layout_marginTop="38dp"
                android:hint="請輸入手機號碼"
                android:inputType="phone"
                android:maxLength="11"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/et_pwd"
                style="@style/style_edit_login_register"
                android:layout_marginTop="15dp"
                android:hint="請輸入密碼"
                android:inputType="textPassword"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/et_account" />

            <TextView
                android:id="@+id/tv_forget"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="21dp"
                android:layout_marginTop="24dp"
                android:text="忘記密碼?"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_12"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/et_pwd" />

        </androidx.constraintlayout.widget.ConstraintLayout>


        <TextView
            android:id="@+id/tv_login"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/ic_next"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </RelativeLayout>
</RelativeLayout>

遇到的問題1

LoginFragment的根布局設(shè)置layout_height為指定高度無效...如果設(shè)置wrapContent,則無法設(shè)置底部按鈕重疊一半在登錄框上,網(wǎng)上查閱,說是自定義ViewPage,結(jié)果發(fā)現(xiàn),viewPage2是final類型的,無法被繼承與重寫

解決辦法

根布局嵌套一層布局,設(shè)置為指定高度就好了

遇到的問題2

Tablayout中的標(biāo)題無法設(shè)置字體大小.百度搜索都是自定義style,在androidX中的Tablayout中不管用

解決辦法

TextView textView = new TextView(mActivity);
textView.setText(titles[position]);
textView.setTextSize(17);
textView.setTextColor(Color.WHITE);
if (position == 0) {
textView.setAlpha(1);
} else {
textView.setAlpha(0.3f);
}
tab.setCustomView(textView);

遇到的問題3

tabItem選中后變換文字的透明度...并非變換文字顏色...搜索半天沒找到對應(yīng)的api設(shè)置

解決辦法

  mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView textView = (TextView) tab.getCustomView();
                textView.setAlpha(1);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView textView = (TextView) tab.getCustomView();
                textView.setAlpha(0.3f);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                TextView textView = (TextView) tab.getCustomView();
                textView.setAlpha(1);
            }
        });

至此,總算完成了UI設(shè)計圖的樣子...關(guān)于Tablayout+ViewPage2+Fragment的詳細使用,后面在詳細研究

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末并扇,一起剝皮案震驚了整個濱河市蹂风,隨后出現(xiàn)的幾起案子吼蚁,更是在濱河造成了極大的恐慌浊仆,老刑警劉巖灶似,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件史汗,死亡現(xiàn)場離奇詭異讥蟆,居然都是意外死亡,警方通過查閱死者的電腦和手機雀彼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門壤蚜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人徊哑,你說我怎么就攤上這事袜刷。” “怎么了莺丑?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵著蟹,是天一觀的道長。 經(jīng)常有香客問我梢莽,道長萧豆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任昏名,我火速辦了婚禮涮雷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘轻局。我一直安慰自己洪鸭,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布仑扑。 她就那樣靜靜地躺著览爵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪镇饮。 梳的紋絲不亂的頭發(fā)上蜓竹,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天,我揣著相機與錄音,去河邊找鬼俱济。 笑死司蔬,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的姨蝴。 我是一名探鬼主播俊啼,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼左医!你這毒婦竟也來了授帕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤浮梢,失蹤者是張志新(化名)和其女友劉穎跛十,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秕硝,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡芥映,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了远豺。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奈偏。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖躯护,靈堂內(nèi)的尸體忽然破棺而出惊来,到底是詐尸還是另有隱情,我是刑警寧澤棺滞,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布裁蚁,位于F島的核電站,受9級特大地震影響继准,放射性物質(zhì)發(fā)生泄漏枉证。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一移必、第九天 我趴在偏房一處隱蔽的房頂上張望室谚。 院中可真熱鬧,春花似錦避凝、人聲如沸舞萄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至撑螺,卻和暖如春含思,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工含潘, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留饲做,地道東北人。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓遏弱,卻偏偏與公主長得像盆均,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子漱逸,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,573評論 2 359

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

  • 一饰抒、概述 它也是design中新出的一個控件肮砾,用來實現(xiàn)選項卡切換的效果,以前我們常用RadioGroup+Radi...
    Serenity那年閱讀 2,415評論 0 9
  • 通常在ViewPager的上方袋坑,我們都會放一個標(biāo)簽指示器與ViewPager進行聯(lián)動仗处。以前,我們大多使用的是Git...
    DoAndKeep閱讀 79,608評論 36 110
  • 前言 好記性不如爛筆頭枣宫,學(xué)習(xí)的知識總要記錄下來婆誓,通過本文來加深對 ViewPager 方方面面的理解: ViewP...
    whd_Alive閱讀 2,212評論 0 9
  • 最近閑來無事,就想學(xué)習(xí)一下android 新出的tablayout控件也颤,原先是用radioGroup+fragme...
    涼了夏天寶貝閱讀 15,786評論 4 16
  • 只要有人的地方旷档,世界就不會是冰冷的!歇拆,我們可以平凡鞋屈,但絕對不可以平庸!——路遙
    大兔子肚子閱讀 1,216評論 5 59