XSuperMES移動端要運行在各種各樣的設備中,有小屏幕的手機,超大屏的平板甚至電視。針對屏幕尺寸的差距,很多情況下,都是先針對手機開發一套app,然後拷貝一份,修改佈局以適應什麼超級大屏的。難道無法做到一個app可以同時適應手機和平板嗎?答案是,當然有,那就是Fragment.Fragment出現的初衷就是爲了解決這樣的問題。
你可以把Fragment當成Activity一個界面的一部分,甚至Activity的界面由完全不同的Fragment組成,更帥氣的是Fragment有自己的聲明週期和接收、處理用戶的事件,這樣就不必要在一個Activity裏面寫一堆事件、控件的代碼了。更爲重要的是,你可以動態的添加、替換、移除某個Fragment。
1、如何使用fragment activity
要想用Fragment 功能必須先讓activity繼承FragmentActivity,其原因是裏面包含了Fragment運作的FragmentManager接口的實現類 FragmentManagerImpl ,由這個類管理所有Fragment的顯示、隱藏
1.使用最簡單的Fragment,我們只要繼承Fragment就可以
public classTextFragment extends Fragment{
privateString mMsg;
public voidsetMessage(String message){
this.mMsg = message;
}
@Override
public ViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODOAuto-generated method stub
finalContext context = getActivity();
FrameLayout root = new FrameLayout(context);
root.setBackgroundColor(Color.YELLOW);
TextViewtv = new TextView(context);
tv.setText(mMsg);
tv.setGravity(Gravity.CENTER);
root.addView(tv, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));
returnroot;
}
}
首先Fragment 就可以把它當作一個view , 只不過這個view 與 activity一樣有了生命週期函數
Fragment.onCreateView() 函數就是用於生成這個Fragment佈局的view的,類似baseadapter.getView()
這樣一個包含一個TextView的簡單佈局就完成了。
2.重寫我們自己的FragmentActivity.
這裏面主要要通過FragmentManager 來進行Fragment的添加和刪除:
public class TextFragment extendsFragment{
private String mMsg;
public void setMessage(Stringmessage){
this.mMsg = message;
}
@Override
public ViewonCreateView(LayoutInflater inflater, ViewGroup container,
BundlesavedInstanceState) {
// TODO Auto-generatedmethod stub
final Context context =getActivity();
FrameLayout root = newFrameLayout(context);
root.setBackgroundColor(Color.YELLOW);
TextView tv = new TextView(context);
tv.setText(mMsg);
tv.setGravity(Gravity.CENTER);
root.addView(tv, newFrameLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));
return root;
}
}
首先Fragment 就可以把它當作一個view , 只不過這個view 與 activity一樣有了生命週期函數
public class DoorFragmentActivityextends FragmentActivity{
public static final StringFRAG_SMS = "sms_list_frag";
public static final StringFRAG_TEXT = "text_frag";
private FragmentmSMSFragment;
private FragmentmTextFragment;
private FragmentManagermFragMgr;
private Button mMenuBtn;
@Override
protected void onCreate(BundlesavedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.door_fragment_activity_layout);
mFragMgr =getSupportFragmentManager();
mMenuBtn = (Button)findViewById(R.id.door_menu_btn);
mMenuBtn.setOnClickListener(new OnClickListener() {
@Override
public voidonClick(View v) {
// TODOAuto-generated method stub
showFragments(FRAG_TEXT, true);
}
});
mMenuBtn.setOnLongClickListener(new OnLongClickListener() {
@Override
public booleanonLongClick(View v) {
// TODOAuto-generated method stub
return false;
}
});
initFragments();
showFragments(FRAG_SMS,false);
}
private voidinitFragments(){
mSMSFragment = newSMSListFragment();
TextFragment textfrag =new TextFragment();
textfrag.setMessage("這是 菜單界面");
mTextFragment =textfrag;
}
private voidshowFragments(String tag, boolean needback){
FragmentTransaction trans= mFragMgr.beginTransaction();
if(needback){
trans.setCustomAnimations(R.anim.frag_enter,
R.anim.frag_exit);
trans.add(R.id.door_root_content_fl, getFragmentByTag(tag), tag);
trans.addToBackStack(tag);
}else{
trans.replace(R.id.door_contents_fl, getFragmentByTag(tag), tag);
}
trans.commit();
}
private FragmentgetFragmentByTag(String tag){
if(FRAG_SMS.equals(tag)){
return mSMSFragment;
}
if(FRAG_TEXT.equals(tag)){
returnmTextFragment;
}
return null;
}
}
-
首先我們獲取FragmentManager實現:直接調用 FragmentActivity.getSupportFragmentManager(),看源碼可以知道這返回的是FragmentManager內部定義的實現類FragmentManagerImpl。
-
我們獲取了FragmentManagerImpl後我們其實不咋操作這個類,只調用FragmentManager.beginTransation(),這個獲取FragmentTransation接口的實現類(裏面具體是BackStackRecord類的實例),我們關於Fragment的所有操作都是通過它來完成的,因爲沒仔細研究,我只瞭解直接自己在代碼裏面定義Fragment而沒有在xml裏面寫(xml寫覺得有點彆扭)
我們主要通過 FragmentTransation的一些方法來處理Fragment的:
1) trans.add(fragment, tag); 這個實際是
containerViewId = 0 調用的3)
2) trans.add(containerViewId, fragment); 這個實際是 tag = null
調用的 3)
3) trans.add(containerViewId, fragment, tag); 如果containerViewId != 0實際上調用的是獲取到
fragment的 onCreateView方法返回的view 並加入到containerViewId這個viewgroup中去即 viewgroup.addView(fragment.onCreateView());
未解決問題:containerViewId = 0 的時候代表什麼??
4) trans.replace(containerViewId, fragment) 一樣是null
tag調用 5)
5) trans.replace(containerViewId, fragment, tag) 這個一樣是添加一個fragment到對應的container中去,只不過比add多了一步對相同containerViewId中已有的fragment檢索,進行removeFragment操作,再去添加這個新來的fragment
6) trans.addToBackStack(tag); 如果你的fragment對於back鍵有類似activity的回退響應,就要記得把它加入到裏面去,trans裏面模擬了棧,但是我的回退沒有響應我設置的exitanim 這個無語還沒解決
3.再使用下ListFragment,我這裏寫的是SMSListFragment繼承了ListFragment:
public class SMSListFragment extends ListFragment{
privateConversationListAdapter mAdapter;
private ConversationQuerymQuery;
private long startTime;
@Override
public void onCreate(BundlesavedInstanceState) {
// TODO Auto-generatedmethod stub
super.onCreate(savedInstanceState);
mAdapter = newConversationListAdapter(getActivity());
mQuery = newConversationQuery(getActivity().getContentResolver());
}
@Override
public voidonActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generatedmethod stub
super.onActivityCreated(savedInstanceState);
setListAdapter(mAdapter);
}
@Override
public void onStart() {
// TODO Auto-generatedmethod stub
super.onStart();
startAsyncQuery();
}
@Override
public void onStop() {
// TODO Auto-generatedmethod stub
super.onStop();
mAdapter.getCursor().close();
mAdapter.changeCursor(null);
}
public void startAsyncQuery(){
startTime =System.currentTimeMillis();
mQuery.startQuery(1, null,Conversation.sAllThreadsUri,
Conversation.ALL_THREADS_PROJECTION, null, null,
Conversation.CONVERSATION_ORDER);
}
private final classConversationQuery extends AsyncQueryHandler {
public ConversationQuery(ContentResolvercr) {
super(cr);
// TODO Auto-generatedconstructor stub
}
@Override
protected voidonQueryComplete(int token, Object cookie, Cursor cursor) {
// TODO Auto-generatedmethod stub
System.out.println("conversation cursor size : "
+cursor.getCount());
mAdapter.changeCursor(cursor);
Toast.makeText( getActivity(),
"查詢短信會話個數:" + cursor.getCount() + ",花費"
+(System.currentTimeMillis() - startTime) + " ms",
Toast.LENGTH_LONG).show();
}
}
}
代碼中可以知道和使用普通的ListActivity完全沒區別,
onCreate()中完成自己要一次性初始的東西,我在裏面主要是初始化一個adapter和一個對sms數據庫的查詢
在onActivityCreated()中將adapter設置給listview,這個不確定有沒有更好的位置,
然後進入我們熟悉的生命週期方法:
onStart()中,開啓查詢
onStop()中,我們界面已經不在顯示了,所以我們不關心數據庫變化了,closecursor
-
主頁面的佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/door_root_content_fl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/door_contents_fl"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal" >
<Button
android:id="@+id/door_menu_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="菜單"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>