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

ViewPager 結合 Fragment 實現微信風格界面的實例

編輯:關於 Android 程式設計

在當今的互聯網時代,微信已經是一款名副其實的超級 App。本文將通過 ViewPager + Fragment 實現一個類似微信的界面。此前我曾用 FragmentTabHost 做過類似效果,相比之下,ViewPager 的實現方式更加簡單明瞭。

ViewPager:

ViewPager 繼承自 ViewGroup,是一個容器類,可以往裡添加 View。

ViewPager 的使用很簡單,通過 setAdapter() 方法設置一個 PagerAdapter 即可,這個 PagerAdapter 需要自己編寫,實現裡面的一些方法。本文要與 Fragment 結合,所以實現的是 FragmentPagerAdapter 類,它繼承自 PagerAdapter。

ViewPager 通過 addOnPageChangeListener() 方法可以設置一個 ViewPager.OnPageChangeListener 監聽,當 Pager 發生變化時便調用相應的方法。

Fragment:

Fragment 有自己的生命週期,有興趣的朋友可以通過各種方式去研究(最簡單的一種就是打 Log 觀察),這裡不再贅述。與 ViewPager 結合時,有幾個 Pager 就需要實現幾個不同的 Fragment。

先看一下最終實現的效果圖:

布局上分為三部分:

最上面是 layout_top.xml,主要就是那個標題,只有一個 TextView;中間是 ViewPager;最下面是 layout_bottom.xml,包含三個線性布局,每個線性布局由一個 ImageView 和一個 TextView 組成。

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.example.administrator.viewpagerl.MainActivity"> <include layout="@layout/layout_top"></include> <android.support.v4.view.ViewPager android:id="@+id/ViewPagerLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </android.support.v4.view.ViewPager> <include layout="@layout/layout_bottom"></include> </LinearLayout>

layout_top.xml

 <?xml version="1.0" encoding="utf-8?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="3dp" android:paddingBottom="3dp" android:background="@android:color/darker_gray"> <TextView android:id="@+id/ViewTitle" android:layout_marginLeft="20dp" android:layout_marginTop="5dp" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>

layout_bottom.xml

 <?xml version="1.0" encoding="utf-8?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:paddingTop="3dp" android:paddingBottom="3dp" android:background="@android:color/holo_green_light"> <LinearLayout android:id="@+id/firstLinearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_weight="1"> <ImageView android:id="@+id/firstImageView" android:background="@drawable/tab_weixin" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/firstTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="微信" /> </LinearLayout> <LinearLayout android:id="@+id/secondLinearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_weight="1"> <ImageView android:id="@+id/secondImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tab_setting"/> <TextView android:id="@+id/secondTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="朋友" /> </LinearLayout> <LinearLayout android:id="@+id/threeLinearLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center_horizontal" android:layout_weight="1"> <ImageView android:id="@+id/threeImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/tab_find"/> <TextView android:id="@+id/threeTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發現" /> </LinearLayout> </LinearLayout>

前面提到,ViewPager 需要實現一個 PagerAdapter,做法很簡單:繼承 FragmentPagerAdapter,實現裡面的 getItem() 和 getCount() 方法即可。

ViewPagerFragmentAdapter .java

 package com.example.administrator.viewpagerl; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.util.Log; import java.util.ArrayList; import java.util.List; public class ViewPagerFragmentAdapter extends FragmentPagerAdapter { private List<Fragment> mList = new ArrayList<Fragment>(); public ViewPagerFragmentAdapter(FragmentManager fm , List<Fragment> list) { super(fm); this.mList = list; } @Override public Fragment getItem(int position) { return mList.get(position); } @Override public int getCount() { return mList != null ? mList.size() : 0; } }

ViewPager 的每個 Pager 都需要一個 Fragment,Fragment 會實例化布局並顯示在 ViewPager 的每個 Pager 中。

ChatFragment.java

 package com.example.administrator.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.example.administrator.viewpagerl.R; public class ChatFragment extends Fragment { View mView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { if (mView == null) { mView = inflater.inflate(R.layout.fragment_layout,null); } ((TextView)mView.findViewById(R.id.mTextView)).setText("聊天界面"); return mView; } }

這裡需要三個 Fragment,由於示例布局很簡單,三個布局基本一致,FriendFragment、FindFragment 這裡就不再貼出代碼了。微信裡的聊天列表、朋友列表,都是在 Fragment 實例化的布局裡放置一個 ListView 通過適配器實現的,這裡只是為了記錄 ViewPager 的用法,就沒有實現那些,感興趣的朋友可以自己動手試試,其實並不難。

在 Activity 裡,只需給 ViewPager 設置上面那個 Adapter,再設置一個監聽來感知 Pager 的變化即可。點擊最下面的微信、朋友、發現三個按鈕,通過 ViewPager 的 setCurrentItem() 方法就能跳轉到對應的 Pager。除此之外,再通過一些簡單的邏輯控制界面的改變就行,沒有太複雜的東西。

MainActivity.java

 package com.example.administrator.viewpagerl; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import com.example.administrator.fragment.ChatFragment; import com.example.administrator.fragment.FindFragment; import com.example.administrator.fragment.FriendFragment; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "MainActivity.TAG"; TextView titleTextView; public LinearLayout firstLinearLayout; public LinearLayout secondLinearLayout; public LinearLayout threeLinearLayout; ViewPager mViewPager; ViewPagerFragmentAdapter mViewPagerFragmentAdapter; FragmentManager mFragmentManager; String[] titleName = new String[] {"微信","朋友","發現"}; List<Fragment> mFragmentList = new ArrayList<Fragment>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mFragmentManager = getSupportFragmentManager(); setContentView(R.layout.activity_main); initFragmetList(); mViewPagerFragmentAdapter = new ViewPagerFragmentAdapter(mFragmentManager,mFragmentList); initView(); initViewPager(); } @Override protected void onResume() { super.onResume(); } public void initViewPager() { mViewPager.addOnPageChangeListener(new ViewPagetOnPagerChangedLisenter()); mViewPager.setAdapter(mViewPagerFragmentAdapter); mViewPager.setCurrentItem(0); titleTextView.setText(titleName[0]); updateBottomLinearLayoutSelect(true,false,false); } public void initFragmetList() { Fragment chat = new ChatFragment(); Fragment friend = new FriendFragment(); Fragment find = new FindFragment(); mFragmentList.add(chat); mFragmentList.add(friend); mFragmentList.add(find); } public void initView() { titleTextView = (TextView) findViewById(R.id.ViewTitle); mViewPager = (ViewPager) findViewById(R.id.ViewPagerLayout); firstLinearLayout = (LinearLayout) findViewById(R.id.firstLinearLayout); firstLinearLayout.setOnClickListener(this); secondLinearLayout = (LinearLayout) findViewById(R.id.secondLinearLayout); secondLinearLayout.setOnClickListener(this); threeLinearLayout = (LinearLayout) findViewById(R.id.threeLinearLayout); threeLinearLayout.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.firstLinearLayout: mViewPager.setCurrentItem(0); updateBottomLinearLayoutSelect(true,false,false); break; case R.id.secondLinearLayout: mViewPager.setCurrentItem(1); updateBottomLinearLayoutSelect(false,true,false); break; case R.id.threeLinearLayout: mViewPager.setCurrentItem(2); updateBottomLinearLayoutSelect(false,false,true); break; default: break; } } private void updateBottomLinearLayoutSelect(boolean f, boolean s, boolean t) { firstLinearLayout.setSelected(f); secondLinearLayout.setSelected(s); threeLinearLayout.setSelected(t); } class ViewPagetOnPagerChangedLisenter implements ViewPager.OnPageChangeListener { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // Log.d(TAG,"onPageScrooled"); } @Override public void onPageSelected(int position) { Log.d(TAG,"onPageSelected"); boolean[] state = new boolean[titleName.length]; state[position] = true; titleTextView.setText(titleName[position]); updateBottomLinearLayoutSelect(state[0],state[1],state[2]); } @Override public void onPageScrollStateChanged(int state) { Log.d(TAG,"onPageScrollStateChanged"); } } }

以上便是 ViewPager 與 Fragment 結合實現微信風格界面的實例代碼,希望對大家有所幫助。如有疑問,歡迎留言交流。

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