日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Fragment使用简单示例

發布時間:2023/12/16 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fragment使用简单示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Fragment即碎片,使用它可以減少Activity的代碼量,降低耦合,使代碼更清爽,更易于讀懂。它和Activity具有類似的生命周期,連主要使用的方法也一樣。

Fragment添加到Activity的FragmetLayout:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag"); transaction.commit();

Fragment的切換:

transaction.replace(R.id.fragment_content, Fragment2.getInstance(),"fragmentTag");

添加使用add方法。替換使用replace方法。

下面是簡單的實現:

java代碼

Activity部分:

package com.example.Activity;import com.example.R; import com.example.Fragment.Fragment1; import com.example.Fragment.Fragment2;import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;public class MainActivity extends FragmentActivity {private Button btn_qiehuan;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();setDefaultFragment(savedInstanceState);}/*** 初始化視圖*/private void initView() {btn_qiehuan = (Button)findViewById(R.id.button_qiehuan);viewListen();}/*** 設置默認的Fragment* @param savedInstanceState */private void setDefaultFragment(Bundle savedInstanceState) {if (savedInstanceState==null) {FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//將fragment對象add添加到FrameLayout中。而FrameLayout是在Activity中,所以也即綁定到Activitytransaction.add(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag");//添加transaction.commit();}}/*** 切換Fragment*/public void switchFragment(){FragmentManager fm = getSupportFragmentManager();//Fragment管理器FragmentTransaction transaction = fm.beginTransaction();//fragment事務/** 每次添加與切換Fragment都共用一個標志:"fragmentTag",因此通過該標識能獲取當前fragment*/Fragment currFragment = fm.findFragmentByTag("fragmentTag");if (currFragment instanceof Fragment1) {//如果當前Fragment 是Fragment1的對象//replace替換transaction.replace(R.id.fragment_content, Fragment2.getInstance(),"fragmentTag");//replace替換}else{transaction.replace(R.id.fragment_content, Fragment1.getInstance(),"fragmentTag");//replace替換}transaction.commit();//提交 }/*** 設置監聽*/private void viewListen() {btn_qiehuan.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {switchFragment();}});}}

Fragment部分:

Fragment1:

package com.example.Fragment;import com.example.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 Fragment1 extends Fragment {private static Fragment1 instance;private Fragment1() {}public static Fragment1 getInstance(){if (instance == null) {instance = new Fragment1();}return instance; }@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_1, container,false);return rootView;} }

Fragment2:

package com.example.Fragment;import com.example.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 Fragment2 extends Fragment {private static Fragment2 instance;private Fragment2() {}public static Fragment2 getInstance(){if (instance == null) {instance = new Fragment2();}return instance; }@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_2, container,false);return rootView;} }

xml布局

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayout android:id="@+id/titleBar"android:layout_width="match_parent"android:layout_height="50dp"android:layout_alignParentTop="true"android:background="#22ff22"android:gravity="center_vertical" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Activity標題欄" /></LinearLayout><Button android:id="@+id/button_qiehuan"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="切換Fragment" /><FrameLayout android:id="@+id/fragment_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_above="@+id/button_qiehuan"android:layout_below="@+id/titleBar" ></FrameLayout></RelativeLayout>

fragment_1.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Fragment1"android:textSize="25sp" /></LinearLayout>

fragment_2

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Fragment2"android:textSize="25sp"/></LinearLayout>

總結

以上是生活随笔為你收集整理的Fragment使用简单示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。