




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 咖啡店合同经营合同协议
- 快递加盟转让合同协议
- 商业住房买卖合同协议
- 一年级语文上册汉语拼音1aoe练习2无答案新人教版
- 2025至2030年中国超声波多普勒固定式流量计数据监测研究报告
- 山东省临沂市2025届高三一模考试英语试题(解析版)
- 限门式起重机司机操作证过关监控题大全附答案
- 金融机构风险防控管理措施
- 离婚起诉状模板及注意事项
- 教师在书香校园中的角色与心得体会
- 扬尘治理培训课件
- 5《以工匠精神雕琢时代品质》说课稿 2024-2025学年统编版高中语文必修上册
- 2024年新疆区公务员录用考试《行测》真题及答案解析
- 《数字营销》全套教学课件
- 2024年考研政治复习要点解析
- 人美版八年级美术下册《1. 绘画的多元化》说课稿
- 过敏性休克应急预案-2
- 渣土、余土运输服务方案(技术方案)
- 2024ABB电机与发电机业务单元产品手册
- 2024-2030年中国菊芋菊粉行业市场发展趋势与前景展望战略分析报告
- 十字相乘法解一元二次方程练习100题及答案
评论
0/150
提交评论