亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線(xiàn)咨詢(xún)客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了如何不間斷地顯示我的導(dǎo)航欄內(nèi)容?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我從頭開(kāi)始構(gòu)建了一個(gè)可定制的導(dǎo)航抽屜(沒(méi)有使用Android Studio提供的默認(rèn)抽屜)。在我的天氣應(yīng)用程序的導(dǎo)航欄菜單https://i.stack.imgur.com/SIjdx.jpg中,每當(dāng)我選擇菜單上的一個(gè)選項(xiàng)(比如設(shè)置)時(shí),它會(huì)顯示選項(xiàng)的內(nèi)容以及底部的導(dǎo)航視圖和我的活動(dòng)的工具欄內(nèi)容,其中包括導(dǎo)航漢堡圖標(biāo)、編輯文本和搜索按鈕(托管我的3個(gè)片段的活動(dòng)),這會(huì)破壞應(yīng)用程序,使其看起來(lái)非常難看,即https://i.stack.imgur.com/gxj5n.jpg(從該屏幕截圖看,如果實(shí)現(xiàn)良好,整個(gè)內(nèi)容應(yīng)該是空的)。其他欄菜單選項(xiàng)的情況相同。所有我想要的是一個(gè)空白的空間來(lái)工作,我想要的應(yīng)用程序只顯示導(dǎo)航欄內(nèi)容,沒(méi)有睡覺(jué)。示例;https://i.stack.imgur.com/3Jtga.png我應(yīng)該怎么做?

導(dǎo)航菜單的視圖由以下代碼控制(在第185行):

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings_id:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,
                        new Settings()).commit();
                break;
            case R.id.ads_upgrade_id:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,
                        new Upgrade()).commit();
                break;
            case R.id.privacy_policy_id:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,
                        new Privacy_Policy()).commit();
                break;
        }
        drawer.closeDrawer(GravityCompat.START);

        return true;
    }

表示我當(dāng)前正在活動(dòng)上使用片段的容器視圖來(lái)顯示導(dǎo)航菜單內(nèi)容,而我知道這些內(nèi)容肯定是錯(cuò)誤的,那么我應(yīng)該在Replace中使用什么呢?(&p>"Fragment"There)表示我當(dāng)前正在活動(dòng)上使用片段的容器視圖來(lái)顯示我知道是錯(cuò)誤的導(dǎo)航菜單內(nèi)容。我缺乏豐富的經(jīng)驗(yàn),因?yàn)檫@是我第一次開(kāi)發(fā)應(yīng)用程序,我不知疲倦地獨(dú)自花了3個(gè)小時(shí)試圖解決這個(gè)問(wèn)題,但最終失敗了。

這是我的活動(dòng)代碼:

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;
    // Last update time, click sound, search button, search panel.
    TextView timeField;
    MediaPlayer player;
    ImageView Search;
    EditText textfield;
    // For scheduling background image change(using constraint layout, start counting from dubai, down to statue of liberty.
    ConstraintLayout constraintLayout;
    public static int count = 0;
    int[] drawable = new int[]{R.drawable.dubai, R.drawable.norway, R.drawable.eiffel_tower, R.drawable.hong_kong, R.drawable.statue_of_liberty,
            R.drawable.beijing, R.drawable.chicago, R.drawable.colombia, R.drawable.vienna,R.drawable.tokyo};
    Timer _t;

    private WeatherDataViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        // use home activity layout.

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Allow activity to make use of the toolbar

        drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        viewModel = new ViewModelProvider(this).get(WeatherDataViewModel.class);

        // Trigger action to open & close navigation drawer
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar
                , R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        timeField = findViewById(R.id.textView9);
        Search = findViewById(R.id.imageView4);
        textfield = findViewById(R.id.textfield);
        //  find the id's of specific variables.

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
        // host 3 fragments along with bottom navigation.
        final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
        assert navHostFragment != null;
        final NavController navController = navHostFragment.getNavController();
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

        // Make hourly & daily tab unusable
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {

            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                getSupportFragmentManager().popBackStack();
            }
            return false;
        });

        navController.addOnDestinationChangedListener((controller, destination, arguments) -> navController.popBackStack(destination.getId(), false));

        // For scheduling background image change
        constraintLayout = findViewById(R.id.layout);
        constraintLayout.setBackgroundResource(R.drawable.dubai);
        _t = new Timer();
        _t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                // run on ui thread
                runOnUiThread(() -> {
                    if (count < drawable.length) {

                        constraintLayout.setBackgroundResource(drawable[count]);
                        count = (count + 1) % drawable.length;
                    }
                });
            }
        }, 5000, 5000);

        Search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // make click sound when search button is clicked.
                player = MediaPlayer.create(HomeActivity.this, R.raw.click);
                player.start();

                getWeatherData(textfield.getText().toString().trim());
                // make use of some fragment's data

                Fragment currentFragment = navHostFragment.getChildFragmentManager().getFragments().get(0);
                if (currentFragment instanceof FirstFragment) {
                    FirstFragment firstFragment = (FirstFragment) currentFragment;
                    firstFragment.getWeatherData(textfield.getText().toString().trim());
                } else if (currentFragment instanceof SecondFragment) {
                    SecondFragment secondFragment = (SecondFragment) currentFragment;
                    secondFragment.getWeatherData(textfield.getText().toString().trim());
                } else if (currentFragment instanceof ThirdFragment) {
                    ThirdFragment thirdFragment = (ThirdFragment) currentFragment;
                    thirdFragment.getWeatherData(textfield.getText().toString().trim());
                }
            }

            private void getWeatherData(String name) {

                ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);

                Call<Example> call = apiInterface.getWeatherData(name);

                call.enqueue(new Callback<Example>() {
                    @Override
                    public void onResponse(@NonNull Call<Example> call, @NonNull Response<Example> response) {

                        try {
                            assert response.body() != null;
                            timeField.setVisibility(View.VISIBLE);
                            timeField.setText("First Updated:" + " " + response.body().getDt());
                        } catch (Exception e) {
                            timeField.setVisibility(View.GONE);
                            timeField.setText("First Updated: Unknown");
                            Log.e("TAG", "No City found");
                            Toast.makeText(HomeActivity.this, "No City found", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(@NotNull Call<Example> call, @NotNull Throwable t) {
                        t.printStackTrace();
                    }

                });
            }

        });
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings_id:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,
                        new Settings()).commit();
                break;
            case R.id.ads_upgrade_id:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,
                        new Upgrade()).commit();
                break;
            case R.id.privacy_policy_id:
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment,
                        new Privacy_Policy()).commit();
                break;
        }
        drawer.closeDrawer(GravityCompat.START);

        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
            // Open/close drawer animation
        }
    }
}

如果您需要任何其他代碼來(lái)調(diào)查此問(wèn)題,請(qǐng)讓我知道。我只是想避免張貼太多

編輯

我的舊底標(biāo)簽導(dǎo)航圖:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_nav"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.viz.lightweatherforecast.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" />
    <fragment
        android:id="@+id/secondFragment"
        android:name="com.viz.lightpreciseweatherforecast.SecondFragment"
        android:label="fragment_second"
        tools:layout="@layout/fragment_second" />
    <fragment
        android:id="@+id/thirdFragment"
        android:name="com.viz.lightpreciseweatherforecast.ThirdFragment"
        android:label="fragment_third"
        tools:layout="@layout/fragment_third" />
</navigation>

我的新導(dǎo)航條形圖:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bar_nav"
    app:startDestination="@id/firstFragment">

    <fragment
        android:id="@+id/firstFragment"
        android:name="com.viz.lightweatherforecast.FirstFragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_first" />
    <fragment
        android:id="@+id/settings_id"
        android:name="com.viz.lightweatherforecast.Settings"
        android:label="@string/settings"
        tools:layout="@layout/settings" />
    <fragment
        android:id="@+id/ads_upgrade_id"
        android:name="com.viz.lightweatherforecast.Upgrade"
        android:label="@string/upgrade_to_remove_ads"
        tools:layout="@layout/upgrade" />
    <fragment
        android:id="@+id/privacy_policy_id"
        android:name="com.viz.lightweatherforecast.Privacy_Policy"
        android:label="@string/privacy_policy"
        tools:layout="@layout/privacy_policy"/>

</navigation>

推薦答案

您使用的是導(dǎo)航體系結(jié)構(gòu)組件,因此navController應(yīng)該是控制片段事務(wù)的組件,您使用BottomNavigationView進(jìn)行的操作是正確的。

但在navDrawer內(nèi),您正在通過(guò)supportFragmentManager執(zhí)行事務(wù),而應(yīng)通過(guò)navController完成,因?yàn)閮烧咛幚韺?dǎo)航的方式不同。

每當(dāng)我在菜單上選擇一個(gè)選項(xiàng)(比如設(shè)置)時(shí),它都會(huì)顯示該選項(xiàng)的內(nèi)容以及底部導(dǎo)航視圖

這是因?yàn)锽ottomNavView是活動(dòng)的一部分,您需要將其移動(dòng)到片段中;這需要更改應(yīng)用程序的導(dǎo)航設(shè)計(jì);為此,請(qǐng)如下所示更改應(yīng)用程序?qū)Ш剑?/p>

主導(dǎo)航:

<navigation
 ..... >

    <fragment
        android:name="......HomeFragment"/>

    <fragment
        android:name="......SettingFragment"/>

    <fragment
        android:name="......AdsUpgradeFragment"/>
        
    <fragment
        android:name="......PrivacyPolicyFragment"/>        
        
        
</navigation>

HomeFragment是應(yīng)該保存BottomNaviagtionView而不是活動(dòng)的片段;當(dāng)您導(dǎo)航到SettingFragment時(shí),navConroll將替換navHostFragment中的整個(gè)片段,因此BottomNaviagtionView將不會(huì)顯示。

由導(dǎo)航漢堡組成的我的活動(dòng)的工具欄內(nèi)容
圖標(biāo)、編輯文本和搜索按鈕(托管My 3的活動(dòng)
碎片),這會(huì)破壞應(yīng)用程序,使其看起來(lái)非常難看

BottomNaviagtionView不同,您不能對(duì)用作supportActionBar的工具欄執(zhí)行此操作,因?yàn)闉榱烁钠渫庥^而多次設(shè)置supportActionBar將復(fù)制它;因此您必須接受單個(gè)工具欄;但是,您可以在目標(biāo)更改時(shí)隱藏/顯示包含搜索按鈕和EditText的布局:

navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
    LinearLayout searchBar = findViewById(R.id.searchbar); // change searchbar according to the layout id that holds the search button and the EditText
    if (destination.getId() == R.id.nav_home) {
        searchBar.setVisibility(View.VISIBLE);

    } else {
        searchBar.setVisibility(View.GONE);
    }

});

是的,他們當(dāng)前在單擊”上一步”時(shí)退出應(yīng)用程序

要在任何時(shí)候按下任何片段中的底部后退按鈕,請(qǐng)?jiān)谶@些片段的onCreateView()內(nèi)使用OnBackPressedDispatcher()(在您的示例中為SettingFragment、PrivacyPolicyFragment和&amp;AdsUpgradeFragment):

并確保appBarConfiguration沒(méi)有引用這些片段,以便可以顯示”向上”按鈕而不是漢堡。

requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
        @Override
        public void handleOnBackPressed() {
            // Exit the app when back is pressed
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                requireActivity().finishAndRemoveTask();
            else requireActivity().finish();
        }
    });

另外,請(qǐng)確保您的設(shè)置DrawerLayoutnavViewnavController中設(shè)置為:

NavigationView navView = findViewById(....);

appBarConfiguration = new AppBarConfiguration.Builder(
        R.id.nav_home) // remove up button from all these fragments >> Keep the up/back button in R.id.settings_id, R.id.settings_id, ads_upgrade_id, privacy_policy_id
        .setOpenableLayout(drawer)
        .build();

NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);

并在navDrawer中默認(rèn)隱藏Home片段:

navView.getMenu().findItem(R.id.nav_home).setVisible(false); // adjust R.id.nav_home to yours

更新

我目前只有一個(gè)導(dǎo)航圖。FirstFragment表示
今天,第二小時(shí)和第三小時(shí)
我正在使用的是我的其他天氣選項(xiàng)卡,但您建議的那個(gè)
是在導(dǎo)航欄菜單上,我應(yīng)該把你的換成我的,還是
是否為您的建議創(chuàng)建新建議?

您應(yīng)該使用兩個(gè)navGraphs,第一個(gè)是我建議的主導(dǎo)航;第二個(gè)是您已經(jīng)使用的BottomNavigationView導(dǎo)航;這是因?yàn)槲覀儗?code>BottomNavigationView從活動(dòng)布局轉(zhuǎn)移到了主/主頁(yè)片段布局;和it’ recommended the BottomNavigationView should have a separate navGraph;

因此,您現(xiàn)在需要兩個(gè)FragmentContainerView;第一個(gè)在引用此答案中提供的navGraph的活動(dòng)布局中,第二個(gè)在引用BottomNavigationView的原始navGraph的主頁(yè)片段布局中。

示例:

這篇關(guān)于如何不間斷地顯示我的導(dǎo)航欄內(nèi)容?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:不間斷 導(dǎo)航 顯示 欄內(nèi)
用戶(hù)無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定