【移动应用开发技术】activity多个片段添加切换(没有用到viewpager)_第1页
【移动应用开发技术】activity多个片段添加切换(没有用到viewpager)_第2页
【移动应用开发技术】activity多个片段添加切换(没有用到viewpager)_第3页
【移动应用开发技术】activity多个片段添加切换(没有用到viewpager)_第4页
【移动应用开发技术】activity多个片段添加切换(没有用到viewpager)_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】activity多个片段添加切换(没有用到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;//当前Tab页面索引 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();//暂停当前tab if(fragment.isAdded()){ fragment.onResume();//启动目标tab的onResume() }else{ ft.add(R.id.content,fragment); //ft.addToBackStack(null);//在commit()方法之前,你//可以调用addToBackStack(),把这个transaction加入backstack中去,这个backstack是由activity管理//的,当用户按返回键时,就会回到上一个fragment的状态。 } ft.show(mFraments.get(i)); ft.hide(mFraments.get(currentTab)); mit(); currentTab=i; }else{ findViewById(mids[i]).setEnabled(true);//被按下的效果 } } } /** *获取一个带动画的FragmentTransaction *

*@paramindex *@return */ privateFragmentTransactionobtainFragmentTransaction(intindex){ FragmentTransactionft=getSupportFragmentManager().beginTransaction(); //设置切换动画 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>第一个片段:/**

*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; }}第二个片段:/**

*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; }}第三个片段:/**

*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; }}第四个片段:/**

*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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论