Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android to realize the main interface of the bottom navigation bar

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use Android to achieve the main interface of the bottom navigation bar, the article is very detailed, has a certain reference value, interested friends must read it!

1. The first is the analysis interface. At the bottom of the navigation bar, we can use a linear layout LinearLayout that occupies the full screen width, wraps several tags TextView, and the orientation is horizontal horizontal. At the top is a FrameLayout that takes up the remaining space.

Activity_main.xml

two。 Four tags correspond to four Fragment. Let's create four new Fragment and put a TextView in the layout to distinguish them.

Private HomeFragment homeFragment;private GameFragment gameFragment;private VideoFragment videoFragment;private MineFragment mineFragment

3. Open MainActivity and initialize the control first

Private void initView () {home= findViewById (R.id.main_home); game= findViewById (R.id.main_game); video= findViewById (R.id.main_video); mine= findViewById (R.id.main_mine); layout= findViewById (R.id.main_layout); home.setOnClickListener (this); game.setOnClickListener (this); video.setOnClickListener (this); mine.setOnClickListener (this);}

The onClick click event is dealt with later.

3. Initialize four fragment

Our original intention is to let fragment load once, click repeatedly or switch back will not load again to consume resources. The common processing methods here are lazy loading of viewpager and hide and show of fragment. Here we will explain the implementation of the latter.

Private void initFragment () {FragmentTransaction transaction= getSupportFragmentManager (). BeginTransaction (); if (homeFragmentalization = null&& homeFragment.isAdded ()) {transaction.remove (homeFragment);} if (gameFragmentalization = null&& gameFragment.isAdded ()) {transaction.remove (gameFragment);} if (videoFragmentalization = null&& videoFragment.isAdded ()) {transaction.remove (videoFragment);} if (mineFragmentalization = null&& mineFragment.isAdded ()) {transaction.remove (mineFragment);} transaction.commitAllowingStateLoss (); homeFragment= null; gameFragment= null; videoFragment= null; mineFragment= null Home.performClick ();}

Let's analyze the code line by line.

Start a transaction first.

FragmentTransaction transaction= getSupportFragmentManager () .beginTransaction ()

Perform non-empty processing of Fragment

If (homeFragmentalization = null&& homeFragment.isAdded ()) {transaction.remove (homeFragment);} if (gameFragmentalization = null&& gameFragment.isAdded ()) {transaction.remove (gameFragment);} if (videoFragmentalization = null&& videoFragment.isAdded ()) {transaction.remove (videoFragment);} if (mineFragmentalization = null&& mineFragment.isAdded ()) {transaction.remove (mineFragment);} transaction.commitAllowingStateLoss ()

Set all fragment to null, and automatically execute the click event of homefragment, that is, HomeFragment is displayed by default

HomeFragment= null;gameFragment= null;videoFragment= null;mineFragment= null;home.performClick ()

4. Going back to the click events of the four bottom tabs, we execute the custom switchContent method and pass in the view of the currently clicked tab as a parameter

Overridepublic void onClick (View view) {switch (view.getId ()) {case R.id.main_home: switchContent (home); break; case R.id.main_game: switchContent (game); break; case R.id.main_video: switchContent (video); break; case R.id.main_mine: switchContent (mine); break;}}

5. Navigate to the switchContent method

Create a new fragment object. When you click a tag, if the current fragment object is empty, an object instance corresponding to fragment is generated.

Public void switchContent (View view) {Fragment fragment; if (view== home) {if (homeFragment== null) {homeFragment= new HomeFragment ();} fragment= homeFragment;} else if (view== game) {if (gameFragment== null) {gameFragment= new GameFragment ();} fragment= gameFragment;} else if (view== video) {if (videoFragment== null) {videoFragment= new VideoFragment ();} fragment= videoFragment;} else if (view== mine) {if (mineFragment== null) {mineFragment= new MineFragment ();} fragment= mineFragment;} else {return;}

After the object is generated, we can add and display the fragment.

FragmentTransaction transaction = getSupportFragmentManager (). BeginTransaction (); if (mContent = = null) {transaction.add (layout.getId (), fragment). Commit (); mContent = fragment;} if (mContent! = fragment) {if (! fragment.isAdded ()) {transaction.hide (mContent) .add (layout.getId (), fragment). CommitAllowingStateLoss ();} else {transaction.hide (mContent) .show (fragment). CommitAllowingStateLoss ();} mContent = fragment;} home.setSelected (false); home.setSelected (false); home.setSelected (false) Home.setSelected (false); view.setSelected (true)

To analyze this code, we mainly compare the current fragment mContent with the last fragment fragment to determine whether the bottom navigation bar has been clicked and switched. First, when the application is opened, because we called the automatic click method of the first tab earlier.

Home.performClick ()

Look at the process, because mContent is still null at this time, so go to this code

If (mContent = = null) {transaction.add (layout.getId (), fragment) .commit (); mContent = fragment;}

At this point, fragment is the HomeFragment object, and the page displays HomeFragment and assigns the object to mContent.

At this point, the HomeFragment life cycle is as follows, from Attach () to onResume (), and everything is fine.

Next, click the second tab, fragment is gameFragment,mContent and homeFragment. There is no difference between the two. Go this way.

If (mContent! = fragment) {if (! fragment.isAdded ()) {transaction.hide (mContent) .add (layout.getId (), fragment). CommitAllowingStateLoss ();} else {transaction.hide (mContent) .show (fragment). CommitAllowingStateLoss ();} mContent = fragment;}

Analyze the code. GameFragment goes first because it hasn't been loaded yet.

Transaction.hide (mContent) .add (layout.getId (), fragment) .commitAllowingStateLoss ()

That is, hide the mContent, that is, HomeFragment, and display the GameFragment load.

Look at the life cycle of GameFragment at this time, everything is normal.

Then we click back to HomeFragment. At this time, fragment is HomeFragment,mContent and GameFragment. At the same time, HomeFragment goes because it has been passed by add.

Transaction.hide (mContent) .show (fragment) .commitAllowingStateLoss ()

This statement means to hide fragment without having to load the fragment that add has already loaded, just show directly. CommitAllowingStateLoss method works similar to commit method, which is more suitable for frequently switching pages to avoid crash.

At the same time, open the log and find that HomeFragment has not been destroyed and overloaded, thus achieving the goal that we do not want to switch between frequent loading.

The above is all the contents of the article "how to use Android to achieve the main interface of the bottom navigation bar". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report