You have customized a framework for TabLayout... In the future, you can write the bottom menu directly as the dependent library, so you don't have to worry about it. tablayout framework
First, let's briefly describe the implementation steps of the framework at the bottom.
1. Define a class and inherit LinearLayout. Because I like LinearLayout, you can also inherit RelativeLayout.
2. Rewrite the two construction methods with parameters.
3. Enter content writing, layout File Processing, and data processing.
4. Add a view to the container at the bottom. A page switch is implemented in the menu at the bottom.
Well, because of my individual table appeal capability. Code directly
Package com. weight. tabbottomlib. view;
Import java. util. ArrayList;
Import android. content. Context;
Import android. graphics. Color;
Import android. util. AttributeSet;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. widget. LinearLayout;
Import android. widget. TextView;
/**
* Bottom switch framework
*
* @ Author Administrator
*
*/
Public class MyBottomLayout extends LinearLayout {
Static ArrayList <Item> itemData = new ArrayList <Item> ();
Static ArrayList <View> viewArryItem = new ArrayList <View> ();
LayoutInflater inflater; // layout manager
View mainView; // This is the container at the bottom
View viewItem;
Int itemSize = 0;
Public MyBottomLayout (Context context, AttributeSet attrs ){
Super (context, attrs );
}
/**
* Add a view to the container at the bottom
*
* @ Param view
*/
Public void addBottomLayoutValue (ArrayList <Item> itemData ){
LayoutInflater inflater = LayoutInflater. from (getContext ());
This. itemData = itemData;
This. itemSize = itemData. size ();
For (int I = 0; I <itemData. size (); I ++ ){
View viewItem = inflater. inflate (R. layout. layout_tab_item, null );
ViewItem. findViewById (R. id. tabImg). setBackgroundResource (
ItemData. get (I). getDrawableNormalId ());
TextView textView = (TextView) (viewItem
. FindViewById (R. id. tabText )));
TextView. setText (itemData. get (I). getName ());
TextView. setTextColor (Color. WHITE );
This. addView (viewItem, new LinearLayout. LayoutParams (
LayoutParams. FILL_PARENT, LayoutParams. FILL_PARENT, 1.0f ));
ViewItem. setId (I );
ViewArryItem. add (viewItem );
ViewItem. setOnClickListener (new lister ());
}
// Process the sorted data
ChangeDataState ();
}
/**
* Change the data status.
*/
Private void changeDataState (){
If (itemSize! = 5) {// No need to handle
For (int I = itemSize; I <5; I ++ ){
ItemData. add (new Item ());
LayoutInflater inflater = LayoutInflater. from (getContext ());
View viewItem = inflater
. Inflate (R. layout. layout_tab_item, null );
ViewArryItem. add (viewItem );
}
}
}
Public interface ICallbackLister {
Public void click (int id );
}
ICallbackLister callbackLister = null;
Public void setOnCallbackLister (ICallbackLister callbackLister ){
This. callbackLister = callbackLister;
}
/**
*
* @ ClassName: lister
* @ Description: handling of tab click events
* @ Author lumin
* @ Date 12:53:36
*
*/
Private class lister implements OnClickListener {
@ Override
Public void onClick (View clickView ){
CallbackLister. click (clickView. getId ());
InitBottom (clickView. getId ());
}
}
Public static void initBottom (int id ){
Switch (id ){
Case 0:
// Step 2: implement a page Switch
InitData (itemData. get (0). getDrawableSelectId (), itemData. get (1)
. GetDrawableNormalId (), itemData. get (2)
. GetDrawableNormalId (), itemData. get (3)
. GetDrawableNormalId (), itemData. get (4)
. GetDrawableNormalId ());
ChangeTextColor (
(TextView) (viewArryItem. get (0). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (1). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (2). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (3). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (4). findViewById (R. id. tabText )));
Break;
Case 1:
InitData (itemData. get (0). getDrawableNormalId (), itemData. get (1)
. GetDrawableSelectId (), itemData. get (2)
. GetDrawableNormalId (), itemData. get (3)
. GetDrawableNormalId (), itemData. get (4)
. GetDrawableNormalId ());
ChangeTextColor (
(TextView) (viewArryItem. get (1). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (0). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (2). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (3). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (4). findViewById (R. id. tabText )));
Break;
Case 2:
InitData (itemData. get (0). getDrawableNormalId (), itemData. get (1)
. GetDrawableNormalId (), itemData. get (2)
. GetDrawableSelectId (), itemData. get (3)
. GetDrawableNormalId (), itemData. get (4)
. GetDrawableNormalId ());
ChangeTextColor (
(TextView) (viewArryItem. get (2). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (0). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (1). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (3). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (4). findViewById (R. id. tabText )));
Break;
Case 3:
InitData (itemData. get (0). getDrawableNormalId (), itemData. get (1)
. GetDrawableNormalId (), itemData. get (2)
. GetDrawableNormalId (), itemData. get (3)
. GetDrawableSelectId (), itemData. get (4)
. GetDrawableNormalId ());
ChangeTextColor (
(TextView) (viewArryItem. get (3). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (0). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (1). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (2). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (4). findViewById (R. id. tabText )));
Break;
Case 4:
InitData (itemData. get (0). getDrawableNormalId (), itemData. get (1)
. GetDrawableNormalId (), itemData. get (2)
. GetDrawableNormalId (), itemData. get (3)
. GetDrawableNormalId (), itemData. get (4)
. GetDrawableSelectId ());
ChangeTextColor (
(TextView) (viewArryItem. get (4). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (0). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (1). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (2). findViewById (R. id. tabText )),
(TextView) (viewArryItem. get (3). findViewById (R. id. tabText )));
Break;
}
}
/**
*
* @ Param resIdOne
* @ Param resIdTwo
* @ Param resIdThree
* @ Param resIdFour
* @ Param resIdFive
*/
Public static void initData (int resIdOne, int resIdTwo, int resIdThree,
Int resIdFour, int resIdFive ){
Try {
ViewArryItem. get (0). findViewById (R. id. tabImg)
. SetBackgroundResource (resIdOne );
ViewArryItem. get (1). findViewById (R. id. tabImg)
. SetBackgroundResource (resIdTwo );
ViewArryItem. get (2). findViewById (R. id. tabImg)
. SetBackgroundResource (resIdThree );
ViewArryItem. get (3). findViewById (R. id. tabImg)
. SetBackgroundResource (resIdFour );
ViewArryItem. get (4). findViewById (R. id. tabImg)
. SetBackgroundResource (resIdFive );
} Catch (Exception err ){
Err. printStackTrace ();
// When an error is reported, it means there are not so many items
}
}
/**
*
* @ Param txtOne
* This is the default color of the image to be changed.
* @ Param txtTwo
* @ Param txtThree
* @ Param txtFour
* @ Param txtFive
*/
Public static void changeTextColor (TextView txtOne, TextView txtTwo,
TextView txtThree, TextView txtFour, TextView txtFive ){
TxtOne. setTextColor (Color. MAGENTA );
TxtTwo. setTextColor (Color. BLUE );
TxtThree. setTextColor (Color. BLUE );
TxtFour. setTextColor (Color. BLUE );
TxtFive. setTextColor (Color. BLUE );
}
}
Well, This completes the compilation of the Framework. What is missing...
The item of the current page is given below .. And entity ..
First, layout files
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<RelativeLayout
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true">
<ImageView
Android: id = "@ + id/tabImg"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerHorizontal = "true"
/>
<TextView
Android: id = "@ + id/tabText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_below = "@ id/tabImg"
Android: layout_centerHorizontal = "true"
Android: textColor = "# ffffff"
Android: textSize = "16dp"/>
</RelativeLayout>
</RelativeLayout>
Then .. Object
Package com. weight. tabbottomlib. view;
Public class Item {
Private int drawableNormalId;
Private int drawableSelectId;
Private String name;
Public Item (int drawableNormalId, int drawableSelectId, String name ){
Super ();
This. drawableNormalId = drawableNormalId;
This. drawableSelectId = drawableSelectId;
This. name = name;
}
Public Item (){
Super ();
}
Public int getDrawableNormalId (){
Return drawableNormalId;
}
Public void setDrawableNormalId (int drawableNormalId ){
This. drawableNormalId = drawableNormalId;
}
Public int getDrawableSelectId (){
Return drawableSelectId;
}
Public void setDrawableSelectId (int drawableSelectId ){
This. drawableSelectId = drawableSelectId;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
}
In this way, all are implemented. If you use it as a library, right-click the Config Build Path under Build Path in your project, go to Android, select isLibrary, and click OK.
Then, I will tell you the specific usage in the following article.