Android with Viewpager to implement a sliding tab bar sample sharing _android

Source: Internet
Author: User

Copy Code code as follows:

Package Com.example.playtabtest.view;

Import COM.EXAMPLE.PLAYTABTEST.R;

Import android.app.Activity;
Import Android.content.Context;
Import Android.support.v4.view.ViewPager;
Import Android.support.v4.view.ViewPager.OnPageChangeListener;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.animation.LinearInterpolator;
Import android.view.animation.TranslateAnimation;
Import Android.widget.HorizontalScrollView;
Import Android.widget.ImageView;
Import Android.widget.RadioButton;
Import Android.widget.RadioGroup;
Import Android.widget.RadioGroup.OnCheckedChangeListener;

public class Synchorizontalscrollview extends Horizontalscrollview {

Private view view;
Private ImageView Leftimage;
Private ImageView Rightimage;
private int WINDOWWITDH = 0;
Private activity Mcontext;
Private Radiogroup rg_nav_content;
Private ImageView Iv_nav_indicator;
Private Layoutinflater Minflater;
private int indicatorwidth;//The width of each label
private int currentindicatorleft = 0;//The offset of the current label page
Private Viewpager mviewpager;//associated with this view Viewpager

Public Synchorizontalscrollview {
Super (context);
TODO auto-generated Constructor stub
}

Public Synchorizontalscrollview (context context, AttributeSet Attrs) {
Super (context, attrs);
TODO auto-generated Constructor stub
}

/**
*
* @param mviewpager associated with this view Viewpager
* @param Leftimage LEFT Arrow
* @param rightimage RIGHT Arrow
* @param tabtitle Tag Array, corresponding to the name of each label
* Number of labels displayed on @param count page
* @param context
*/
public void Setsomeparam (Viewpager mviewpager, ImageView leftimage,
ImageView rightimage, string[] tabtitle, int count, activity context) {
This.mcontext = context;
This.mviewpager = Mviewpager;
Minflater = Layoutinflater.from (context);
This.view = minflater.inflate (R.layout.sync_hsv_item, NULL);
This.addview (view);
This.leftimage = Leftimage;
This.rightimage = Rightimage;
Displaymetrics dm = new Displaymetrics ();
Context.getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);
WINDOWWITDH = Dm.widthpixels;
Indicatorwidth = Windowwitdh/count;
Init (tabtitle);
This.invalidate ();
}


private void Init (string[] tabtitle) {
Rg_nav_content = (radiogroup) View.findviewbyid (r.id.rg_nav_content);
Iv_nav_indicator = (ImageView) View.findviewbyid (r.id.iv_nav_indicator);
Initindicatorwidth ();
INITNAVIGATIONHSV (Tabtitle);
Setlistener ();
}

Initial sliding under the object width
private void Initindicatorwidth () {
Viewgroup.layoutparams Cursor_params = Iv_nav_indicator
. Getlayoutparams ();
Cursor_params.width = Indicatorwidth;
Iv_nav_indicator.setlayoutparams (Cursor_params);
}

Add Top Label
private void Initnavigationhsv (string[] tabtitle) {
Rg_nav_content.removeallviews ();
for (int i = 0; i < tabtitle.length; i++) {
RadioButton RB = (RadioButton) minflater.inflate (
R.layout.nav_radiogroup_item, NULL);
Rb.setid (i);
Rb.settext (Tabtitle[i]);
Rb.setlayoutparams (New Layoutparams (Indicatorwidth,
Layoutparams.match_parent));
Rg_nav_content.addview (RB);
}

}

private void Setlistener () {
Rg_nav_content
. Setoncheckedchangelistener (New Oncheckedchangelistener () {
@Override
public void OnCheckedChanged (radiogroup group, int checkedid) {
if (Rg_nav_content.getchildat (checkedid)!= null) {
translateanimation animation = new Translateanimation (
Currentindicatorleft,
((RadioButton) rg_nav_content
. Getchildat (Checkedid)). GetLeft (),
0f, 0f);
Animation.setinterpolator (New Linearinterpolator ());
Animation.setduration (100);
Animation.setfillafter (TRUE);

Perform a displacement animation
Iv_nav_indicator.startanimation (animation);
Mviewpager.setcurrentitem (Checkedid); Viewpager
Follow together to toggle
Record the leftmost distance from the current subscript
Currentindicatorleft = ((RadioButton) rg_nav_content
. Getchildat (Checkedid)). GetLeft ();
Smoothscrollto (
(Checkedid > 1?) ((RadioButton) rg_nav_content
. Getchildat (Checkedid)). GetLeft ()
: 0)
-((RadioButton) rg_nav_content
. Getchildat (2)). GetLeft (),
0);
}
}
});
}

/**
* Analog Click events for external calls
* @param position
*/
public void Performlabelclick (int position) {
if (rg_nav_content!= null && rg_nav_content.getchildcount () > position) {
((RadioButton) Rg_nav_content.getchildat (position)). PerformClick ();
}
}

Show and hide arrows on both sides
protected void onscrollchanged (int l, int t, int oldl, int Oldt) {
Super.onscrollchanged (L, T, OLDL, Oldt);
if (!mcontext.isfinishing () && view!= null && rightimage!= NULL
&& leftimage!= null) {
if (View.getwidth () <= WINDOWWITDH) {
Leftimage.setvisibility (View.gone);
Rightimage.setvisibility (View.gone);
} else {
if (L = = 0) {
Leftimage.setvisibility (View.gone);
Rightimage.setvisibility (view.visible);
else if (View.getwidth ()-L = = WINDOWWITDH) {
Leftimage.setvisibility (view.visible);
Rightimage.setvisibility (View.gone);
} else {
Leftimage.setvisibility (view.visible);
Rightimage.setvisibility (view.visible);
}
}
}
}
}

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<relativelayout
Android:id= "@+id/rl_nav"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:layout_gravity= "Top"
android:background= "#5AB0EB" >

<radiogroup
Android:id= "@+id/rg_nav_content"
Android:layout_width= "Fill_parent"
android:layout_height= "38dip"
Android:layout_alignparenttop= "true"
Android:background= "#F2F2F2"
android:orientation= "Horizontal" >
</RadioGroup>

<imageview
Android:id= "@+id/iv_nav_indicator"
Android:layout_width= "1dip"
android:layout_height= "5dip"
Android:layout_alignparentbottom= "true"
Android:background= "#5AB0EB"
android:contentdescription= "@string/nav_desc"
android:scaletype= "Matrix"/>
</RelativeLayout>

</LinearLayout>

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<radiobutton xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "0dip"
android:layout_height= "Fill_parent"
Android:background= "#F2F2F2"
android:button= "@null"
Android:checked= "true"
android:gravity= "Center"
Android:text= ""
Android:textcolor= "@drawable/RB_BLUE_BG"
Android:textsize= "14.0dip"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.