国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android應用開發中Fragment與Activity間通信示例講解

2020-04-11 10:44:25
字體:
來源:轉載
供稿:網友

首先,如果你想在android3.0及以下版本使用fragment,你必須引用android-support-v4.jar這個包
然后你寫的activity不能再繼承自Activity類了,而是要繼承android.support.v4.app.FragmentActivity,一些其他的父類也有相應的變化.
由于在android的實現機制中fragment和activity會被分別實例化為兩個不相干的對象,他們之間的聯系由activity的一個成員對象fragmentmanager來維護.fragment實例化后會到activity中的fragmentmanager去注冊一下,這個動作封裝在fragment對象的onAttach中,所以你可以在fragment中聲明一些回調接口,當fragment調用onAttach時,將這些回調接口實例化,這樣fragment就能調用各個activity的成員函數了,當然activity必須implements這些接口,否則會包classcasterror
fragment和activity的回調機制又是OOP的一次完美演繹!
下面通過一個例子來說明:
首先,我們看下界面

2016225172715543.jpg (480×800)

2016225172753509.jpg (480×800)

左邊的TextView會根據右邊點擊button的不同而改變。

下面開始介紹代碼:

1.在layout里新建fragment1.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:background="#00ff00" android:orientation="vertical" > <TextView android:id="@+id/fragment_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 1" android:textColor="#000000" android:textSize="25sp" /></LinearLayout>

可以看出,這里就只有一個TextView

2.在layout里新建fragment2.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:background="#ffff00" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="this is fragment 2" android:textColor="#000000" android:textSize="25sp" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 2" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="num 3" /></LinearLayout>

這里是三個button

3.創建類Fragment1繼承Fragment

package lgx.fram.framents;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); }}

重寫onCreateView()方法,這里 return inflater.inflate(R.layout.fragment1, container, false); 這句話是重點

4.創建類Fragment2繼承Fragment

package lgx.fram.framents;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.TextView;public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, container, false); } TextView textview; Button button, button2, button3; @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); button = (Button) getActivity().findViewById(R.id.button); button2 = (Button) getActivity().findViewById(R.id.button2); button3 = (Button) getActivity().findViewById(R.id.button3); textview = (TextView) getActivity().findViewById(R.id.fragment_text); button.setOnClickListener(new OnClickListener() {  @Override  public void onClick(View v) {  textview.setText(button.getText());  } }); button2.setOnClickListener(new OnClickListener() {  @Override  public void onClick(View v) {  textview.setText(button2.getText());  } }); button3.setOnClickListener(new OnClickListener() {  @Override  public void onClick(View v) {  textview.setText(button3.getText());  } }); }}button = (Button) getActivity().findViewById(R.id.button); 

通過這種方法來得到fragment上面的控件

5.activity_fragment.xml里面的代碼是這個樣子的

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" > <fragment android:id="@+id/fragment1" android:name="lgx.fram.framents.Fragment1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <fragment android:id="@+id/fragment2" android:name="lgx.fram.framents.Fragment2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /></LinearLayout>

注意:控件fragment里的android:name=" "里面填寫的是你的Fragment類的絕對路徑(腦子突然短路,是這樣說的嗎??),id用來標示fragment。

6.FragmentActivity是最簡單的,就只是setContentView,并沒有進行其他改變。看下面

package lgx.fram.framents;import android.app.Activity;import android.os.Bundle;public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); }}

在這里我的整個小應用就做完了。我這里的Fragment通過布局文件加入到Activity里的,還有另一種方式是通過編程的方式將Fragment加入Activity里。這里我簡單敘述

上面的1,2,3,4都不需要動。

第5步驟,activity_fragment.xml里面的代碼變成下面的

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal" ></LinearLayout>

你會發現我知識去掉了兩個Fragment,整個LinearLayout加進去了id

第6個步驟,里面的注釋,已經寫得很清楚了:

package lgx.fram.framents;import android.os.Bundle;import android.app.Activity;import android.view.Display;import android.view.Menu;

 @author lenovo 動態添加Fragment主要分為4步:
(1)獲取到FragmentManager,在Activity中可以直接通過getFragmentManager得到。
(2)開啟一個事務,通過調用beginTransaction方法開啟。
(3)向容器內加入Fragment,一般使用replace方法實現,需要傳入容器的id和Fragment的實例。
(4)提交事務,調用commit方法提交。

public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); Display display = getWindowManager().getDefaultDisplay(); if (display.getWidth() > display.getHeight()) {  Fragment1 fragment1 = new Fragment1();  getFragmentManager().beginTransaction()   .replace(R.id.main_layout, fragment1).commit(); } else {  Fragment2 fragment2 = new Fragment2();  getFragmentManager().beginTransaction()   .replace(R.id.main_layout, fragment2).commit(); } }}

這個代碼的意思是,橫豎屏顯示不同的Fragment。如果是模擬機測試,請按Ctrl+F11。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌审旗| 社会| 张北县| 烟台市| 武平县| 黔西| 潮州市| 来安县| 绵竹市| 长治市| 新乐市| 建宁县| 闽清县| 苏州市| 通江县| 台北县| 漳州市| 禄劝| 凤山县| 资溪县| 霸州市| 黄冈市| 涿州市| 沙田区| 合肥市| 鱼台县| 鲁山县| 霍城县| 武邑县| 永州市| 灵宝市| 防城港市| 张北县| 宝鸡市| 桐柏县| 普兰县| 邵阳县| 通河县| 临澧县| 彭州市| 上思县|