亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android實現(xiàn)點擊按鈕切換不同的fragment布局

 更新時間:2021年01月20日 09:55:30   作者:筱個椰子皮  
這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)點擊按鈕切換不同的fragment布局,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了android點擊按鈕切換不同布局的具體代碼,供大家參考,具體內(nèi)容如下

先上效果圖:

如圖所示,實現(xiàn)點擊下面的按鈕切換不同的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);//對按鈕設(shè)置監(jiān)聽
  foot2.setOnClickListener(this);
  foot3.setOnClickListener(this);
  foot4.setOnClickListener(this);
  foot5.setOnClickListener(this);
  //第一次初始化首頁默認(rèn)顯示第一個fragment
  initFragment1();
 }
 
 //顯示第一個fragment
 private void initFragment1(){
  //開啟事務(wù),fragment的控制是由事務(wù)來實現(xiàn)的
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 
  //第一種方式(add),初始化fragment并添加到事務(wù)中,如果為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);
 
  //提交事務(wù)
  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("發(fā)現(xiàn)");
//  }
//  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();
  }
 }
}

大家需要創(chuàng)建5個fragment,還有對應(yīng)的xml文件,這里我給大家展示我創(chuàng)建的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;                     //自己創(chuàng)建一個xml.
 }
 
}

差不多就是這樣的了,有問題大家可以提出來。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android?Jetpack組件Navigation導(dǎo)航組件的基本使用

    Android?Jetpack組件Navigation導(dǎo)航組件的基本使用

    本篇主要簡單介紹了一下?Navigation?是什么?以及使用它的流程是什么,并且結(jié)合實際案例?操作了一番,Navigation?還有很多其他用法,如條件導(dǎo)航、嵌套圖、過度動畫?等等功能?有機會再操作,需要的朋友可以參考下
    2022-06-06
  • Android Compose實現(xiàn)伸縮ToolBar的思路詳解

    Android Compose實現(xiàn)伸縮ToolBar的思路詳解

    這篇文章主要介紹了Android Compose之伸縮ToolBar的實現(xiàn),本文給大家分享主要實現(xiàn)思路及實現(xiàn)過程,通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • android ContentResolver獲取手機電話號碼和短信內(nèi)容

    android ContentResolver獲取手機電話號碼和短信內(nèi)容

    這篇文章主要為大家詳細(xì)介紹了android ContentResolver獲取手機電話號碼、短信內(nèi)容,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android獲取屏幕或View寬度和高度的方法

    Android獲取屏幕或View寬度和高度的方法

    這篇文章主要介紹了Android獲取屏幕或View寬度和高度的方法,涉及Android針對手機屏幕的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-02-02
  • Android獲取本機各種類型文件的方法

    Android獲取本機各種類型文件的方法

    這篇文章主要為大家詳細(xì)介紹了Android獲取本機各種類型文件的方法,包括音樂、視頻、圖片、文檔等,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Kotlin 使用Lambda來設(shè)置回調(diào)的操作

    Kotlin 使用Lambda來設(shè)置回調(diào)的操作

    這篇文章主要介紹了Kotlin 使用Lambda來設(shè)置回調(diào)的操作方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Json數(shù)據(jù)解析模擬美團界面顯示

    Json數(shù)據(jù)解析模擬美團界面顯示

    這篇文章主要介紹了Json數(shù)據(jù)解析模擬美團界面顯示,涉及到j(luò)son數(shù)據(jù)解析相關(guān)知識,本文寫的非常不錯,具有參考價值,特此分享供大家學(xué)習(xí)
    2016-01-01
  • Android Notification使用方法總結(jié)

    Android Notification使用方法總結(jié)

    這篇文章主要介紹了Android Notification使用方法總結(jié)的相關(guān)資料,這里提供了四種使用方法,需要的朋友可以參考下
    2017-09-09
  • Android LeakCanary檢測內(nèi)存泄露原理

    Android LeakCanary檢測內(nèi)存泄露原理

    這篇文章主要介紹了分析LeakCanary檢測內(nèi)存泄露原理,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下
    2021-03-03
  • Android啟動畫面的實現(xiàn)方法

    Android啟動畫面的實現(xiàn)方法

    這篇文章主要介紹了Android啟動畫面的實現(xiàn)方法,分析了布局文件及加載啟動文件的實現(xiàn)方法,非常具有實用價值,需要的朋友可以參考下
    2015-01-01

最新評論