Android bottom navigation summary, android navigation Summary
There are several ways to implement the bottom navigation of android, such as fragment:, TabActivity, ViewGroup, and viewPager,
This section describes how to use viewPager to implement bottom navigation.
Let's talk about the reasons for using viewPager.
1. fragment
A. If we need to use fragment, we can only use fragment in v4, because we need backward compatibility.
B. When we use the fragment in the V4 package, the system automatically adds a node.
C fragment does not allow nested fragmentd, which Google solved in 4.2. If my SDK version is not updated, this method cannot be found.
The getChildFragmentManager () method cannot be found in Fragment of android. support. v4.app.
2. viewPager:
A is easy to use.
3. TabActivity:
A TabActivity is out of date. Use fragment directly.
B Activity is the four main components in android. It is a heavyweight component. fragment is a clip in android and a special view. It has a lifecycle.
4. ViewGroup:
A: It is a low-level component. It is troublesome.
First, customize the lazy viewpager. The purpose is: you do not need to load the next page by default.
View the source code of ViewPager and find that the DEFAULT_OFFSCREEN_PAGES variable controls the number of pages to be loaded.
Private static final int DEFAULT_OFFSCREEN_PAGES = 1; // The page size is loaded by default.
Private int mOffscreenPageLimit = DEFAULT_OFFSCREEN_PAGES;
Final int pageLimit = mOffscreenPageLimit;
Final int startPos = Math. max (0, mCurItem-pageLimit );
Final int N = mAdapter. getCount ();
Final int endPos = Math. min (N-1, mCurItem + pageLimit );
When DEFAULT_OFFSCREEN_PAGES = 1, select the first pager, that is, when mCurItem = 0, assume that there are 3 pages in total N = 3.
StartPos = Math. max (0, mCurItem-pageLimit); startPos = 0
EndPos = Math. min (N-1, mCurItem + pageLimit); endPos = 1
When DEFAULT_OFFSCREEN_PAGES = 1 and select the second pager, that is, mCurItem = 1, suppose there are 3 pages N = 3.
StartPos = Math. max (0, mCurItem-pageLimit); startPos = 1
EndPos = Math. min (N-1, mCurItem + pageLimit); endPos = 2
When DEFAULT_OFFSCREEN_PAGES = 1, select the third pager, that is, when mCurItem = 2, suppose there are 3 pages in total N = 3.
StartPos = Math. max (0, mCurItem-pageLimit); startPos = 2
EndPos = Math. min (N-1, mCurItem + pageLimit); endPos = 2
Therefore, to load the next page by default, you only need to change the value to 0.
Private static final int DEFAULT_OFFSCREEN_PAGES = 0; // The default page is loaded. ViewPager is 1, so two Fragment
When DEFAULT_OFFSCREEN_PAGES = 0 and the first pager is selected, that is, when mCurItem = 0, suppose there are 3 pages in total N = 3.
StartPos = Math. max (0, mCurItem-pageLimit); startPos = 0
EndPos = Math. min (N-1, mCurItem + pageLimit); endPos = 0
When DEFAULT_OFFSCREEN_PAGES = 0 and the second pager is selected, that is, when mCurItem = 1, suppose there are 3 pages in total N = 3.
StartPos = Math. max (0, mCurItem-pageLimit); startPos = 1
EndPos = Math. min (N-1, mCurItem + pageLimit); endPos = 1
When DEFAULT_OFFSCREEN_PAGES = 0 and the third pager is selected, that is, when mCurItem = 2, suppose there are 3 pages in total N = 3.
StartPos = Math. max (0, mCurItem-pageLimit); startPos = 2
EndPos = Math. min (N-1, mCurItem + pageLimit); endPos = 2
The ViewPager pre-loading problem is solved.
You can override the two methods to disable ViewPager sliding. To achieve this, you can slide the variable setTouchMode to true.
<span style="white-space:pre"></span>private boolean setTouchMode = false;<span style="white-space:pre"></span>@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {if (setTouchMode)return super.onInterceptTouchEvent(ev);elsereturn false;}<span style="white-space:pre"></span>@Overridepublic boolean onTouchEvent(MotionEvent ev) {if(setTouchMode) return super.onTouchEvent(ev);elsereturn false;}
The layout code activity_main.xml on the home page is as follows:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <view android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1.0" class="com.yuweiguo.bottomnavibyviewpager.view.MyViewPager" /> <RadioGroup android:id="@+id/main_radio" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_gravity="bottom" android:background="#133575" android:gravity="center_vertical" android:orientation="horizontal" android:paddingTop="2dp" > <RadioButton android:id="@+id/rb_navi" style="@style/main_tab_bottom" android:drawableTop="@drawable/tab_selector1" /> <RadioButton android:id="@+id/rb_routeplan" style="@style/main_tab_bottom" android:drawableTop="@drawable/tab_selector2" /> <RadioButton android:id="@+id/rb_location" style="@style/main_tab_bottom" android:drawableTop="@drawable/tab_selector3" /> <RadioButton android:id="@+id/rb_setting" style="@style/main_tab_bottom" android:drawableTop="@drawable/tab_selector4" /> </RadioGroup></LinearLayout>
MainActivity. java source code
package com.yuweiguo.bottomnavibyviewpager;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.support.v4.view.PagerAdapter;import android.view.KeyEvent;import android.view.View;import android.view.ViewGroup;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.Toast;import com.yuweiguo.bottomnavibyviewpager.base.BasePager;import com.yuweiguo.bottomnavibyviewpager.pager.MessagePager;import com.yuweiguo.bottomnavibyviewpager.pager.NaviPager;import com.yuweiguo.bottomnavibyviewpager.pager.RouteplanPager;import com.yuweiguo.bottomnavibyviewpager.pager.SearchPager;import com.yuweiguo.bottomnavibyviewpager.view.LazyViewPager.OnPageChangeListener;import com.yuweiguo.bottomnavibyviewpager.view.MyViewPager;public class MainActivity extends Activity {private MyViewPager viewpager;private RadioGroup main_radio;private List<BasePager> pages = new ArrayList<BasePager>();private ViewPageAdapter viewPageAdapter;private int currentItem = R.id.rb_navi;private int oldPosition = 2;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);viewpager = (MyViewPager) findViewById(R.id.viewpager);main_radio = (RadioGroup) findViewById(R.id.main_radio);pages.clear();pages.add(new NaviPager(this));pages.add(new RouteplanPager(this));pages.add(new SearchPager(this));pages.add(new MessagePager(this));viewPageAdapter = new ViewPageAdapter(pages);viewpager.setAdapter(viewPageAdapter);viewpager.setOnPageChangeListener(new OnPageChangeListener() {@Overridepublic void onPageSelected(int position) {BasePager pager = pages.get(position);pager.onResume();pager.initData();}@Overridepublic void onPageScrolled(int position, float positionOffset,int positionOffsetPixels) {}@Overridepublic void onPageScrollStateChanged(int state) {}});main_radio.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.rb_navi:if (oldPosition != 0) {pages.get(oldPosition).onPause();oldPosition = 0;}viewpager.setCurrentItem(0, false);break;case R.id.rb_routeplan:if (oldPosition != 1) {pages.get(oldPosition).onPause();oldPosition = 1;}viewpager.setCurrentItem(1, false);break;case R.id.rb_location:if (oldPosition != 2) {pages.get(oldPosition).onPause();oldPosition = 2;}viewpager.setCurrentItem(2, false);break;case R.id.rb_setting:if (oldPosition != 3) {pages.get(oldPosition).onPause();oldPosition = 3;}viewpager.setCurrentItem(3, false);break;}currentItem = checkedId;}});main_radio.check(currentItem);}@Overrideprotected void onResume() {super.onResume();pages.get(oldPosition).onResume();}@Overrideprotected void onPause() {super.onPause();pages.get(oldPosition).onPause();}public class ViewPageAdapter extends PagerAdapter {private List<BasePager> list;public ViewPageAdapter(List<BasePager> pages) {this.list = pages;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {((MyViewPager) container).removeView(list.get(position).getRootView());}@Overridepublic Object instantiateItem(ViewGroup container, int position) {((MyViewPager) container).addView(list.get(position).getRootView(),0);return list.get(position).getRootView();}@Overridepublic int getCount() {return list.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}}@Overrideprotected void onDestroy() {if (pages != null) {for (BasePager pager : pages) {pager.onDestroy();}}super.onDestroy();}}
Last
Finally presented free source http://download.csdn.net/detail/gfbgl/8196027