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

Fragment + ViewPager 高仿微信主界面

編輯:關於 Android 程式設計

什麼是 ViewPager?關於 ViewPager 的介紹和使用,我之前寫過一篇《ViewPager 組件(仿微信引導界面)》,這裡就不再重複,不清楚的朋友可以翻看舊文。

什麼是 Fragment?Fragment 是 Android 3.0 之後新增的概念,中文名為「碎片」,它與 Activity 十分相似,同樣擁有自己的生命週期。Fragment 用來描述某個 Activity 中的一部分行為或一部分用戶界面,我們可以在單個 Activity 中合併多個 Fragment 來搭建多個 UI 面板,也可以在多個 Activity 中重複使用 Fragment。

由於 Fragment 依賴於 Activity,所以當一個 Activity 的生命週期結束後,Fragment 的生命週期自然也隨之結束。如果把一個 Activity 比作一座大宅子,那麼 Fragment 就是宅子裡的房間——其中一間房塌了並不會讓整座宅子倒塌,但一旦宅子塌了,裡面的房間也就都不復存在了。

理解了基本概念後,先看看今天要實現的效果(高仿微信主界面):底部三個 Tab 分別是「聊天」「發現」「通訊錄」,頂部有標題欄,中間是滑動切換的內容區,下方有一條隨滑動移動的下劃線。

這裡把 XML 布局拆分開來寫,先看頂部的 top1.xml:

1 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 xmlns:tools="https://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="50dp" 5 android:background="@drawable/bg" 6 android:paddingLeft="12dp" 7 android:paddingRight="12dp" > 8 9 <LinearLayout 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_centerVertical="true" 13 android:gravity="center" 14 android:orientation="horizontal" > 15 16 <ImageView 17 android:layout_width="30dp" 18 android:layout_height="30dp" 19 android:src="@drawable/weixin" /> 20 21 <TextView 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_marginLeft="12dp" 25 android:text="微信" 26 android:textColor="@android:color/white" 27 android:textSize="18dp" /> 28 </LinearLayout> ... </RelativeLayout>

中間的 Tab 切換欄 top2.xml(含三個 TextView 和一條下劃線 ImageView):

1 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="40dp" 4 android:orientation="vertical" > 5 <LinearLayout 6 android:layout_width="match_parent" 7 android:layout_height="37dp" 8 android:gravity="center_vertical" 9 android:background="#cccccc" > 10 <LinearLayout ... android:layout_weight="1" android:gravity="center" > 11 <TextView android:id="@+id/tv1" android:text="聊天" android:textColor="#339900"/> 12 </LinearLayout> 13 <LinearLayout ... android:layout_weight="1" android:gravity="center" > 14 <TextView android:id="@+id/tv2" android:text="發現" android:textColor="@android:color/black"/> 15 </LinearLayout> 16 <LinearLayout ... android:layout_weight="1" android:gravity="center" > 17 <TextView android:id="@+id/tv3" android:text="通訊錄" android:textColor="@android:color/black"/> 18 </LinearLayout> 19 </LinearLayout> 20 <LinearLayout android:layout_width="match_parent" android:layout_height="3dp" > 21 <ImageView android:id="@+id/tabline" android:layout_width="100dp" android:layout_height="match_parent" android:background="@drawable/tabline" /> 22 </LinearLayout> 23 </LinearLayout>

主布局 mywx.xml(用 include 包含前兩個布局,並放置 ViewPager):

1 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 <include layout="@layout/top1" /> 6 <include layout="@layout/top2" /> 7 <android.support.v4.view.ViewPager 8 android:id="@+id/viewpager" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:layout_weight="1" > 12 </android.support.v4.view.ViewPager> 13 </LinearLayout>

每個 Fragment 的布局都很簡單,只用文字標示,這裡只給出 Fragment1.xml:

1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 <TextView 6 android:layout_width="wrap_content" 7 android:layout_height="wrap_content" 8 android:text="我是第一個界面" 9 android:textSize="30dp" 10 android:layout_centerInParent="true" /> 11 </RelativeLayout>

接下來是核心的 JAVA 代碼,用法與之前的 ViewPager 一致,只不過數據源裡存放的不再是 View 對象,而是 Fragment:

 package com.example.weixin_test; import java.util.ArrayList; import java.util.List; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.DisplayMetrics; import android.view.Window; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MyWxTest extends FragmentActivity { private ViewPager viewPager; private TextView tv1, tv2, tv3; private ImageView tabline; private List<Fragment> list; private int tabLineLength; private int currentPage = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.mywx); initTabLine(); initView(); } private void initTabLine() { Display display = getWindow().getWindowManager().getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); tabLineLength = metrics.widthPixels / 3; tabline = (ImageView) findViewById(R.id.tabline); LayoutParams lp = tabline.getLayoutParams(); lp.width = tabLineLength; tabline.setLayoutParams(lp); } private void initView() { viewPager = (ViewPager) findViewById(R.id.viewpager); tv1 = (TextView) findViewById(R.id.tv1); tv2 = (TextView) findViewById(R.id.tv2); tv3 = (TextView) findViewById(R.id.tv3); list = new ArrayList<Fragment>(); list.add(new Fragment1()); list.add(new Fragment2()); list.add(new Fragment3()); FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public int getCount() { return list.size(); } @Override public Fragment getItem(int arg0) { return list.get(arg0); } }; viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { tv1.setTextColor(Color.BLACK); tv2.setTextColor(Color.BLACK); tv3.setTextColor(Color.BLACK); switch (position) { case 0: tv1.setTextColor(Color.rgb(51, 153, 0)); break; case 1: tv2.setTextColor(Color.rgb(51, 153, 0)); break; case 2: tv3.setTextColor(Color.rgb(51, 153, 0)); break; } currentPage = position; } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { LinearLayout.LayoutParams ll = (android.widget.LinearLayout.LayoutParams) tabline.getLayoutParams(); if (currentPage == 0 && arg0 == 0) { ll.leftMargin = (int) (currentPage * tabLineLength + arg1 * tabLineLength); } else if (currentPage == 1 && arg0 == 1) { ll.leftMargin = (int) (currentPage * tabLineLength + arg1 * tabLineLength); } else if (currentPage == 1 && arg0 == 0) { ll.leftMargin = (int) (currentPage * tabLineLength - ((1 - arg1) * tabLineLength)); } else if (currentPage == 2 && arg0 == 1) { ll.leftMargin = (int) (currentPage * tabLineLength - (1 - arg1) * tabLineLength); } tabline.setLayoutParams(ll); } @Override public void onPageScrollStateChanged(int arg0) {} }); } }

對這個類做幾點說明:

1. 滑動時下劃線跟隨的效果有兩種實現思路,原理相同:一是使用 ViewPager 的子類,比較簡單;二是用原生代碼動態控制下劃線的左外邊距。這裡採用第二種,授人以魚不如授人以漁,理清細節後其實並不複雜。

關鍵在 onPageScrolled 方法的三個參數。第一個參數是当前頁面索引,第二個參數是滑動偏移比例(從 0.0 遞增到接近 1,到達新頁面後歸零),第三個參數是滾動像素值(可忽略)。利用第二個參數就能計算下劃線的左外邊距,推導出的公式為:

向左滑:當前頁數 × 屏幕 1/3 寬 + 第二個參數 × 屏幕 1/3 寬;

向右滑:當前頁數 × 屏幕 1/3 寬 −(1 − 第二個參數)× 屏幕 1/3 寬。

2. 由於用到了 Fragment,這裡不再像以往那樣繼承 Activity,而是繼承其子類 FragmentActivity。

由於三個 Fragment 代碼幾乎一致,這裡只給出 Fragment1.java:

 package com.example.weixin_test; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } }

關於這個類補充三點:

1. Fragment 通常作為 Activity 界面的一部分,它把 Layout 對象嵌入到 Activity 中。要為 Fragment 提供 Layout,必須重寫 onCreateView() 方法,它返回一個 View 對象,並為我們提供了一個 LayoutInflater,便於把 XML 布局轉換成 View。

2. onCreateView() 中的 container 參數用來存放 Fragment 的 layout,savedInstanceState 是一個 Bundle,作用與 Activity 的 onCreate() 中的 Bundle 類似,用於狀態恢復。

3. inflate() 方法有三個參數:布局資源 id、存放 fragment 的 ViewGroup,以及一個布爾值——它表示是否在創建 Fragment layout 期間把 layout 附加到 container 上。由於系統已經把 layout 對象放進了 ViewGroup,所以這裡傳 false。

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