安卓中点击不同按钮切换不同到Fragment
来源:互联网 发布:js原生排序 编辑:程序博客网 时间:2024/04/29 17:36
整体效果如下:
实现方式:通过Activity的FragmentManage去实现
首先要先去创建两个布局文件,分别为pay.xml和income.xml,代表两个片段的内容,下面我只是贴了其中一个布局文件的内容
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#445543" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
简单起见我只是把这两个布局文件的background给设置了一下,用于区分。
然后写两个类,分别为PayFragment和IncomeFragment,让它们都去继承Fragment类,重写onCreateView方法,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.pay, null);
return view;
}
Inflate()作用就是将xml定义的一个布局找出来,inflate方法中间那个参数是制定的布局文件。
完成上面的工作后我们就该总布局文件的编写了:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".SecondActivity" ><LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/pay" android:text="支出"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/income" android:text="收入"/></LinearLayout><FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/fl"> </FrameLayout></LinearLayout>然后写一个类去继承Activity:
import android.os.Bundle;import android.app.Activity;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RelativeLayout;public class SecondActivity extends FragmentActivity implements OnClickListener{private Button pay;private Button income;private RelativeLayout rl;private IncomeFragment incomeFragment;private PayFragment payFragment;FragmentManager fm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second); initview();}private void initview(){pay = (Button) findViewById(R.id.pay);income = (Button) findViewById(R.id.income);pay.setOnClickListener(this);income.setOnClickListener(this);fm = getSupportFragmentManager();}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.income:setTabSelection(0);break; case R.id.pay: setTabSelection(1);break;}}private void setTabSelection(int index){FragmentTransaction ft = fm.beginTransaction();hideFragment(ft);switch (index) {case 0:if(incomeFragment==null){incomeFragment = new IncomeFragment();ft.add(R.id.fl, incomeFragment);}else{ft.show(incomeFragment);}break; case 1: if(payFragment==null){ payFragment = new PayFragment(); ft.add(R.id.fl, payFragment); } ft.show(payFragment);break;}ft.commit();}//用于隐藏fragmentprivate void hideFragment(FragmentTransaction ft){if(incomeFragment!=null){ft.hide(incomeFragment);}if(payFragment!=null){ft.hide(payFragment);}}}其中重要是用了FragmentTransaction的show和hide方法,当然,如果用replace方法也能实现,但是replace方法相比而言比用show和hide方法要浪费资源,因为replace方法其实就是remove方法和add方法的结合,当我们加载布局文件后当不需要显示的时候就remove掉,当用的时候再去加载,这个中间要耗资源,如果用show和hide方法的话,如果用到某个布局,那我们就show,不显示布局就hide,这样避免了重复加载。
0 0
- 安卓中点击不同按钮切换不同到Fragment
- android 点击按钮切换不同的fragment布局
- 点击UIButton按钮,按钮切换不同图片
- WPF CM框架下点击不同的按钮切换到不同的界面
- 底部放5个RadioButton,点击切换不同的Fragment
- 安卓底部导航栏点击变色切换不同Fragment
- android 点击底部图片进行切换不同的Fragment
- 利用ImageView + fragment 实现类似QQ消息界面的 点击不同按钮 展示不同界面
- 不同的fragment切换不同的actionbar.
- 点击一个按钮后可以切换按钮的背景图片并执行不同的点击事件
- 点击不同按钮换不同标题栏图标
- storyboard点击一个按钮跳转到不同的控制器
- Fragment互相切换,点击Fragment上的按钮跳转到Fragment简单总结
- Java实现点击按钮切换不同的字,相当于Tab切换
- 点击不同的按钮把同一个表单提交到不同的页面处理的方法
- 点击不同按钮,弹出每个按钮的不同的内容:
- Android的fragment页面点击按钮切换到下一个fragment页面,由主activity控制
- 一个html按钮切换绑定不同函数后,点击时执行多次函数。
- Hibernate中ManyToOne,OneToMany,OneToOne的使用例
- MFC双缓冲典型代码
- sys.modules
- 21javascript异常处理
- JAVA 图片处理
- 安卓中点击不同按钮切换不同到Fragment
- mybatis学习笔记(十)一对多关联表查询
- iOS7-9。获取iOS设备uuid
- HDU3939Sticks and Right Triangle勾股数,容斥,欧拉筛,积性函数
- 【Tjoi2016&Heoi2016】游戏
- javaScript元素位置
- dos编译程序时 使用csc显示不是内部命令
- (国内)基于坐标位置变化的k匿名位置隐私保护方法
- Linux(CentOS)网络流量实时监控(iftop)