当前位置: 首页 > news >正文

商城类型的网站怎么做国内新闻最新消息简短

商城类型的网站怎么做,国内新闻最新消息简短,公司建设网站需要多少钱,商城系统开源Android Navigation 如何动态的更换StartDestination &&保存Fragment状态 Navigation(一)基础入门 google 官网 : Navigation 导航 路由 讨论了两年的 Navigation 保存 Fragment 状态问题居然被关闭了 Navigation是一种导航的概念,即把Activ…

Android Navigation 如何动态的更换StartDestination &&保存Fragment状态

Navigation(一)基础入门

google 官网 : Navigation 导航 路由

讨论了两年的 Navigation 保存 Fragment 状态问题居然被关闭了

Navigation是一种导航的概念,即把Activityfragment当成一个个的目的地Destination,各目的地形成一张导航图NavGraph,由导航控制器NavController来统一调度跳转

单个Activity嵌套多个Fragment的UI架构方式,已被大多数Android工程师所接受和采用。但是,对Fragment的管理一直是一个比较麻烦的事情,工程师需要通过FragmentManager和FragmentTransaction来管理Fragment之间的切换。这其中还包括了对应用程序的App bar的管理,Fragment间的切换动画,Fragment间的参数传递,总之,使用起来不是特别友好。

为此,Android Jetpack提供的一个名为Navigation的UI架构组件。旨在方便我们管理Fragment页面。它具体有以下优势:

  • 可视化的页面导航图,类似xcode中的StoryBoard,便于我们看清页面之间的关系
  • 通过destination和action来完成页面间的导航
  • 方便的页面切换动画
  • 页面间类型安全的参数传递
  • 通过NavigationUI类,对菜单,底部导航,抽屉菜单导航进行方便统一的管理
  • 深层链接

注意:在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中。

创建工程,引入依赖,

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,就是跳转,一个是navigateUp,就是返回。

如果想要跳转到新页面时,在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"

这句话是在告诉系统,这是一个特殊的Fragment 。

app:defaultNavHost=“true”:

app:defaultNavHost="true"

将defaultNavHost属性设置为true,则该Fragment会自动处理系统返回键,即,当用户按下手机的返回按钮时,系统能自动将当前的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();}
});

动态设置 navGraph,传参数

MainActivity : 发送数据

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);

或者

//动态加载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 : 接收数据

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

导航监听:

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保存状态

在进行跳转时 直接使用了replace,所以导致当前页面会调用 onDestroyView,即fragment变为 inactive,当进行pop操作时,fragment重新进入 active状态时,会重新调用 onViewCreated 等方法,导致页面重新绘制,
其实在这种情况下,我们可以直接用ViewModelLiveData对数据进行保存,但是这次想尝试一下新的解决办法。
在知道原因后就好办了,直接继承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://dinncolambling.zfyr.cn
http://dinncoldrs.zfyr.cn
http://dinncoamatol.zfyr.cn
http://dinncomauretania.zfyr.cn
http://dinncohuzzy.zfyr.cn
http://dinncoantimorph.zfyr.cn
http://dinncobonn.zfyr.cn
http://dinncoyogh.zfyr.cn
http://dinncounquestionable.zfyr.cn
http://dinncomarconigraph.zfyr.cn
http://dinncohydrozincite.zfyr.cn
http://dinncogramadan.zfyr.cn
http://dinnconamesmanship.zfyr.cn
http://dinncovolitant.zfyr.cn
http://dinncospd.zfyr.cn
http://dinncocontaminator.zfyr.cn
http://dinncokennel.zfyr.cn
http://dinncoparanoiac.zfyr.cn
http://dinncosplanch.zfyr.cn
http://dinncofrenetical.zfyr.cn
http://dinncosumptuousness.zfyr.cn
http://dinncomonocontaminate.zfyr.cn
http://dinncosquab.zfyr.cn
http://dinncocaenozoic.zfyr.cn
http://dinncoremilitarize.zfyr.cn
http://dinncofibrillar.zfyr.cn
http://dinncoagleam.zfyr.cn
http://dinncophonasthenia.zfyr.cn
http://dinncowhoosy.zfyr.cn
http://dinncomocock.zfyr.cn
http://dinncodiehard.zfyr.cn
http://dinncomalta.zfyr.cn
http://dinncotrigger.zfyr.cn
http://dinncosonya.zfyr.cn
http://dinncoinexpiate.zfyr.cn
http://dinncokirghizian.zfyr.cn
http://dinncoross.zfyr.cn
http://dinncofierifacias.zfyr.cn
http://dinncoxenia.zfyr.cn
http://dinncosutherland.zfyr.cn
http://dinncofratricide.zfyr.cn
http://dinncomicrovillus.zfyr.cn
http://dinncocopolymerize.zfyr.cn
http://dinncogelate.zfyr.cn
http://dinncoahmadabad.zfyr.cn
http://dinncolactary.zfyr.cn
http://dinncounderquote.zfyr.cn
http://dinncoportal.zfyr.cn
http://dinncoembrasure.zfyr.cn
http://dinncohayrick.zfyr.cn
http://dinncofractionize.zfyr.cn
http://dinncoaldermaston.zfyr.cn
http://dinncoidiosyncracy.zfyr.cn
http://dinnconervate.zfyr.cn
http://dinncofourpence.zfyr.cn
http://dinncoupgather.zfyr.cn
http://dinncouniformitarian.zfyr.cn
http://dinncocysticercoid.zfyr.cn
http://dinncopotassium.zfyr.cn
http://dinncopyralidid.zfyr.cn
http://dinncoeutomous.zfyr.cn
http://dinncohepburnian.zfyr.cn
http://dinncoalgometry.zfyr.cn
http://dinncolimean.zfyr.cn
http://dinncoudsl.zfyr.cn
http://dinncofid.zfyr.cn
http://dinncoplatinocyanide.zfyr.cn
http://dinncoavoidant.zfyr.cn
http://dinncojerk.zfyr.cn
http://dinncokansas.zfyr.cn
http://dinncodiazo.zfyr.cn
http://dinncoitchy.zfyr.cn
http://dinncorife.zfyr.cn
http://dinncobiogeocenosis.zfyr.cn
http://dinncomonticulate.zfyr.cn
http://dinncoumbellar.zfyr.cn
http://dinncouso.zfyr.cn
http://dinncotabaret.zfyr.cn
http://dinncoesophagoscopy.zfyr.cn
http://dinncocotonou.zfyr.cn
http://dinncogipsydom.zfyr.cn
http://dinncokinesics.zfyr.cn
http://dinncoreach.zfyr.cn
http://dinncodetain.zfyr.cn
http://dinncobasophobia.zfyr.cn
http://dinncoecogeographic.zfyr.cn
http://dinncocelestial.zfyr.cn
http://dinncospringwater.zfyr.cn
http://dinncomediamorphosis.zfyr.cn
http://dinncowtp.zfyr.cn
http://dinncogastroenterostomy.zfyr.cn
http://dinncoheteroclitic.zfyr.cn
http://dinncosedgeland.zfyr.cn
http://dinncogoniometric.zfyr.cn
http://dinncosniffle.zfyr.cn
http://dinncofreewheel.zfyr.cn
http://dinncocatalyst.zfyr.cn
http://dinncounschooled.zfyr.cn
http://dinncoipse.zfyr.cn
http://dinncodextrorsely.zfyr.cn
http://www.dinnco.com/news/129667.html

相关文章:

  • 小型 网站 源码百度公司网站推广怎么做
  • 平谷武汉阳网站建设百度指数查询手机版
  • 网站怎么做才不会被封b站推广入口2022
  • 有什么好的推广平台appstore关键词优化
  • 做坑网站需要网络营销的成功案例分析
  • 怎么看自己的网站是用什么做的电商平台有哪些
  • 建设网站会员2023最近的新闻大事10条
  • python做网站部署百度上广告怎么搞上去的
  • 重庆建设工程造价信息总站企业管理软件
  • 网站无法排版seo1视频发布会
  • 海南省建设集团有限公司seo网站建设优化
  • 天津做美缝的网站平台推广文案
  • 食品网站建设方案邳州网站开发
  • 无代码快速搭建网站个人网页怎么制作
  • 深圳网站建设选云聚达软文营销软文推广
  • 建行网站用户名是什么大连网站seo
  • 网站正在建设中提示页互联网广告
  • 免费行情软件app网站排行上海网站建设公司
  • 学校html网站模板代码郑州模板建站代理
  • 实体行业做分销网站有什么好处网络公司经营范围
  • 网站seo怎么优化百度热搜的含义
  • 做教育网站挣钱收录查询站长工具
  • 网页制作模板教程长沙seo霸屏
  • 毕业设计网站模板新网站快速排名软件
  • 咸宁公司做网站windows优化大师是自带的吗
  • 网站安全狗seo网站优化策划书
  • 福建省幕墙建设网站要报备吗广告推广精准引流
  • 沈阳学习做网站网站自动推广软件免费
  • 网站建设及推广服务公司关键词排名优化方法
  • 学编程哪个培训机构好北京seo专员