[AS2.3.3] Implementation in the bottom bar: Using FragmentTabHost + Fragment,
The bottom switch bar is implemented using FragmentTabHost + Fragment. I wrote an article long ago. This article is integrated and used.
First, let's look at the effect.
Use the following
List
List; BarTab tab; Bundle bundle; list = new ArrayList <> (); tab = new BarTab (); tab. set ("Homepage"); tab. setCls (TestFragment. class); tab. setImageNormal (R. mipmap. home1); tab. setImageSelect (R. mipmap. home2); bundle = new Bundle (); bundle. putString ("", "Homepage"); tab. setBundle (bundle); list. add (tab); tab = new BarTab (); tab. set ("found"); tab. setCls (TestFragment. class); tab. setImageNormal (R. mipmap. glod1); tab. setImageSelect (R. mipmap. glod2); bundle = new Bundle (); bundle. putString ("", "found"); tab. setBundle (bundle); list. add (tab); tab = new BarTab (); tab. set ("user"); tab. setCls (TestFragment. class); tab. setImageNormal (R. mipmap. user1); tab. setImageSelect (R. mipmap. user2); bundle = new Bundle (); bundle. putString ("", "user"); tab. setBundle (bundle); list. add (tab); BaseBottomBar
BottomBar = new BaseBottomBar
(MActivity, tabHost, R. id. fl_test, R. layout. item_bar, list) {@ Override protected TextView getBarText (View view) {return (TextView) view. findViewById (R. id. TV _bar) ;}@ Override protected ImageView getBarImage (View view) {return (ImageView) view. findViewById (R. id. iv_bar) ;}@ Override protected int setSelectColor () {return ContextCompat. getColor (mContext, R. color. appMainColor) ;}}; bottomBar. setImageLoader (new ImageLoader () {@ Override public void ImageLoader (Context context, Object o, ImageView view) {Glide. with (context ). load (o ). into (view );}}). create ();
Create a BaseBottomBar object and set its layout id, Bottom Bar layout, and data list!
The two methods that must be implemented are getBarText and getBarImage, which are used to set the image and text changes after clicking
You can use the create () method to create a project.
The Code has only three classes.
First, let's take a look at the basic usage of FragmentTabHost.
Google official example Activity used
import com.example.android.supportv4.R;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentTabHost;/** * This demonstrates how you can implement switching between the tabs of a * TabHost through fragments, using FragmentTabHost. */public class FragmentTabs extends FragmentActivity { private FragmentTabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_tabs); mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), FragmentStackSupport.CountingFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), LoaderCursorSupport.CursorLoaderListFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"), LoaderCustomSupport.AppListFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), LoaderThrottleSupport.ThrottledLoaderListFragment.class, null); }}
Used in Fragment
import com.example.android.supportv4.R;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentTabHost;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentTabsFragmentSupport extends Fragment { private FragmentTabHost mTabHost; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mTabHost = new FragmentTabHost(getActivity()); mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1); mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"), FragmentStackSupport.CountingFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"), LoaderCursorSupport.CursorLoaderListFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"), LoaderCustomSupport.AppListFragment.class, null); mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"), LoaderThrottleSupport.ThrottledLoaderListFragment.class, null); return mTabHost; } @Override public void onDestroyView() { super.onDestroyView(); mTabHost = null; }}
The above is from the online search
After the basic usage, you can start to write a management method for simple use!
First, set the main layout.
Activity_test.xml
<framelayout android:id="@+id/fl_test" android:layout_height="0dp" android:layout_weight="1" android:layout_width="match_parent"> </framelayout>
Then we set a menu at the bottom of each item.
I have set a regular image and a title.
Item_bar.xml
Then we need to define the attributes of the Item.
Here I have defined an attribute class.
BarTab. java
Import android. OS. bundle; import android. view. view; import android. widget. imageView; import android. widget. textView;/*** BarTab * Author: gjn. * Time: 2018/1/4. */public class BarTab {// Title private String; // bound Fragment private Class
Cls; // View private View view at the bottom; // Bundle private Bundle bundle; // private Object imageNormal under normal conditions of the icon; // private Object imageSelect when the icon is selected; // two la s in the bottom menu: private ItemView itemView; public static class ItemView {private ImageView image; private TextView text; public ImageView getImage () {return image ;} public void setImage (ImageView image) {this. image = image;} public TextView getText () {return text;} public void setText (TextView text) {this. text = text ;}} public String get () {return;} public void set (String) {this. =;} public Class
GetCls () {return cls;} public void setCls (Class
Cls) {this. cls = cls;} public Bundle getBundle () {return bundle;} public void setBundle (Bundle bundle) {this. bundle = bundle;} public View getView () {return view;} public void setView (View view) {this. view = view;} public Object getImageNormal () {return imageNormal;} public void setImageNormal (Object imageNormal) {this. imageNormal = imageNormal;} public Object getImageSelect () {return imageSelect;} public void setImageSelect (Object imageSelect) {this. imageSelect = imageSelect;} public ItemView getItemView () {return itemView;} public void setItemView (ItemView itemView) {this. itemView = itemView ;}}
We set attributes for each bottom option.
Finally, we write a management tool.
BaseBottomBar. java
Import android. app. activity; import android. graphics. color; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentTabHost; import android. view. layoutInflater; import android. view. view; import android. widget. imageView; import android. widget. tabHost; import android. widget. tabWidget; import android. widget. textView; import com. gjn. utilslibrary. utils. imageLoader; import java. util. arrayList; import java. util. list;/*** BaseBottomBar * Author: gjn. * Time: 2018/1/4. */public abstract class BaseBottomBar
Implements TabHost. OnTabChangeListener {private Activity mActivity; private FragmentTabHost mTabHost; private int mContainerId; private int mViewId; private List
MItems; private int mNormalColor =-1; private int mSelectColor =-1; private BottomBarListener bottomBarListener; private ImageLoader mImageLoader; public activities (Activity activity, FragmentTabHost tabHost, int containerId, int viewid, List
Items) {mActivity = activity; mTabHost = tabHost; mContainerId = containerId; mViewId = viewid; mItems = items = null? New ArrayList <> (): items; mNormalColor = setNormalColor (); mSelectColor = setSelectColor ();} protected int setSelectColor () {return Color. BLACK;} protected int setNormalColor () {return Color. BLACK;} private void reset (T item) {mImageLoader. imageLoader (mActivity, item. getImageNormal (), getImage (item); getText (item ). setText (item. get (); getText (item ). setTextColor (mNormalColor);} private void select (T item) {mImageLoader. imageLoader (mActivity, item. getImageSelect (), getImage (item); getText (item ). setText (item. get (); getText (item ). setTextColor (mSelectColor);} private ImageView getImage (T item) {return item. getItemView (). getImage ();} private TextView getText (T item) {return item. getItemView (). getText ();} public BaseBottomBar setBottomBarListener (BottomBarListener bottomBarListener) {this. bottomBarListener = bottomBarListener; return this;} public void setCurrentTab (int I) {mTabHost. setCurrentTab (I);} public List
GetAllItem () {return mItems;} public View getView (int I) {T item = (T) mItems. get (I); return item. getView ();} public BarTab. itemView getItemView (int I) {T item = (T) mItems. get (I); return item. getItemView ();} public BaseBottomBar setImageLoader (ImageLoader imageLoader) {mImageLoader = imageLoader; return this;} public void setItems (List
Items) {mItems = items; mTabHost. removeAllViews (); create ();} public void create () {if (mTabHost = null) {throw new NullPointerException ("TabHost is null! ");} If (mImageLoader = null) {throw new NullPointerException (" ImageLoader is null. ");} mTabHost. setup (mActivity, (FragmentActivity) mActivity ). getsuppfrfragmentmanager (), mContainerId); mTabHost. setOnTabChangedListener (this); mTabHost. getTabWidget (). setDividerDrawable (null); for (int I = 0; I <mItems. size (); I ++) {T item = (T) mItems. get (I); // set the view and internal layout of each Item. View = LayoutInflater. From (mActivity ). inflate (mViewId, null, false); BarTab. itemView itemView = new BarTab. itemView (); itemView. setImage (getBarImage (view); itemView. setText (getBarText (view); item. setView (view); item. setItemView (itemView); // The selected image is not set. The default value is normal if (item. getImageSelect () = null) {item. setImageSelect (item. getImageNormal ();} // set the default image if (I = 0) {select (item);} else {reset (item);} TabHost. tabSpec tabSp Ec = mTabHost. newTabSpec (item. get ()). setIndicator (view); mTabHost. addTab (tabSpec, item. getCls (), item. getBundle () ;}@override public void onTabChanged (String tabId) {TabWidget widget = mTabHost. getTabWidget (); for (int I = 0; I <widget. getChildCount (); I ++) {T item = (T) mItems. get (I); if (I = mTabHost. getCurrentTab () {select (item); if (bottomBarListener! = Null) {bottomBarListener. onClick (I, tabId) ;}} else {reset (item) ;}} public interface BottomBarListener {void onClick (int I, String );} protected abstract TextView getBarText (View view); protected abstract ImageView getBarImage (View view );}
The ImageLoader in is just a simple interface used to set Images Using Glide
ImageLoader. java
import android.content.Context;import android.widget.ImageView;/** * ImageLoader * Author: gjn. * Time: 2018/1/2. */public interface ImageLoader { void ImageLoader(Context context, Object o, ImageView view);}
Summary
Write down the previous implementation in the bottom bar. It may be complicated to integrate the methods, but it is something I can achieve at present.