Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

高仿微信 5.2.1 主介面:ViewPager + Fragment 選項卡與紅點提醒

編輯:關於 Android 程式設計

好一陣子沒更新文章了,最近在忙公司的專案,這也是我參與的第一個正式專案。做完之後才發現,自己累積一年的知識真的還不夠用。想精進,靠得住的路徑其實很樸素:專案、書、影片、部落格,加上最重要的一點——親手做。最近發現慕課網的教學影片免費而且含金量很高,用業餘時間跟著做,收穫不少。以下把這些小 demo 整理出來分享,我在過程中做了一些優化,程式碼與影片略有出入,但功能完整可用。

這是一個模仿微信 5.2.1 版本的介面:

功能盤點與技術選型

目標功能很單純:

  1. 頂部有一條自訂標題欄。
  2. 下方是「聊天/發現/通訊錄」三個選項卡。
  3. 手指滑動時,文字下方的藍色 indicator 會跟著移動。
  4. 聊天分頁右側有一顆紅色的未讀訊息提醒圓點。

對應的技術選擇:

  • 自訂標題欄:一個 LinearLayout 即可,同時把系統的 TitleBar/ActionBar 隱藏起來。
  • 選項卡:直覺想到 Fragment。
  • 可滑動:中間那塊當然是 ViewPager,資料來源是 Fragment 集合,用 FragmentPagerAdapter 管理。
  • indicator 跟手:為 ViewPager 掛上監聽,依回調值動態調整 indicator 的 marginLeft。
  • 紅點:使用第三方開源元件 BadgeView。

主版面配置

先來自訂的標題欄:

 <!-- top1.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/topone_bg" android:paddingLeft="12dp" android:paddingRight="12dp">
 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:gravity="center" android:orientation="horizontal">
 <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_icon" />
 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:text="微信" android:textColor="#D3D3D3" android:textSize="18sp" />
 </LinearLayout>
 <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:orientation="horizontal">
 <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_search_icon" />
 <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_add_icon" />
 <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/actionbar_more_icon" />
 </LinearLayout>
 </RelativeLayout> 

效果如下:

接著是選項卡的部分。三個分頁均分寬度,下方那條藍線就是 indicator:

 <!-- top2.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="#EEEEEE" android:orientation="vertical">
 <LinearLayout android:layout_width="match_parent" android:layout_height="37dp" android:orientation="horizontal">
 <LinearLayout android:id="@+id/ll_chat" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:orientation="horizontal">
 <TextView android:id="@+id/tv_tab_chat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="聊天" android:textColor="#008000" android:textSize="16sp" />
 </LinearLayout>
 <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center">
 <TextView android:id="@+id/tv_tab_discover" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發現" android:textColor="@android:color/black" android:textSize="16sp" />
 </LinearLayout>
 <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center">
 <TextView android:id="@+id/tv_tab_contacts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="通訊錄" android:textColor="@android:color/black" android:textSize="16sp" />
 </LinearLayout>
 </LinearLayout>
 <ImageView android:id="@+id/iv_tab_line" android:layout_width="100dp" android:layout_height="3dp" android:background="@drawable/tabline" />
 </LinearLayout> 

效果如下:

這裡 indicator 的寬度先寫成任意值,因為真正的長度要在程式碼中依螢幕寬度動態設定。

最後把兩段配置 include 進主版面,並加入 ViewPager:

 <!-- activity_main.xml -->
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.demo.lenovo.myapplication.MainActivity">
 <include layout="@layout/top1" />
 <include layout="@layout/top2" />
 <android.support.v4.view.ViewPager android:id="@+id/vp_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" />
 </LinearLayout> 

效果如下:

補充一個隱藏標題欄的小技巧:若你的 Activity 繼承自 ActionBarActivity,可在 setContentView() 之前呼叫 requestWindowFeature(Window.FEATURE_NO_TITLE);;若繼承自 AppCompatActivity,則可在 AndroidManifest 的 Application 標籤上設定 android:theme="@style/Theme.AppCompat.NoActionBar",同樣能達到目的。

用 FragmentPagerAdapter 餵資料

宣告好成員變數,並在 onCreate 中關掉標題欄:

 private FragmentPagerAdapter adapter;
 private List<Fragment> mData;
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 setContentView(R.layout.activity_main);
 initView(); 

在 initView() 裡把 Fragment 實例依序放進 List,交給 Adapter 管理,最後掛到 ViewPager 上(三個 Fragment 的版面與程式碼此處省略):

 mData = new ArrayList<>();
 mData.add(new ChatFragment());
 mData.add(new DiscoverFragment());
 mData.add(new ContactsFragment());
 adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
 @Override public Fragment getItem(int position) {
 return mData.get(position);
 }
 @Override public int getCount() {
 return mData.size();
 }
 };
 vp_content.setAdapter(adapter); 

切換分頁時的顏色變化

為 ViewPager 掛上 OnPageChangeListener,在 onPageSelected() 裡先把所有文字歸零成黑色,再把當前分頁上色:

 @Override public void onPageSelected(int position) {
 Log.e(TAG, "onPageSelected: " + position);
 resetTextViewColor();
 switch (position) {
 case 0:
 addBadgeView();
 tv_tab_chat.setTextColor(Color.parseColor("#008000"));
 break;
 case 1:
 tv_tab_discover.setTextColor(Color.parseColor("#008000"));
 break;
 case 2:
 tv_tab_contacts.setTextColor(Color.parseColor("#008000"));
 break;
 }
 }
 // 首先將每個選項卡文字顏色置為黑色
 private void resetTextViewColor() {
 tv_tab_contacts.setTextColor(Color.BLACK);
 tv_tab_chat.setTextColor(Color.BLACK);
 tv_tab_discover.setTextColor(Color.BLACK);
 } 

加上 BadgeView

寫法是先判斷是否已存在,有的話先移除再重新加入,避免堆疊:

 private void addBadgeView() {
 if (mBadgeView != null) {
 ll_chat.removeView(mBadgeView);
 }
 mBadgeView = new BadgeView(MainActivity.this);
 ll_chat.addView(mBadgeView);
 mBadgeView.setBadgeCount(9);
 } 

indicator 跟手滑動

這部分的邏輯寫在 onPageScrolled() 裡。先看官方對三個參數的定義:

第一個參數 position 代表目前所在頁,例如從第 0 頁往第 1 頁滑,過程中 position 會一直是 0,直到手放開完成切換才變成 1;positionOffset 是滑動百分比,範圍 0~1;positionOffsetPixels 則是滑動的像素數。

從 0 頁滑到 1 頁時印出的 log:

從 1 頁滑到 2 頁:

從 2 頁回滑到 1 頁:

有了這兩個參數,indicator 的位置就能用 (position + positionOffset) × 螢幕寬度 ÷ 3 算出來。

先把 indicator 的寬度設為螢幕寬度的三分之一:

 WindowManager manager = getWindow().getWindowManager();
 Display display = manager.getDefaultDisplay();
 DisplayMetrics outMetrics = new DisplayMetrics();
 display.getMetrics(outMetrics);
 mScreenOneThird = outMetrics.widthPixels / 3; 

這裡 mScreenOneThird 是 int 型別,單位為 px。接著套用到 view 上:

 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv_tab_line.getLayoutParams();
 lp.width = mScreenOneThird;
 iv_tab_line.setLayoutParams(lp); 

最後在 onPageScrolled() 中動態更新 marginLeft:

 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv_tab_line.getLayoutParams();
 lp.leftMargin = (int) ((positionOffset * mScreenOneThird) + (mScreenOneThird * position));
 iv_tab_line.setLayoutParams(lp);
 } 

這樣一來,indicator 就會平順地跟著手指移動了。

整體拆下來其實沒有什麼神奇的地方:自訂標題欄、Fragment 選項卡、監聽回調算位置、加上一個開源的 BadgeView,四塊拼起來就是熟悉的微信主頁手感。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved