Android Fragment 简单实例
Android上的界面展示都是通過Activity實現的。Activity實在是太經常使用了。我相信大家都已經很熟悉了,這里就不再贅述。 可是Activity也有它的局限性,相同的界面在手機上顯示可能很好看,在平板上就未必了,由于平板的屏幕很大。手機的界面放在平板上可能會有過分被拉長、控件間距過大等情況。這個時候更好的體驗效果是在Activity中嵌入”小Activity”。然后每個”小Activity”又能夠擁有自己的布局。這就是Fragment碎片技術。
一、Fragment簡單介紹
Android是在Android 3.0 (API level 11)開始引入Fragment的。能夠把Fragment想成Activity中的模塊,這個模塊有自己的布局,有自己的生命周期,單獨處理自己的輸入,在Activity執行的時候能夠載入或者移除Fragment模塊。
能夠把Fragment設計成能夠在多個Activity中復用的模塊。
當開發的應用程序同一時候適用于平板電腦和手機時。能夠利用Fragment實現靈活的布局,改善用戶體驗。
二、Fragment生命周期
由于Fragment必須嵌入在Acitivity中使用。所以Fragment的生命周期和它所在的Activity是密切相關的。
假設Activity是暫停狀態。當中全部的Fragment都是暫停狀態;假設Activity是stopped狀態。這個Activity中全部的Fragment都不能被啟動。假設Activity被銷毀,那么它當中的全部Fragment都會被銷毀。可是,當Activity在活動狀態。能夠獨立控制Fragment的狀態,比方加上或者移除Fragment。
當這樣進行fragment transaction(轉換)的時候,能夠把fragment放入Activity的back stack中。這樣用戶就能夠進行返回操作。
Activity與Fragment生命周期對照圖
三、兩個簡單實例
布局文件activity_main.xml
Java文件
package com.example.fragmenttabhost;import android.os.Bundle; import android.support.v4.app.ListFragment; import android.widget.ArrayAdapter; import android.widget.Toast;public class MyFragment extends ListFragment{String show1[] = {"1.1","1.2","1.3","1.4"};String show2[] = {"2.1","2.2","2.3","2.4"};@Overridepublic void onActivityCreated(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);String show[] = null;Bundle bundle = getArguments();if(bundle == null)show = show1;else {show = show2;Toast.makeText(getActivity(), (CharSequence) bundle.get("key"), 1).show();}setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, show));} } package com.example.fragmenttabhost;import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost;public class MainActivity extends FragmentActivity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);FragmentTabHost tabHost = (FragmentTabHost) findViewById(R.id.tab);tabHost.setup(this, getSupportFragmentManager(), R.id.content);tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1"), MyFragment.class, null);Bundle b = new Bundle();b.putString("key", "I am tab2");tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Tab2",getResources().getDrawable(R.drawable.ic_launcher)), MyFragment.class, b);}}執行結果
2. 比較好看的demo,模仿微信主頁面
碎片布局文件fragment_1,2,3,4,5.xml
tab_item_view.xmlbutton布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:padding="3dp" android:src="@drawable/tab_home_btn"> </ImageView> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="首頁" android:textSize="10sp" android:textColor="#ffffff"> </TextView> </LinearLayout>主頁面布局main_tab_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><FrameLayout android:id="@+id/realtabcontent"android:layout_width="fill_parent"android:layout_height="0dip"android:layout_weight="1" /><android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost"android:layout_width="fill_parent"android:layout_height="wrap_content" android:background="@drawable/maintab_toolbar_bg"><FrameLayout android:id="@android:id/tabcontent"android:layout_width="0dp"android:layout_height="0dp"android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost></LinearLayout>FragmentPage1,2,3,4,5.java碎片實現
package com.vhreal.myfragmenttest;import com.vhreal.myfragmenttest.R;import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;public class FragmentPage1 extends Fragment{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_1, null); } }主頁面實現MainTabActivity.java
package com.vhreal.myfragmenttest;import com.vhreal.myfragmenttest.R;import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TabHost.TabSpec; import android.widget.TextView;/*** @author vhreal* 功能描寫敘述:自己定義TabHost*/ public class MainTabActivity extends FragmentActivity {// 定義FragmentTabHost對象private FragmentTabHost mTabHost;// 定義一個布局private LayoutInflater layoutInflater;// 定義數組來存放Fragment界面private Class fragmentArray[] = { FragmentPage1.class, FragmentPage2.class,FragmentPage3.class, FragmentPage4.class, FragmentPage5.class };// 定義數組來存放button圖片private int mImageViewArray[] = { R.drawable.tab_home_btn,R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,R.drawable.tab_square_btn, R.drawable.tab_more_btn };// Tab選項卡的文字private String mTextviewArray[] = { "首頁", "消息", "好友", "廣場", "很多其它" };public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main_tab_layout);initView();}/*** 初始化組件*/private void initView() {// 實例化布局對象layoutInflater = LayoutInflater.from(this);// 實例化TabHost對象,得到TabHostmTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);// 得到fragment的個數int count = fragmentArray.length;for (int i = 0; i < count; i++) {// 為每個Tabbutton設置圖標、文字和內容TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));// 將Tabbutton加入進Tab選項卡中mTabHost.addTab(tabSpec, fragmentArray[i], null);// 設置Tabbutton的背景mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);}}/*** 給Tabbutton設置圖標和文字*/private View getTabItemView(int index) {View view = layoutInflater.inflate(R.layout.tab_item_view, null);ImageView imageView = (ImageView) view.findViewById(R.id.imageview);imageView.setImageResource(mImageViewArray[index]);TextView textView = (TextView) view.findViewById(R.id.textview);textView.setText(mTextviewArray[index]);return view;} }執行結果
四、參考引用
- android碎片Fragment簡單介紹具體解釋
- Android Fragment 基本介紹
- Android Fragment 真正的全然解析(上下)必看
總結
以上是生活随笔為你收集整理的Android Fragment 简单实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中的ExceptionInIni
- 下一篇: 为何使用消息系统