android实现点击按钮切换不同的fragment布局
作者:筱个椰子皮 时间:2022-11-19 10:55:45
本文实例为大家分享了android点击按钮切换不同布局的具体代码,供大家参考,具体内容如下
先上效果图:
如图所示,实现点击下面的按钮切换不同的fragment布局;
不说了,先上主MainActivity代码:
MainActivity.java:
package com.example.xh.twostylefragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//定义5个fragment
private MyFragment f1;
private MyFragment2 f2;
private MyFragment3 f3;
private MyFragment4 f4;
private MyFragment5 f5;
//定义底部5个按钮
private Button foot1;
private Button foot2;
private Button foot3;
private Button foot4;
private Button foot5;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
foot1 = (Button) findViewById(R.id.btn1);//注册按钮
foot2 = (Button) findViewById(R.id.btn2);
foot3 = (Button) findViewById(R.id.btn3);
foot4 = (Button) findViewById(R.id.btn4);
foot5 = (Button) findViewById(R.id.btn5);
foot1.setOnClickListener(this);//对按钮设置监听
foot2.setOnClickListener(this);
foot3.setOnClickListener(this);
foot4.setOnClickListener(this);
foot5.setOnClickListener(this);
//第一次初始化首页默认显示第一个fragment
initFragment1();
}
//显示第一个fragment
private void initFragment1(){
//开启事务,fragment的控制是由事务来实现的
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//第一种方式(add),初始化fragment并添加到事务中,如果为null就new一个
if(f1 == null){
f1 = new MyFragment();
transaction.add(R.id.main_frame_layout, f1);
}
//隐藏所有fragment
hideFragment(transaction);
//显示需要显示的fragment
transaction.show(f1);
//第二种方式(replace),初始化fragment
// if(f1 == null){
// f1 = new MyFragment("首页");
// }
// transaction.replace(R.id.main_frame_layout, f1);
//提交事务
transaction.commit();
}
//显示第二个fragment
private void initFragment2(){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if(f2 == null){
f2 = new MyFragment2();
transaction.add(R.id.main_frame_layout,f2);
}
hideFragment(transaction);
transaction.show(f2);
// if(f2 == null) {
// f2 = new MyFragment("分类");
// }
// transaction.replace(R.id.main_frame_layout, f2);
transaction.commit();
}
//显示第三个fragment
private void initFragment3(){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if(f3 == null){
f3 = new MyFragment3();
transaction.add(R.id.main_frame_layout,f3);
}
hideFragment(transaction);
transaction.show(f3);
// if(f3 == null) {
// f3 = new MyFragment("发现");
// }
// transaction.replace(R.id.main_frame_layout, f3);
transaction.commit();
}
private void initFragment4(){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if(f4 == null){
f4 = new MyFragment4();
transaction.add(R.id.main_frame_layout,f4);
}
hideFragment(transaction);
transaction.show(f4);
// if(f4 == null) {
// f4 = new MyFragment("购物车");
// }
// transaction.replace(R.id.main_frame_layout, f4);
transaction.commit();
}
private void initFragment5(){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if(f5 == null){
f5 = new MyFragment5();
transaction.add(R.id.main_frame_layout,f5);
}
hideFragment(transaction);
transaction.show(f5);
// if(f5 == null) {
// f5 = new MyFragment("我的);
// }
// transaction.replace(R.id.main_frame_layout, f5);
transaction.commit();
}
//隐藏所有的fragment
private void hideFragment(FragmentTransaction transaction){
if(f1 != null){
transaction.hide(f1);
}
if(f2 != null){
transaction.hide(f2);
}
if(f3 != null){
transaction.hide(f3);
}
if(f4 != null){
transaction.hide(f4);
}
if(f5 != null){
transaction.hide(f5);
}
}
@Override
public void onClick(View v) {//点击哪个按钮就显示哪个fragment;
if(v == foot1){
initFragment1();
}else if(v == foot2){
initFragment2();
}else if(v == foot3){
initFragment3();
}else if(v == foot4){
initFragment4();
}else if(v == foot5){
initFragment5();
}
}
}
大家需要创建5个fragment,还有对应的xml文件,这里我给大家展示我创建的MyFragment4.java:
package com.example.xh.twostylefragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 2016/7/8.
*/
public class MyFragment4 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.shoppingcar,container,false);//用view保存shoppingcar.xml布局,大家可以
return view; //自己创建一个xml.
}
}
差不多就是这样的了,有问题大家可以提出来。
来源:https://blog.csdn.net/qq_35595164/article/details/52193086
标签:android,切换布局,fragment
0
投稿
猜你喜欢
详解xamarin Android 实现ListView万能适配器
2022-11-26 02:12:33
C# 表达式目录树Expression的实现
2023-04-03 22:57:32
c#实现最简洁的快速排序(你绝对可以看懂)
2022-04-01 22:35:48
Java全排列算法字典序下的下一个排列讲解
2023-07-30 17:44:39
Android实现屏幕旋转方法总结
2023-04-01 19:37:31
SpringBoot2.x过后static下的静态资源无法访问的问题
2023-07-07 00:21:09
详解C#设置Excel数据自适应行高、列宽的2种情况
2022-10-21 23:46:29
详解Android OkHttp完全解析
2023-04-05 05:05:31
Java缓存池代码实例详解
2021-07-23 07:40:39
ViewFlipper实现文字轮播效果
2022-04-14 11:10:25
基于Spring p标签和c标签注入方式
2023-11-25 22:26:36
解决springboot启动失败的问题('hibernate.dialect' not set)
2023-11-09 03:21:21
如何使用Jenkins构建GIT+Maven项目
2021-11-18 04:42:52
.Net多进程通信共享内存映射文件Memory Mapped
2022-04-30 23:33:57
Android代码实现新年贺卡动画示例详解
2022-09-11 00:28:49
SpringMVC中Controller类数据响应的方法
2021-10-07 21:00:08
C#表达式中的动态查询详解【译】
2021-07-29 05:19:42
Android App开发中ViewPager组件的入门使用教程
2023-04-30 21:45:34
SpringBoot四种读取properties文件的方式(小结)
2023-10-10 03:25:14
SpringBoot入坑笔记之spring-boot-starter-web 配置文件的使用
2021-12-15 13:11:46