Android 使用Fragment模仿微信界面的實例代碼
什么是Fragment
自從Android 3.0中引入fragments 的概念,根據(jù)詞海的翻譯可以譯為:碎片、片段。其目的是為了解決不同屏幕分辯率的動態(tài)和靈活UI設(shè)計。大屏幕如平板小屏幕如手機(jī),平板電腦的設(shè)計使得其有更多的空間來放更多的UI組件,而多出來的空間存放UI使其會產(chǎn)生更多的交互,從而誕生了fragments 。
fragments 的設(shè)計不需要你來親自管理view hierarchy 的復(fù)雜變化,通過將Activity 的布局分散到frament 中,可以在運行時修改activity 的外觀,并且由activity 管理的back stack 中保存些變化。當(dāng)一個片段指定了自身的布局時,它能和其他片段配置成不同的組合,在活動中為不同的屏幕尺寸修改布局配置(小屏幕可能每次顯示一個片段,而大屏幕則可以顯示兩個或更多)。
Fragment必須被寫成可重用的模塊。因為fragment有自己的layout,自己進(jìn)行事件響應(yīng),擁有自己的生命周期和行為,所以你可以在多個activity中包含同一個Fragment的不同實例。這對于讓你的界面在不同的屏幕尺寸下都能給用戶完美的體驗尤其重要。
Fragment優(yōu)點
Fragment可以使你能夠?qū)ctivity分離成多個可重用的組件,每個都有它自己的生命周期和UI。
Fragment可以輕松得創(chuàng)建動態(tài)靈活的UI設(shè)計,可以適應(yīng)于不同的屏幕尺寸。從手機(jī)到平板電腦。
Fragment是一個獨立的模塊,緊緊地與activity綁定在一起??梢赃\行中動態(tài)地移除、加入、交換等。
Fragment提供一個新的方式讓你在不同的安卓設(shè)備上統(tǒng)一你的UI。
Fragment 解決Activity間的切換不流暢,輕量切換。
Fragment 替代TabActivity做導(dǎo)航,性能更好。
Fragment 在4.2.版本中新增嵌套fragment使用方法,能夠生成更好的界面效果。
Fragment做局部內(nèi)容更新更方便,原來為了到達(dá)這一點要把多個布局放到一個activity里面,現(xiàn)在可以用多Fragment來代替,只有在需要的時候才加載Fragment,提高性能。
可以從startActivityForResult中接收到返回結(jié)果,但是View不能。

圖片中給出了實例的效果,在點擊下方的按鈕時,上半部分會自動切換成對應(yīng)的內(nèi)容。這里使用的技術(shù)是fragment。
想必大家對fragment已經(jīng)有所了解,就算不清楚,百度也有詳細(xì)的介紹。在這里就著重介紹實現(xiàn)的過程。
首先,拿其中的一個部分“首頁”來講:
上面一部分是fragment,下面則是相對固定的按鈕區(qū)。也就是說,當(dāng)點擊按鈕時,切換的只是上半部分內(nèi)容。所以,每一個fragment都有一個自己的xml布局文件。就想圖中所示的,“首頁”這個fragment的xml文件就是由一個textview構(gòu)成。
完成fragment的xml文件后,需要定義一個對應(yīng)的Java類來找到它,比如:首頁對應(yīng)的類是homeFragment.java。注意,這個類需要繼承fragment,并且每一個這樣繼承fragment的類都需要重寫其onCreateView的方法。具體代碼是:
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.cerian.marcon.R;
/**
* Created by Cerian on 2017/7/9.
*/
public class homeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_home, null);
//找到按鈕前要加view.
return view;
}
}
完成到這步時,每一個fragment的內(nèi)容就已經(jīng)完成了。接下來要做的是,將每一個fragment與一個頁面綁定并在其上顯示。這里我用了一個menufunction.xml

代碼是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/rl_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:showDividers="beginning|end|middle"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="@+id/ig_home"
android:clickable="true"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@mipmap/homepage1"/>
<ImageView
android:id="@+id/ig_lib"
android:clickable="true"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@mipmap/library1"/>
<ImageView
android:id="@+id/ig_my"
android:clickable="true"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@mipmap/my1"/>
</LinearLayout>
</RelativeLayout>
在這個布局中,上面的LinearLayout是用來顯示fragment內(nèi)容的,下面的是按鈕。
然后,在這個menufunction.xml的對應(yīng)java類中,找到定義好的fragment,并顯示。主要的思想是:①拿到一個管理者②開啟一個事務(wù)③替換fragment內(nèi)容④提交,注意,這里的第四步很容易被遺忘。
代碼是:
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import com.example.cerian.marcon.fragment.homeFragment;
import com.example.cerian.marcon.fragment.libFragment;
import com.example.cerian.marcon.fragment.myFragment;
/**
* Created by Cerian on 2017/7/9.
*/
public class home extends AppCompatActivity implements View.OnClickListener {
private ImageView ig_home, ig_lib, ig_my;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menufunction);
ig_home = (ImageView) findViewById(R.id.ig_home);
ig_lib = (ImageView) findViewById(R.id.ig_lib);
ig_my = (ImageView) findViewById(R.id.ig_my);
ig_home.setOnClickListener(this);
ig_lib.setOnClickListener(this);
ig_my.setOnClickListener(this);
/**
* 第一步:拿到管理者
* 第二步:開啟事務(wù)
* 第三步:替換
* 第四步:提交
*/
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.replace(R.id.ll_layout, new homeFragment());
ig_home.setImageResource(R.mipmap.homepage2);
beginTransaction.commit();
}
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
switch (view.getId()) {
case R.id.ig_home: //點擊的是主頁
beginTransaction.replace(R.id.ll_layout, new homeFragment());
ig_home.setImageResource(R.mipmap.homepage2);
ig_my.setImageResource(R.mipmap.my1);
ig_lib.setImageResource(R.mipmap.library1);
break;
case R.id.ig_lib: //點擊的是收藏
beginTransaction.replace(R.id.ll_layout, new libFragment());
ig_home.setImageResource(R.mipmap.homepage1);
ig_my.setImageResource(R.mipmap.my1);
ig_lib.setImageResource(R.mipmap.library2);
break;
case R.id.ig_my: //點擊的是我的
beginTransaction.replace(R.id.ll_layout, new myFragment());
ig_home.setImageResource(R.mipmap.homepage1);
ig_my.setImageResource(R.mipmap.my2);
ig_lib.setImageResource(R.mipmap.library1);
break;
}
beginTransaction.commit();
}
}
其中,因為涉及到的點擊事件有點多且相似,我用到了一個特殊的寫法,也就是setonclicklistener(this),參數(shù)用了this,并重新定義了一個click方法。注意:這樣寫,必須要繼承一個clicklistener的接口。
最后,提交就ok。
效果是:

這就是利用fragment來模擬微信界面。
以上所述是小編給大家介紹的Android 使用Fragment模仿微信界面的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Flutter runApp GestureBinding使用介紹
這篇文章主要為大家介紹了Flutter runApp GestureBinding使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android監(jiān)聽Home鍵和Back鍵的區(qū)別介紹
這篇文章主要介紹了Android監(jiān)聽Home鍵和Back鍵的區(qū)別介紹,本文還同時給出了Home鍵監(jiān)聽的實現(xiàn)代碼,需要的朋友可以參考下2015-06-06
Android用戶輸入自動提示控件AutoCompleteTextView使用方法
這篇文章主要為大家詳細(xì)介紹了Android用戶輸入自動提示控件AutoCompleteTextView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
Android音樂播放器制作 掃描本地音樂顯示在手機(jī)(一)
這篇文章主要介紹了Android音樂播放器的制作方法,掃描本地音樂顯示在手機(jī)上,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02
Android開發(fā)之Activity管理工具類完整示例
這篇文章主要介紹了Android開發(fā)之Activity管理工具類,集合完整實例形式分析了Android操作Activity創(chuàng)建、添加、獲取、移除等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
使用OkHttp包在Android中進(jìn)行HTTP頭處理的教程
HTTP頭部處理是HTTP網(wǎng)絡(luò)編程中的基本操作,安卓中使用OkHttp包(github.com/square/okhttp)進(jìn)行相關(guān)操作當(dāng)然也是得心應(yīng)手,這里我們就來看一下使用OkHttp包在Android中進(jìn)行HTTP頭處理的教程2016-07-07

