国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當前位置: 首頁 > news >正文

淘寶客優(yōu)惠券網(wǎng)站怎么做的google怎么推廣

淘寶客優(yōu)惠券網(wǎng)站怎么做的,google怎么推廣,小程序搭建多少錢一個,微信廣告投放收費標準Android Navigation 如何動態(tài)的更換StartDestination &&保存Fragment狀態(tài) Navigation(一)基礎入門 google 官網(wǎng) : Navigation 導航 路由 討論了兩年的 Navigation 保存 Fragment 狀態(tài)問題居然被關閉了 Navigation是一種導航的概念,即把Activ…

Android Navigation 如何動態(tài)的更換StartDestination &&保存Fragment狀態(tài)

Navigation(一)基礎入門

google 官網(wǎng) : Navigation 導航 路由

討論了兩年的 Navigation 保存 Fragment 狀態(tài)問題居然被關閉了

Navigation是一種導航的概念,即把Activityfragment當成一個個的目的地Destination,各目的地形成一張導航圖NavGraph,由導航控制器NavController來統(tǒng)一調(diào)度跳轉(zhuǎn)

單個Activity嵌套多個Fragment的UI架構(gòu)方式,已被大多數(shù)Android工程師所接受和采用。但是,對Fragment的管理一直是一個比較麻煩的事情,工程師需要通過FragmentManager和FragmentTransaction來管理Fragment之間的切換。這其中還包括了對應用程序的App bar的管理,Fragment間的切換動畫,Fragment間的參數(shù)傳遞,總之,使用起來不是特別友好。

為此,Android Jetpack提供的一個名為Navigation的UI架構(gòu)組件。旨在方便我們管理Fragment頁面。它具體有以下優(yōu)勢:

  • 可視化的頁面導航圖,類似xcode中的StoryBoard,便于我們看清頁面之間的關系
  • 通過destination和action來完成頁面間的導航
  • 方便的頁面切換動畫
  • 頁面間類型安全的參數(shù)傳遞
  • 通過NavigationUI類,對菜單,底部導航,抽屜菜單導航進行方便統(tǒng)一的管理
  • 深層鏈接

注意:在Android Studio3.2及以上版本才能支持Navigation特性。
本文所說的“頁面”包括了Fragment和Activity,但主要是Fragment,因為Navigation組件的主要目地就是方便我們在一個Activity中對多個Fragment進行管理。
首先,我們需要先對Navigation有一個大致的了解。

Navigation Graph

這是一種新型的XML資源文件,里面包含了應用程序所有的頁面及頁面之間的關系

NavHostFragment

這是一個特殊的布局文件,Navigation Graph中的頁面通過該Fragment展示

NavController

這是一個Java/Kotlin對象,用于在代碼中完成Navigation Graph中具體的頁面切換

當你想要切換頁面的時候,使用NavController對象,告訴它你想要去Navigation Graph中的哪個頁面,NavController會將相關的頁面展示在NavHostFragment中。

創(chuàng)建工程,引入依賴,

android {compileSdkVersion 30buildToolsVersion "30.0.3"defaultConfig {applicationId "com.xq.mybottomnavigation"minSdkVersion 29targetSdkVersion 30versionCode 1versionName "1.0"}buildFeatures {viewBinding true}
}dependencies {implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'com.google.android.material:material:1.2.1'implementation 'androidx.constraintlayout:constraintlayout:2.0.1'testImplementation 'junit:junit:4.+'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'implementation 'androidx.navigation:navigation-fragment:2.0.0'implementation 'androidx.navigation:navigation-ui:2.0.0'
}

MainActivity和布局 :

import android.os.Bundle;import com.google.android.material.bottomnavigation.BottomNavigationView;import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;import com.xq.mybottomnavigation.databinding.ActivityMainBinding;public class MainActivity extends AppCompatActivity {private ActivityMainBinding binding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding = ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());BottomNavigationView navView = findViewById(R.id.nav_view);// Passing each menu ID as a set of Ids because each// menu should be considered as top level destinations.//獲取App bar配置:AppBarConfigurationAppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);//將NavController和AppBarConfiguration進行綁定NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);//將需要交互的App barUI與NavController和AppBarConfiguration進行綁定NavigationUI.setupWithNavController(binding.navView, navController);}}
  • navigation pop 和 push的時候 對Fragment的 操作是 replace,所以會導致生命周期重新走一遍

  • 其實Navigation使用很簡單,navigation和activity(確切的說是Fragment)綁定之后,使用兩個方法就行,一個是navigate,就是跳轉(zhuǎn),一個是navigateUp,就是返回。

如果想要跳轉(zhuǎn)到新頁面時,在Fragment中使用:

NavHostFragment.findNavController(this).navigate(destinationID, bundle);
NavHostFragment.findNavController(this).navigateUp();

布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingTop="?attr/actionBarSize"><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/nav_view"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="0dp"android:layout_marginEnd="0dp"android:background="?android:attr/windowBackground"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:menu="@menu/bottom_nav_menu" /><fragmentandroid:id="@+id/nav_host_fragment_activity_main"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:layout_constraintBottom_toTopOf="@id/nav_view"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:navGraph="@navigation/mobile_navigation" /></androidx.constraintlayout.widget.ConstraintLayout>

NavHostFragment:

android:name="androidx.navigation.fragment.NavHostFragment"

這句話是在告訴系統(tǒng),這是一個特殊的Fragment 。

app:defaultNavHost=“true”:

app:defaultNavHost="true"

將defaultNavHost屬性設置為true,則該Fragment會自動處理系統(tǒng)返回鍵,即,當用戶按下手機的返回按鈕時,系統(tǒng)能自動將當前的Fragment推出。

app:navGraph=“@navigation/nav_graph”:

app:navGraph="@navigation/nav_graph"

設置該Fragment對應的導航圖 。

導航圖文件 :@navigation/mobile_navigation

<?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/mobile_navigation"app:startDestination="@+id/navigation_home"><fragmentandroid:id="@+id/navigation_home"android:name="com.xq.mybottomnavigation.ui.home.HomeFragment"android:label="@string/title_home"tools:layout="@layout/fragment_home" /><fragmentandroid:id="@+id/navigation_dashboard"android:name="com.xq.mybottomnavigation.ui.dashboard.DashboardFragment"android:label="@string/title_dashboard"tools:layout="@layout/fragment_dashboard" /><fragmentandroid:id="@+id/navigation_notifications"android:name="com.xq.mybottomnavigation.ui.notifications.NotificationsFragment"android:label="@string/title_notifications"tools:layout="@layout/fragment_notifications" />
</navigation>

fragment

三個fragment類似

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;import com.xq.mybottomnavigation.R;
import com.xq.mybottomnavigation.databinding.FragmentHomeBinding;public class HomeFragment extends Fragment {private HomeViewModel homeViewModel;private FragmentHomeBinding binding;public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {homeViewModel =new ViewModelProvider(this).get(HomeViewModel.class);binding = FragmentHomeBinding.inflate(inflater, container, false);View root = binding.getRoot();final TextView textView = binding.textHome;homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {@Overridepublic void onChanged(@Nullable String s) {textView.setText(s);}});return root;}@Overridepublic void onDestroyView() {super.onDestroyView();binding = null;}}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ui.home.HomeFragment"><TextViewandroid:id="@+id/text_home"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:layout_marginEnd="8dp"android:textAlignment="center"android:textSize="20sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

其他

Activity中導航到指定fragment

//方式一 、 通過NavController
NavController controller = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
binding.btn1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {controller.navigate(R.id.navigation_notifications);}
});//方式二 、 通過NavHostFragment
NavHostFragment fragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_activity_main);
binding.btn2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (fragment != null) {NavHostFragment.findNavController(fragment).navigate(R.id.navigation_notifications);}}
});

NotificationsFragment 返回上一級

textView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {NavHostFragment.findNavController(NotificationsFragment.this).navigateUp();}
});

動態(tài)設置 navGraph,傳參數(shù)

MainActivity : 發(fā)送數(shù)據(jù)

NavController controller = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
NavInflater navInflater = controller.getNavInflater();
NavGraph navGraph = navInflater.inflate(R.navigation.mobile_navigation);
navGraph.setStartDestination(R.id.navigation_notifications);//初始界面Bundle args = new Bundle();
args.putBoolean("6no6", true);//傳參navController.setGraph(navGraph, args);

或者

//動態(tài)加載setGraph
FragmentManager manager = getSupportFragmentManager();
NavHostFragment hostFragment = (NavHostFragment) manager.findFragmentById(R.id.nav_host_fragment_activity_main);
NavController controller = null;
if (hostFragment != null) {controller = hostFragment.getNavController();
}
navController.setGraph(R.navigation.mobile_navigation);

NotificationsFragment : 接收數(shù)據(jù)

Bundle arguments = getArguments();
if (arguments != null) {boolean aBoolean = arguments.getBoolean("6no6");textView.setTextColor(aBoolean ? Color.RED : Color.BLUE);
}

導航監(jiān)聽:

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {@Overridepublic void onDestinationChanged(@NonNull @NotNull NavController controller,@NonNull @NotNull NavDestination destination,@Nullable Bundle arguments) {CharSequence label = destination.getLabel();Log.e(TAG, "onDestinationChanged: ===="+label);}
});

切換時使Fragment保存狀態(tài)

在進行跳轉(zhuǎn)時 直接使用了replace,所以導致當前頁面會調(diào)用 onDestroyView,即fragment變?yōu)?inactive,當進行pop操作時,fragment重新進入 active狀態(tài)時,會重新調(diào)用 onViewCreated 等方法,導致頁面重新繪制,
其實在這種情況下,我們可以直接用ViewModelLiveData對數(shù)據(jù)進行保存,但是這次想嘗試一下新的解決辦法。
在知道原因后就好辦了,直接繼承FragmentNavigator把方法重寫

public NavDestination navigate(@NonNull Destination destination, @Nullable Bundle args,@Nullable NavOptions navOptions, @Nullable Navigator.Extras navigatorExtras) {//      ft.replace(mContainerId, frag);//      change to  if(mFragmentManager.getFragments().size()>0){ft.hide(mFragmentManager.getFragments().get(mFragmentManager.getFragments().size()-1));ft.add(mContainerId, frag);}else {ft.replace(mContainerId, frag);}}

KeepStateFragmentNavigator 使用如下:

NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
navController.getNavigatorProvider().addNavigator(new KeepStateFragmentNavigator(this, navHostFragment.getChildFragmentManager(), R.id.nav_host_fragment));
NavHostFragment.findNavController(this).navigate(destinationID, bundle);
http://aloenet.com.cn/news/30341.html

相關文章:

  • 怎么在網(wǎng)上做公司網(wǎng)站如何創(chuàng)建自己的網(wǎng)址
  • 建設兵團12師教育局網(wǎng)站淘寶一個關鍵詞要刷多久
  • wordpress自己寫代碼百度自然排名優(yōu)化
  • 煙臺龍口網(wǎng)站建設百度收錄最新方法
  • 天津建設銀行網(wǎng)站深圳百度推廣代理商
  • 保定建設信息網(wǎng)站百度外推代發(fā)排名
  • 有一個網(wǎng)站叫浪什么網(wǎng)站優(yōu)化比較好的公司
  • 導購網(wǎng)站怎么做有特色友情鏈接頁面
  • 網(wǎng)站怎么做 吸引人上海做關鍵詞推廣企業(yè)
  • 如何做網(wǎng)站推廣的方案設計企業(yè)推廣哪個平臺好
  • web網(wǎng)站開發(fā)的設計思想公司域名查詢官網(wǎng)
  • 做企業(yè)網(wǎng)站首頁尺寸百度關鍵詞網(wǎng)站排名優(yōu)化軟件
  • 如何在導航網(wǎng)站上做鏈接優(yōu)化近義詞
  • 做網(wǎng)站外包網(wǎng)頁設計與制作代碼
  • 長沙建站公司模板百度seo快速排名優(yōu)化軟件
  • 福建省華榮建設集團有限公司網(wǎng)站長春網(wǎng)絡推廣優(yōu)化
  • 個人網(wǎng)站logo青島網(wǎng)站制作推廣
  • 免費域名查詢網(wǎng)站推廣app最快的方法
  • 創(chuàng)建一個網(wǎng)頁多少錢sem優(yōu)化托管
  • wordpress ios 默認合肥seo網(wǎng)站排名
  • 專注做蔬菜的網(wǎng)站汕頭seo管理
  • 定制網(wǎng)站費用南寧seo專員
  • 如何設計酒店網(wǎng)站建設seo建站優(yōu)化推廣
  • 羅湖高端網(wǎng)站設計推廣普通話手抄報一等獎
  • 專門做蛋糕視頻的網(wǎng)站流程優(yōu)化四個方法
  • linux系統(tǒng)網(wǎng)站架構(gòu)淘寶網(wǎng)站的推廣與優(yōu)化
  • 網(wǎng)絡營銷做女鞋的網(wǎng)站設計seo的形式有哪些
  • 武漢服務好的建站平臺公司愛站網(wǎng)絡挖掘詞
  • 做網(wǎng)站上饒百度數(shù)據(jù)查詢
  • wordpress首頁制作免費下載優(yōu)化大師