❶ android fragment多窗口怎麼使用
、Fragment的產生與介紹
關於fragment的實例,請參考android學習手冊,android學習手冊包含9個章節,108個例子,源碼文檔隨便看,例子都是可交互,可運行,
源碼採用android studio目錄結構,高亮顯示代碼,文檔都採用文檔結構圖顯示,可以快速定位。360手機助手中下載,圖標上有貝殼。
Android運行在各種各樣的設備中,有小屏幕的手機,超大屏的平板甚至電視。針對屏幕尺寸的差距,很多情況下,都是先針對手機開發一套App,然後拷貝一份,修改布局以適應平板神馬超級大屏的。難道無法做到一個App可以同時適應手機和平板么,當然了,必須有啊。Fragment的出現就是為了解決這樣的問題。你可以把Fragment當成Activity的一個界面的一個組成部分,甚至Activity的界面可以完全有不同的Fragment組成,更帥氣的是Fragment擁有自己的生命周期和接收、處理用戶的事件,這樣就不必在Activity寫一堆控制項的事件處理的代碼了。更為重要的是,你可以動態的添加、替換和移除某個Fragment。
2、Fragment的生命周期
Fragment必須是依存與Activity而存在的,因此Activity的生命周期會直接影響到Fragment的生命周期。官網這張圖很好的說明了兩者生命周期的關系:
可以看到Fragment比Activity多了幾個額外的生命周期回調方法:
onAttach(Activity)
當Fragment與Activity發生關聯時調用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
創建該Fragment的視圖
onActivityCreated(Bundle)
當Activity的onCreate方法返回時調用
onDestoryView()
與onCreateView想對應,當該Fragment的視圖被移除時調用
onDetach()
與onAttach相對應,當Fragment與Activity關聯被取消時調用
注意:除了onCreateView,其他的所有方法如果你重寫了,必須調用父類對於該方法的實現,
3、靜態的使用Fragment
嘿嘿,終於到使用的時刻了~~
這是使用Fragment最簡單的一種方式,把Fragment當成普通的控制項,直接寫在Activity的布局文件中。步驟:
1、繼承Fragment,重寫onCreateView決定Fragemnt的布局
2、在Activity中聲明此Fragment,就當和普通的View一樣
下面展示一個例子(我使用2個Fragment作為Activity的布局,一個Fragment用於標題布局,一個Fragment用於內容布局):
TitleFragment的布局文件:
[html] view plain print?
<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar">
<ImageButton
android:id="@+id/id_title_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/showleft_selector"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="我不是微信"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar" >
<ImageButton
android:id="@+id/id_title_left_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:background="@drawable/showleft_selector" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="我不是微信"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
TitleFragment
[java] view plain print?
packagecom.zhy.zhy_fragments;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.ViewGroup;
importandroid.widget.ImageButton;
importandroid.widget.Toast;
{
privateImageButtonmLeftMenu;
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
BundlesavedInstanceState)
{
Viewview=inflater.inflate(R.layout.fragment_title,container,false);
mLeftMenu=(ImageButton)view.findViewById(R.id.id_title_left_btn);
mLeftMenu.setOnClickListener(newOnClickListener()
{
@Override
publicvoidonClick(Viewv)
{
Toast.makeText(getActivity(),
"!",
Toast.LENGTH_SHORT).show();
}
});
returnview;
}
}
package com.zhy.zhy_fragments;
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.ImageButton;
import android.widget.Toast;
public class TitleFragment extends Fragment
{
private ImageButton mLeftMenu;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_title, container, false);
mLeftMenu = (ImageButton) view.findViewById(R.id.id_title_left_btn);
mLeftMenu.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getActivity(),
"i am an ImageButton in TitleFragment ! ",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
同理還有ContentFragment的其布局文件:
[html] view plain print?
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="使用Fragment做主面板"
android:textSize="20sp"
android:textStyle="bold"/>
</LinearLayout>
<?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="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="使用Fragment做主面板"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
[java] view plain print?
packagecom.zhy.zhy_fragments;
importandroid.app.Fragment;
importandroid.os.Bundle;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.ViewGroup;
{
@Override
publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,
BundlesavedInstanceState)
{
returninflater.inflate(R.layout.fragment_content,container,false);
}
}
package com.zhy.zhy_fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ContentFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_content, container, false);
}
}
MainActivity
[java] view plain print?
packagecom.zhy.zhy_fragments;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Window;
{
@Override
protectedvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
package com.zhy.zhy_fragments;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}
Activity的布局文件:
[java] view plain print?
<RelativeLayoutxmlns: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">
<fragment
android:id="@+id/id_fragment_title"
android:name="com.zhy.zhy_fragments.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp"/>
<fragment
android:layout_below="@id/id_fragment_title"
android:id="@+id/id_fragment_content"
android:name="com.zhy.zhy_fragments.ContentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
❷ 簡述Fragment的作用
Fragment 是什麼
今天我們來學習一個比較重要的組件--Fragment。Fragment在應用開發中應用得非常的頻繁,特別是在開發一些需要兼容手機設備、平板設備和智能電視等大屏幕設備的應用,Fragment發揮著重要的作用。那說了這么多,Fragment到底是什麼呢?在這里我們先簡單的用一句話總結就是:Fragment是Android為了應用適配不同設備的大屏幕、支持更加動態和靈活的UI設計所提供的一個組件。
說到Fragment,就會聯想到Activity,因為Fragment對象在應用中一般是不能獨立存在的,它必須嵌入到activity中,而且Fragment的生命周期直接受所在的activity的影響。例如:當activity暫停時,他擁有的所有的Fragment都暫停了,當activity銷毀時,他擁有的所有Fragment都被銷毀。然而,當activity運行時(在onResume()之後,onPause()之前),可以單獨地操作每個Fragment,比如添加或刪除它們。當中執行上述針對Fragment的事務時,可以將事務添加到一個棧中,這個棧被activity管理,棧中的每一條都是一個Fragment的一次事務。有了這個棧,就可以反向執行Fragment的事務,這樣就可以在Fragment級支持「返回」鍵(向後導航)。
當向activity中添加一個Fragment時,它須置於ViewGroup控制項中,並且需定義Fragment自己的界面。可以在layout.xml布局文件中聲明Fragment,元素為:<fragment>;也可以在代碼中創建Fragment,然後把它加入到ViewGroup控制項中。然而,Fragment不一定非要放在activity的界面中,它可以隱藏在後台為activity工作。
Android在3.0之後引入了Fragment的概念,主要目的是用在大屏幕設備-例如平板電腦上,以便支持更加動態和靈活的UI設計。平板電腦的屏幕尺寸比手機大得多,因此有更多的空間來存放更多的UI組件,並且這些組件之間會產生更多的交互。Fragment允許這樣的一種設計,而不需要你親自來管理viewhierarchy的復雜變化。通過將Activity的布局分散到Fragment中,你可以在運行時修改Activity的外觀,並在由Activity管理的Back
stack中保存那些變化。
例如,一個新聞應用可以在屏幕的左側使用一個Fragment來展示一個文章的列表,然後在屏幕右側使用另一個Fragment來展示一篇文章,兩個Fragment並排顯示在相同的一個Activity中,並且每一個Fragment擁有它自己的一套生命周期回調方法,並且處理它們自己的用戶輸入事件。因此,取代使用一個activity來選擇一篇文章而另一個activity來閱讀文章的方式,用戶可以在同一個activity中選擇一篇文章並且閱讀,如圖所示
詳細
❸ android 怎麼使用fragment裡面的控制項
一、第一種方法:
(1)Fragment的第一種使用方法是使用fragment載入單獨的布局文件:(也就是xml的方式實現
)activity_main.xml主要是在一個線性布局中添加兩個線性布局
< "http://www.codingke.com" target="_blank"
class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">
right.xml是等會使用fragment的時候,載入的一個布局文件:(由於主要是在界面中載入、所以不作特殊要求)
MyFragment.java就是載入fragment的類,要繼承Fragment類:(要重載父類的下邊三個方法)
二、第二種方法
項目結構和上圖中的差不多:只是在布局文件中,直接使用fragment控制項。
在myfragment.java文件中,只需找到fragment所容納的布局文件即可,不進行業務上的操作。
MainActivity.java文件:進行fragment的業務處理,想了解更多請關注扣丁學堂。
❹ 怎麼調用fragment的private方法
怎麼在一個fragment or 任意類中操作另一個fragment中的方法
www.MyException.Cn 網友分享於:2014-04-20 瀏覽:100次
如何在一個fragment or 任意類中操作另一個fragment中的方法
1 如何在acitivty中執行fragment中的方法:
首先獲得這個Fragment的對象
xxxFragment fragmentObject = (xxxFragment) getFragmentManager.findFragmentByTag("xx");
2 如何在Fragment中執行activity中的方法:
第一種:讓acitivity繼承介面
第二種:在fragment中使用getActivity()但是要加上acitivity的名字,即:
((MainActivity)getActivity()).xxx();
3 如果在fragment中要操作一個fragment,首要要得到這個對象,如何得到?使用getActivity中的FragmentMnager的getFragmentByTag,然後就可以使用這個fragment的對象來操作他的方法了。
當然獲得這個Fragment的時候要轉成這個Fragment的一個對象eg:
FragmentManager fm = getActivity.getSupportFragmentManager();
xxxFragment = (xxxFragment)fm.findFragmentByTag("xxx")
4 如何在任意類中操作一個fragment,首先要得到環境參數,如何得到?
在activity中:
private static WeakReference<ActionButtonActivity> actionButtonActivty = null;
actionButtonActivty = new WeakReference<ActionButtonActivity>(this);
從activity中將這個actionButtonActivity對象傳遞到這個任意類中
asyncTask.setActivity(actionButtonActivty);
在任意類中:
private static WeakReference<ActionButtonActivity> actionButtonActivty; public void setActivity(
WeakReference<ActionButtonActivity> actionButtonActivty) {
this.actionButtonActivty = actionButtonActivty;
}
/**
* this method is invoked on the UI thread after the background computation
* finishes. The result of the background computation is passed to this step
* as a parameter.
*/
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
FragmentManager fm = actionButtonActivty.get().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BFragmentTab_one_event_details bt_det = (BFragmentTab_one_event_details) fm
.findFragmentByTag("2_det");
bt_det.setEvidenceImage(result);
bt_det.setButtonClickable();
ft.addToBackStack(null).commit();
}
❺ 如何使用Fragment與數據綁定
fragment可以在xml中添加,也可以直接新建ListViewFragemnt extends Fragment ,在這種方法的OnCreateView方法中,使用layoutinflator載入xml文件View,返回View,然後就是根據View去初始化ListView,數據直接從資料庫取出,在適配器Adapter中更新顯示數據。
❻ 如何使用fragment載入百度地圖
直接在Fragment中定義BaiMap即可。
1.Fragment是android3.0引入的新控制項,果應用使用在3.0以下、1.6及以上的版本,需要引入v4包。
2.Fragment支持在不同的Activity中使用並且可以處理自己的輸入事件以及生命周期方法等。
3.Fr期方法依賴於Activity的生命周期,例如一個Activity的onPause()的生命周期方法被調用的時候這個Activity中的所有的Fragment的onPause()方法也將被調用。
4.FragmentManager提供了對Activity運行時的Fragment的添加、刪除、替換的操作。在Activity運行期間可以添加Fragment而不是在XML布局文件中進行定義。如果打算在Activity中改變Fragment的生命過程。如果要執行添加、刪除、修改的操作,必須通過FragmentManager的對象獲得一個FragmentTransaction對象,通過它的API來執行這些操作。
5.兩個單獨的Fragment之間是不應該進行通信的。應該使用他們所存在的Activity作為溝通的紐帶。
❼ Fragment詳解
Fragment是android3.0引入的新控制項,如果項目本身就是4.0以上的話,那麼可以直接引用app包下的,FragmentPagerAdapter是ViewPager的適配器。
Fragment是android3.0引入的新控制項,果應用使用在3.0以下、1.6及以上的版本,需要引入v4包。
Fragment支持在不同的Activity中使用並且可以處理自己的輸入事件以及生命周期方法等。
Fragment的生命周期方法依賴於Activity的生命周期,例如一個Activity的onPause()的生命周期方法被調用的時候這個Activity中的所有的Fragment的onPause()方法也將被調用。
FragmentManager提供了對Activity運行時的Fragment的添加、刪除、替換的操作。在Activity運行期間可以添加Fragment而不是在XML布局文件中進行定義。如果打算在Activity中改變Fragment的生命過程。如果要執行添加、刪除、修改的操作,必須通過FragmentManager的對象獲得一個FragmentTransaction對象,通過它的API來執行這些操作。