版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】activity多個(gè)片段添加切換(沒(méi)有用到viewpager)
MainActivitypackagecom.example.test;importjava.util.ArrayList;importjava.util.List;importandroid.annotation.SuppressLint;importandroid.app.ActionBar;importandroid.os.Bundle;importandroid.support.v4.app.Fragment;importandroid.support.v4.app.FragmentActivity;importandroid.support.v4.app.FragmentTransaction;importandroid.view.View;importandroid.view.View.OnClickListener;publicclassMainActivityextendsFragmentActivityimplementsOnClickListener{ privateintcurrentTab;//當(dāng)前Tab頁(yè)面索引 privateList<Fragment>mFraments=null; @SuppressLint({"NewApi","CommitTransaction"}) @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); initFragment(); } privatevoidinitFragment(){ FragmentTransactionft=getSupportFragmentManager().beginTransaction(); ft.add(R.id.content,mFraments.get(currentTab)); mit();
findViewById(R.id.button1).setEnabled(false); } @SuppressLint("NewApi") privatevoidinitUI(){ ActionBaractionBar=getActionBar(); actionBar.hide(); mFraments=newArrayList<Fragment>(); mFraments.add(newOneFragment()); mFraments.add(newTwoFragment()); mFraments.add(newThreeFragment()); mFraments.add(newFourFragment()); findViewById(R.id.button1).setOnClickListener(this); findViewById(R.id.button2).setOnClickListener(this); findViewById(R.id.button3).setOnClickListener(this); findViewById(R.id.button4).setOnClickListener(this); } /** *切換tab *
*@paramidx */ privatestaticfinalint[]mids={R.id.button1,R.id.button2,R.id.button3,R.id.button4}; publicvoidshowTabById(intid){ for(inti=0;i<mids.length;i++){ if(id==mids[i]){ findViewById(mids[i]).setEnabled(false); Fragmentfragment=mFraments.get(i); FragmentTransactionft=obtainFragmentTransaction(i); fragment.onPause();//暫停當(dāng)前tab if(fragment.isAdded()){ fragment.onResume();//啟動(dòng)目標(biāo)tab的onResume() }else{ ft.add(R.id.content,fragment); //ft.addToBackStack(null);//在commit()方法之前,你//可以調(diào)用addToBackStack(),把這個(gè)transaction加入backstack中去,這個(gè)backstack是由activity管理//的,當(dāng)用戶按返回鍵時(shí),就會(huì)回到上一個(gè)fragment的狀態(tài)。 } ft.show(mFraments.get(i)); ft.hide(mFraments.get(currentTab)); mit(); currentTab=i; }else{ findViewById(mids[i]).setEnabled(true);//被按下的效果 } } } /** *獲取一個(gè)帶動(dòng)畫的FragmentTransaction *
*@paramindex *@return */ privateFragmentTransactionobtainFragmentTransaction(intindex){ FragmentTransactionft=getSupportFragmentManager().beginTransaction(); //設(shè)置切換動(dòng)畫 if(index>currentTab){ ft.setCustomAnimations(R.anim.slide_left_in,R.anim.slide_left_out); }else{ ft.setCustomAnimations(R.anim.slide_right_in,R.anim.slide_right_out); } returnft; } @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub intid=v.getId(); if(id==R.id.button1){ showTabById(id); } if(id==R.id.button2){ showTabById(id); } if(id==R.id.button3){ showTabById(id); } if(id==R.id.button4){ showTabById(id); } }}activity_main<RelativeLayoutxmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.MainActivity"
tools:ignore="MergeRootFrame">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="切換片段1"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/button3"
android:text="切換片段4"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button3">
</FrameLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="29dp"
android:text="切換片段3"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:layout_marginLeft="34dp"
android:text="切換片段2"/>
</RelativeLayout>第一個(gè)片段:/**
*Asimple{@linkandroid.support.v4.app.Fragment}subclass.
*
*/publicclassOneFragmentextendsFragment{ publicOneFragment(){ //Requiredemptypublicconstructor } @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){ //Inflatethelayoutforthisfragment Viewinflate=inflater.inflate(R.layout.fragment_one,container,false); returninflate; }}第二個(gè)片段:/**
*Asimple{@linkandroid.support.v4.app.Fragment}subclass.
*
*/publicclassTwoFragmentextendsFragment{ publicTwoFragment(){ //Requiredemptypublicconstructor } @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){ //Inflatethelayoutforthisfragment Viewinflate=inflater.inflate(R.layout.fragment_one,container,false); ((TextView)inflate.findViewById(R.id.textView1)).setText("2");
returninflate; }}第三個(gè)片段:/**
*Asimple{@linkandroid.support.v4.app.Fragment}subclass.
*
*/publicclassThreeFragmentextendsFragment{ publicTwoFragment(){ //Requiredemptypublicconstructor } @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){ //Inflatethelayoutforthisfragment Viewinflate=inflater.inflate(R.layout.fragment_one,container,false); ((TextView)inflate.findViewById(R.id.textView1)).setText("3");
returninflate; }}第四個(gè)片段:/**
*Asimple{@linkandroid.support.v4.app.Fragment}subclass.
*
*/publicclassFourFragmentextendsFragment{ publicFourFragment(){ //Requiredemptypublicconstructor } @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){ //Inflatethelayoutforthisfragment Viewinflate=inflater.inflate(R.layout.fragment_one,container,false); ((TextView)inflate.findViewById(R.id.textView1)).setText("4");
returninflate; }}fragment_one布局:<RelativeLayoutxmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.OneFragment">
<!--TODO:Updateblankfragmentlayout-->
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"/>
<EditText
android:id="@+id/editText1"
android:layout_width="176dp"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="21dp"
android:layout_marginTop="179dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"/></RelativeLayout>切
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 遺體防腐整容師崗前考核試卷及答案
- 《國(guó)際金融》測(cè)試題附答案
- 全國(guó)獸醫(yī)考試試題及答案
- 醫(yī)師考核法律法規(guī)(醫(yī)院法律法規(guī)考試試題和答案)
- 安全考試試卷及答案大全
- 土建工程師面試試題(含答案)
- 營(yíng)銷培訓(xùn)試題及答案大全
- 消防安全技術(shù)綜合能力測(cè)試題及答案
- 高頻領(lǐng)導(dǎo)力協(xié)會(huì)面試題及答案
- 護(hù)士急診急救試題及答案
- 印刷行業(yè)安全培訓(xùn)班課件
- 《慢性胃炎診療》課件
- 北京市延慶區(qū)2026屆八年級(jí)物理第一學(xué)期期末達(dá)標(biāo)測(cè)試試題含解析
- 繼電器性能測(cè)試及故障診斷方案
- 酒店清欠協(xié)議書模板模板
- 長(zhǎng)者探訪義工培訓(xùn)
- 地下室結(jié)構(gòu)加固技術(shù)方案
- 人教版高一必修二英語(yǔ)單詞表
- 2026年高考數(shù)學(xué)一輪復(fù)習(xí)周測(cè)卷及答案解析:第9周 數(shù)列的概念、等差與等比數(shù)列
- 電廠清潔生產(chǎn)管理制度
- 第五單元第22課-健康生活新設(shè)件人教版初中信息科技八年級(jí)全一冊(cè)
評(píng)論
0/150
提交評(píng)論