Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

用 Include 佈局與 Fragment 實作滑動切換畫面

編輯:Android 資訊

前面的文章已經談過「隨手拍」專案在圖像處理上的技術細節,這一篇則聚焦主畫面的佈局安排與畫面滑動切換。參考鴻洋與郭神的 Android 部落格後,完成了以下幾件事:

(1). 學會使用 Include 佈局 XML
(2). 透過加入適配器來載入 fragment
(3). 以 ViewPager 實現滑動觸碰切換畫面
(4). 切換圖示與背景,並回應 fragment 內的元件與參數傳遞

一、運行效果

下圖所示,滑動螢幕即可在「空間」、「相冊」、「關注」三個佈局間切換,同時對應的圖示會變藍、背景顏色加深。

此外也加入了按鈕事件:在 fragment1 點擊按鈕會顯示內容,在 fragment3 點擊按鈕則取得第二個佈局的內容並顯示出來。


二、專案工程結構

三、Include 佈局 XML 檔

先加入頂部佈局 top_layout.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:paddingLeft="12dp" android:paddingRight="12dp" android:background="@drawable/image_toolbar_bg" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center" android:orientation="horizontal" > <ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icon_suishoupai" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:text="隨手拍" android:textSize="15sp" android:layout_gravity="center" android:textColor="#ffffff" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center" android:layout_alignParentRight="true" android:orientation="horizontal" > <ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/image_top_watch" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/image_top_add" /> </LinearLayout> </RelativeLayout>

接著加入底部佈局 bottom_layout.xml,由三個 LinearLayout 水平排列組成,每個 LinearLayout 又各含一個 ImageView 與 TextView:

<?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="@drawable/image_toolbar_bg" android:orientation="horizontal" > <LinearLayout android:id="@+id/bottomLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:background="@drawable/image_toolbar_bg_sel" android:orientation="vertical" > <ImageView android:id="@+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="1dp" android:src="@drawable/image_bottom_effect" /> <TextView android:layout_width="wrap_content" android:layout_height="15dp" android:text="空間" android:textColor="#ffffff" android:textSize="10dp" /> </LinearLayout> <LinearLayout android:id="@+id/bottomLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="1dp" android:src="@drawable/image_bottom_frame_no" /> <TextView android:layout_width="wrap_content" android:layout_height="15dp" android:text="相冊" android:textColor="#ffffff" android:textSize="10dp" /> </LinearLayout> <LinearLayout android:id="@+id/bottomLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/image3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="1dp" android:src="@drawable/image_bottom_person_no" /> <TextView android:layout_width="wrap_content" android:layout_height="15dp" android:text="關注" android:textColor="#ffffff" android:textSize="10dp" /> </LinearLayout> </LinearLayout>

最後在 activity_main.xml 中呼叫 Include 佈局,ViewPager 用於載入不同的 fragment,並在其上實現觸控切換:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/top_layout"/> <android.support.v4.view.ViewPager android:id="@+id/viewpager1" android:layout_width="match_parent" android:layout_height="0dp" android:background="#ccffff" android:layout_weight="1" /> <include layout="@layout/bottom_layout"/> </LinearLayout>

在 MainActivity.java 的 onCreate 中設定 requestWindowFeature(Window.FEATURE_NO_TITLE) 以隱藏標題;在 XML 中也能預覽無標題的 Frame 效果,顯示如下圖:

四、實現觸控切換 fragment

首先設定 Fragment 的佈局 XML,fragment_layout1.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="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25sp" android:gravity="center" android:text="The First Fragment" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Button1" /> </LinearLayout>

接著新增 FragmentFirst.java、FragmentSecond.java 與 FragmentThird,其中 FragmentSecond.java 如下,其餘類似:

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

補充:剛學 Android 一個月,所以內容偏基礎。新增類別時可點「瀏覽」自訂要繼承的超類,或點「添加」增加介面,這裡繼承 Fragment。務必注意「import android.support.v4.app.Fragment;」需保持一致。

接著設定 MainActivity.java,程式碼如下:

package com.example.layouttest; import java.util.ArrayList; import java.util.List; 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.view.Window; public class MainActivity extends FragmentActivity { //注意:導入時均為support.v4.app/view 保持一致 private ViewPager viewPager1; private FragmentPagerAdapter fpAdapter; private List<Fragment> listData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //注意:設定無標題需要在setContentView前呼叫 否則會崩潰 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //初始化設定ViewPager setViewPager(); } private void setViewPager() { //初始化資料 viewPager1 = (ViewPager) findViewById(R.id.viewpager1); listData = new ArrayList<Fragment>(); FragmentFirst fragmentFirst = new FragmentFirst(); FragmentSecond fragmentSecond = new FragmentSecond(); FragmentThird fragmentThird = new FragmentThird(); //三個佈局加入列表 listData.add(fragmentFirst); listData.add(fragmentSecond); listData.add(fragmentThird); //ViewPager相當於一組件容器 實現頁面切換 fpAdapter =new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public int getCount() { return listData.size(); } @Override public Fragment getItem(int arg0) { return listData.get(arg0); } }; //設定適配器 viewPager1.setAdapter(fpAdapter); } }

此時即可實現觸控切換效果,但需留意兩點:
(1). 要把 MainActivity 的繼承從 Activity 改為 FragmentActivity。
(2). 可能會遇到「類型對於參數(FragmentFirst)不適用」的錯誤,你需要把導入改成「import android.support.v4.app.Fragment;」,並注意 support.v4.app/view 保持一致。

五、實現滑屏切換圖示

要讓底部圖示隨滑動切換,需先加入自訂變數:

//底部圖示 private ImageView image1; private ImageView image2; private ImageView image3; private LinearLayout layout1; private LinearLayout layout2; private LinearLayout layout3;

接著在 setViewPager() 的「viewPager1.setAdapter(fpAdapter)」之後加入以下程式碼即可實現,其中 switch 中的 0、1、2 對應 listData 中的三個佈局:

//初始化圖示 image1 = (ImageView) findViewById(R.id.image1); image2 = (ImageView) findViewById(R.id.image2); image3 = (ImageView) findViewById(R.id.image3); layout1 = (LinearLayout) findViewById(R.id.bottomLayout1); layout2 = (LinearLayout) findViewById(R.id.bottomLayout2); layout3 = (LinearLayout) findViewById(R.id.bottomLayout3); //滑屏切換圖示 viewPager1.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int arg0) { switch(arg0) { case 0: //圖片切換 image1.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_effect)); image2.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_frame_no)); image3.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_person_no)); //背景加深 layout1.setBackgroundResource(R.drawable.image_toolbar_bg_sel); layout2.setBackgroundResource(R.drawable.image_toolbar_bg); layout3.setBackgroundResource(R.drawable.image_toolbar_bg); break; case 1: //圖片切換 image1.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_effect_no)); image2.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_frame)); image3.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_person_no)); //背景加深 layout1.setBackgroundResource(R.drawable.image_toolbar_bg); layout2.setBackgroundResource(R.drawable.image_toolbar_bg_sel); layout3.setBackgroundResource(R.drawable.image_toolbar_bg); break; case 2: //圖片切換 image1.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_effect_no)); image2.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_frame_no)); image3.setImageDrawable(getResources().getDrawable(R.drawable.image_bottom_person)); //背景加深 layout1.setBackgroundResource(R.drawable.image_toolbar_bg); layout2.setBackgroundResource(R.drawable.image_toolbar_bg); layout3.setBackgroundResource(R.drawable.image_toolbar_bg_sel); break; } } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } });

六、呼叫 Fragment 中的按鈕並傳遞參數

設定 FragmentFirst.java,透過 onActivityCreated 實作點擊按鈕事件:

public class FragmentFirst extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_layout1, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //加入Fragment1的回應事件 Button button1 = (Button) getActivity().findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TextView textView1 = (TextView) getActivity().findViewById(R.id.textView1); textView1.setText("在fragment1中點擊按鈕"); } }); } }

FragmentThird.java 則實作點擊 Fragment3 的按鈕去取得 Fragment2 的資料:

public class FragmentThird extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_layout3, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //加入Fragment3的回應事件 Button button3 = (Button) getActivity().findViewById(R.id.button3); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { TextView textView1 = (TextView) getActivity().findViewById(R.id.textView2); TextView textView3 = (TextView) getActivity().findViewById(R.id.textView3); textView3.setText("點擊按鈕獲取fragment2資訊:\n"+textView1.getText()); } }); } }

補充:注意各 Fragment 的 XML 中 TextView 要設不同的 id,若 Fragment1 與 Fragment2 設了相同的 textView1,程式將不會有回應。

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