版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- (新教材)2026年沪科版七年级上册数学 3.5 二元一次方程组的应用 课件
- (新教材)2026年沪科版八年级下册数学 17.4 一元二次方程的根与系数的关系 课件
- 崇义中学高一下学期第一次月考化学试题
- 2025年办公楼网络安装协议
- 售后服务质量评价规范
- 城市云边协同计算
- 专题02大都市圈-冲刺2025年高考地理热点梳理情境对点练
- 基于隐私增强的文件共享协议设计
- 2026 年中职酒店管理与数字化运营(酒店前厅服务)试题及答案
- 类比推理考试题目及答案
- 延保产品推广方案
- 通信工程规划设计
- Hyperion预算管理信息系统介绍
- 手术室中的团队协作与沟通
- 五人制足球技术智慧树知到课后章节答案2023年下电子科技大学
- 涉密人员汇总表
- 其他方便食品(冲调谷物制品)
- S7-200SMARTPLC应用技术PPT完整全套教学课件
- 第三、四单元综合测试卷(含答案)-统编版语文高一下学期必修下册
- 基于短周期价量特征多因子体系
- 山西省2022年高中会考数学考试真题与答案解析
评论
0/150
提交评论