国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

從一個(gè)Activity跳轉(zhuǎn)到另一個(gè)Activity的指定Fragment,附底部菜單欄的實(shí)現(xiàn)

這篇具有很好參考價(jià)值的文章主要介紹了從一個(gè)Activity跳轉(zhuǎn)到另一個(gè)Activity的指定Fragment,附底部菜單欄的實(shí)現(xiàn)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

先實(shí)現(xiàn)底部菜單欄

這部分參考B站視頻Springboot:
2022最新版】Android Studio 安裝 Android(安卓)開發(fā)零基礎(chǔ)入門到精通全套教程P118-119
效果圖:activity界面返回到fragment頁面,android,ui,android studio

  • 忘了需不需要添加依賴了,大概率是本來就有不用添加,但還是把可能有關(guān)的依賴先貼上來
    implementation 'androidx.navigation:navigation-fragment:2.3.5'
    implementation 'androidx.navigation:navigation-ui:2.3.5'
  • 先在res下的menu包里(沒有就建一個(gè)menu包)新建一個(gè)buttom_nav_menu的資源文件(xml文件),代碼為:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/bottom_home"
        android:title="@string/bottom_title_home"
        android:icon="@drawable/home"/>
    <item
        android:id="@+id/bottom_notice"
        android:title="@string/bottom_title_notice"
        android:icon="@drawable/notice"
    />
    <item
        android:id="@+id/bottom_mine"
        android:title="@string/bottom_title_mine"
        android:icon="@drawable/person"/>
    <item
        android:id="@+id/bottom_unfold"
        android:title="@string/bottom_title_unfold"
        android:icon="@drawable/more"
        />

</menu>
  • 然后在HomeActivity的基礎(chǔ)上創(chuàng)建四個(gè)對應(yīng)的fragment文件
  • 然后HomeActivity里的代碼
package com.example.academymanageapp.ui;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.MenuItem;

import com.example.academymanageapp.R;
import com.example.academymanageapp.ui.base.BaseActivity;
import com.example.academymanageapp.ui.home.HomeFragment;
import com.example.academymanageapp.ui.mine.MineFragment;
import com.example.academymanageapp.ui.notice.NoticeFragment;
import com.example.academymanageapp.ui.unfold.UnfoldFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class HomeActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private Fragment[] fragments; //fragment數(shù)組,用來存儲(chǔ)底部菜單欄用到的fragment
    private int lastFragmentIndex = 0;//切換前的fragment
    private int nextFragmentIndex;//切換后的fragment
    private int fragmentFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initViews() {

        //初始化fragments
        fragments = new Fragment[]{new HomeFragment(),new NoticeFragment(), new MineFragment(),new UnfoldFragment()};
        //注冊一個(gè)監(jiān)聽,用來監(jiān)聽用戶點(diǎn)擊底部菜單里的哪一個(gè)fragment
        BottomNavigationView bottomNavigationView = find(R.id.main_bottom_navigation);
        
        //設(shè)置默認(rèn)的
        getSupportFragmentManager().beginTransaction().add(R.id.main_frame,fragments[0]).commit();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    //獲得用戶點(diǎn)擊的menuItem
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//        item.getItemId();//獲得用戶點(diǎn)擊的部件的Id
        item.setChecked(true);//給點(diǎn)中的item設(shè)置checked
        switch (item.getItemId()){
            case R.id.bottom_home:
                switchFragment(0);
                break;
            case R.id.bottom_notice:
                switchFragment(1);
                break;
            case R.id.bottom_mine:
                switchFragment(2);
                break;
            case R.id.bottom_unfold:
                switchFragment(3);
                break;
        }
        return false;
    }

    //默認(rèn)點(diǎn)擊是home,所以要?jiǎng)?chuàng)建一個(gè)fragment的切換
    private void switchFragment(int to){
        if (lastFragmentIndex == to){ //如果切換前后一致則不切換
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        //如果沒有添加過,則添加對應(yīng)的fragment
        if (!fragments[to].isAdded()){
            fragmentTransaction.add(R.id.main_frame,fragments[to]);
        }else {
            fragmentTransaction.show(fragments[to]);//否則就展示出來
        }
        //添加后將之前的隱藏
        fragmentTransaction.hide(fragments[lastFragmentIndex]).commitAllowingStateLoss();
        lastFragmentIndex = to;
    }

}
  • fragment里的代碼:
package com.example.academymanageapp.ui.mine;

import android.view.View;
import androidx.core.content.ContextCompat;
import com.example.academymanageapp.R;
import com.example.academymanageapp.databinding.FragmentMineBinding;
import com.example.academymanageapp.ui.base.BaseFragment;

public class MineFragment extends BaseFragment {
    private FragmentMineBinding binding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentMineBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        return root;
        }
        
    @Override
    protected void initViews() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }
}

其他fragment同理

實(shí)現(xiàn)跳轉(zhuǎn)

參考文章:android 如何從activity跳轉(zhuǎn)到另一個(gè)activity下指定的fragment

問題描述:在做畢設(shè)的時(shí)候,需要實(shí)現(xiàn)點(diǎn)擊返回按鈕時(shí)從activityA(即ActivityDetailActivity)返回到進(jìn)入前的界面(可能是fragmentA(即homeFragment),也有可能是fragmentB(即mineFragment),兩者都屬于HomeActivity)。

思路:從ActivityDetailActivity跳轉(zhuǎn)時(shí),帶flag跳轉(zhuǎn),根據(jù)flag跳轉(zhuǎn)到指定的fragment

ActivityDetailActivity部分的代碼:

 Intent intent = new Intent();
 intent.setClass(ActivityDetailActivity.this,HomeActivity.class);
 intent.putExtra("fragment_flag",2);
 startActivity(intent);

HomeActivity部分只需要對傳回來的值進(jìn)行一個(gè)判斷,根據(jù)值switch到指定的fragment就好,添加的代碼:

fragmentFlag = getIntent().getIntExtra("fragment_flag",0);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (fragmentFlag){
            case 0:
                switchFragment(0);
                //使對應(yīng)的底部菜單欄的Item處于被點(diǎn)擊的狀態(tài)(即點(diǎn)擊都更改顏色)
                bottomNavigationView.setSelectedItemId(R.id.bottom_home);
                //調(diào)用底部菜單欄函數(shù),以實(shí)現(xiàn)根據(jù)點(diǎn)擊底部菜單欄實(shí)現(xiàn)fragment的跳轉(zhuǎn)
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                //記得加break,否則頁面不會(huì)變化
                break;
            case 1:
                switchFragment(1);
                bottomNavigationView.setSelectedItemId(R.id.bottom_notice);
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                break;
            case 2:
                switchFragment(2);
                bottomNavigationView.setSelectedItemId(R.id.bottom_mine);
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                break;
            case 3:
                switchFragment(3);
                bottomNavigationView.setSelectedItemId(R.id.bottom_unfold);
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                break;
        }
        transaction.commit();

此時(shí)HomeActivity的完整代碼是:文章來源地址http://www.zghlxwxcb.cn/news/detail-771345.html

package com.example.academymanageapp.ui;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.ClipData;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.MenuItem;

import com.example.academymanageapp.R;
import com.example.academymanageapp.ui.base.BaseActivity;
import com.example.academymanageapp.ui.home.HomeFragment;
import com.example.academymanageapp.ui.mine.MineFragment;
import com.example.academymanageapp.ui.notice.NoticeFragment;
import com.example.academymanageapp.ui.unfold.UnfoldFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class HomeActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private Fragment[] fragments; //fragment數(shù)組,用來存儲(chǔ)底部菜單欄用到的fragment
    private int lastFragmentIndex = 0;//切換前的fragment
    private int nextFragmentIndex;//切換后的fragment
    private int fragmentFlag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initViews() {

        //初始化fragments
        fragments = new Fragment[]{new HomeFragment(),new NoticeFragment(), new MineFragment(),new UnfoldFragment()};
        //注冊一個(gè)監(jiān)聽,用來監(jiān)聽用戶點(diǎn)擊底部菜單里的哪一個(gè)fragment
        BottomNavigationView bottomNavigationView = find(R.id.main_bottom_navigation);

        fragmentFlag = getIntent().getIntExtra("fragment_flag",0);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (fragmentFlag){
            case 0:
                switchFragment(0);
                //使對應(yīng)的底部菜單欄的Item處于被點(diǎn)擊的狀態(tài)(即點(diǎn)擊都更改顏色)
                bottomNavigationView.setSelectedItemId(R.id.bottom_home);
                //調(diào)用底部菜單欄函數(shù),以實(shí)現(xiàn)根據(jù)點(diǎn)擊底部菜單欄實(shí)現(xiàn)fragment的跳轉(zhuǎn)
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                //記得加break,否則頁面不會(huì)變化
                break;
            case 1:
                switchFragment(1);
                bottomNavigationView.setSelectedItemId(R.id.bottom_notice);
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                break;
            case 2:
                switchFragment(2);
                bottomNavigationView.setSelectedItemId(R.id.bottom_mine);
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                break;
            case 3:
                switchFragment(3);
                bottomNavigationView.setSelectedItemId(R.id.bottom_unfold);
                bottomNavigationView.setOnNavigationItemSelectedListener(this);
                break;
        }
        transaction.commit();

        //設(shè)置默認(rèn)的
        getSupportFragmentManager().beginTransaction().add(R.id.main_frame,fragments[0]).commit();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    //獲得用戶點(diǎn)擊的menuItem
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//        item.getItemId();//獲得用戶點(diǎn)擊的部件的Id
        item.setChecked(true);//給點(diǎn)中的item設(shè)置checked
        switch (item.getItemId()){
            case R.id.bottom_home:
                switchFragment(0);
                break;
            case R.id.bottom_notice:
                switchFragment(1);
                break;
            case R.id.bottom_mine:
                switchFragment(2);
                break;
            case R.id.bottom_unfold:
                switchFragment(3);
                break;
        }
        return false;
    }

    //默認(rèn)點(diǎn)擊是home,所以要?jiǎng)?chuàng)建一個(gè)fragment的切換
    private void switchFragment(int to){
        if (lastFragmentIndex == to){ //如果切換前后一致則不切換
            return;
        }
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        //如果沒有添加過,則添加對應(yīng)的fragment
        if (!fragments[to].isAdded()){
            fragmentTransaction.add(R.id.main_frame,fragments[to]);
        }else {
            fragmentTransaction.show(fragments[to]);//否則就展示出來
        }
        //添加后將之前的隱藏
        fragmentTransaction.hide(fragments[lastFragmentIndex]).commitAllowingStateLoss();
        lastFragmentIndex = to;
    }

}

到了這里,關(guān)于從一個(gè)Activity跳轉(zhuǎn)到另一個(gè)Activity的指定Fragment,附底部菜單欄的實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Android:單Activity多Fragment,Navigation實(shí)現(xiàn)Fragment跳轉(zhuǎn),F(xiàn)ragment之間通過ViewModel共享數(shù)據(jù)

    Android:單Activity多Fragment,Navigation實(shí)現(xiàn)Fragment跳轉(zhuǎn),F(xiàn)ragment之間通過ViewModel共享數(shù)據(jù)

    1、activity_main.xml 2、MainActivity FragmentA:包括SeekBar和一個(gè)按鈕,點(diǎn)擊button跳轉(zhuǎn)到FragmentB FragmentB:SeekBar加一和減一操作的按鈕,一個(gè)返回FragmentA的按鈕,即經(jīng)過加減操作以后,在FragmentA上顯示加減的結(jié)果。 nav_graph.xml 1、fragment_home.xml 2、HomeFragment 1、fragment_detail.xml 2、DetailFragm

    2023年04月08日
    瀏覽(25)
  • Flutter 和 Android原生(Activity、Fragment)相互跳轉(zhuǎn)、傳參

    Flutter 和 Android原生(Activity、Fragment)相互跳轉(zhuǎn)、傳參

    本文主要講解 Flutter 和 Android原生之間,頁面相互跳轉(zhuǎn)、傳參, 但其中用到了 兩端相互通信 的知識(shí), 非常建議 先看完這篇 講解通信的文章 : Flutter 與 Android原生 相互通信:BasicMessageChannel、MethodChannel、EventChannel_flutter eventchannel methodchannel basemessagechan-CSDN博客 當(dāng)前案例 Flu

    2024年02月22日
    瀏覽(29)
  • 【Android】怎么使用一個(gè)ViewModel用在多個(gè)Activity或者Fragment中

    項(xiàng)目需求 在多個(gè)Activity或者Fragment中使用同一個(gè)ViewModel 需求實(shí)現(xiàn) 1.使用ActivityScope或FragmentScope 想在一個(gè)Activity或Fragment中共享ViewModel實(shí)例,可以使用ActivityScope或FragmentScope。這兩種范圍會(huì)根據(jù)它們所綁定的Activity或Fragment自動(dòng)管理ViewModel實(shí)例的生命周期。 例如,創(chuàng)建一個(gè)繼承自

    2024年02月15日
    瀏覽(19)
  • js跳轉(zhuǎn)到指定url

    js跳轉(zhuǎn)到指定url

    js怎么跳轉(zhuǎn)到指定url方法如下: 需求:頁面上點(diǎn)擊按鈕 需要調(diào)用設(shè)備提供的地址

    2024年02月11日
    瀏覽(20)
  • HTML跳轉(zhuǎn)到頁面指定位置

    使用純超鏈接實(shí)現(xiàn) 1. 實(shí)現(xiàn)本頁面跳轉(zhuǎn)到指定位置(a.html),代碼如下: 2. 實(shí)現(xiàn)點(diǎn)擊跳轉(zhuǎn)到新頁面的指定位置,代碼如下: 如果上方有導(dǎo)航條遮擋 ?

    2024年02月12日
    瀏覽(100)
  • vscode快速跳轉(zhuǎn)到指定文件

    在 VS Code 中,您可以使用以下快捷鍵快速跳轉(zhuǎn)到某個(gè)文件: 在 Windows 和 Linux 上,使用快捷鍵? Ctrl + P 。 在 macOS 上,使用快捷鍵? Cmd + P 。 這將打開 \\\"快速打開\\\" 命令框,您可以在其中輸入文件名或路徑來快速跳轉(zhuǎn)到該文件。例如,如果您想跳轉(zhuǎn)到 \\\"index.html\\\" 文件,只需在命令

    2024年02月11日
    瀏覽(21)
  • Nginx精確匹配并跳轉(zhuǎn)到指定路徑

    Nginx精確匹配并跳轉(zhuǎn)到指定路徑

    說明: 1、根據(jù)研發(fā)要求,將/welcome頁面跳轉(zhuǎn)到指定頁面/example 2、請?jiān)贜ginx配置文件server中配置 3、請查閱如下圖所示:https://xxxx/welcom ——https://xxxx/example 說明:將所有精確匹配到/welcom的請求都重定向到/example這個(gè)路徑下,并且以永久的方式進(jìn)行重定向。

    2024年02月12日
    瀏覽(23)
  • 微信短鏈跳轉(zhuǎn)到小程序指定頁面調(diào)試

    微信短鏈跳轉(zhuǎn)到小程序指定頁面調(diào)試

    首先說下背景:后端給了短鏈地址,但是無法跳轉(zhuǎn)到指定頁面。總是在小程序首頁。指定的頁面我們是h5頁面。排查步驟如下: 1、通過快速URL Scheme 編譯。上部普通編譯 下拉找到此選項(xiàng)。 、 2、按照小程序的要求的URL Scheme輸入。另外后端給的短鏈打開之后,拷貝尾綴作為t

    2024年02月10日
    瀏覽(86)
  • Java for循環(huán)標(biāo)簽跳轉(zhuǎn)到指定位置

    大家是否見過這種for循環(huán),在for循環(huán)前加了個(gè)標(biāo)記的: 我之前有一次在公司業(yè)務(wù)代碼中見過有這種寫法的,沒在意,今天在看JDK線程池的代碼時(shí),又看到 ThreadPoolExecutor 的 addWorker 方法中有這種寫法。于是就查了相關(guān)資料,也比較簡單。 總結(jié)下它的用法吧: 上面代碼中的 o

    2024年02月03日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包