android works with viewpager for slideable TAB bar example sharing

  • 2020-05-24 06:07:43
  • OfStack


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 occupied by each label 
 private int currentIndicatorLeft = 0;//  The displacement of the current TAB page 
 private ViewPager mViewPager;// With this view The associated viewpager
 public SyncHorizontalScrollView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }
 public SyncHorizontalScrollView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }
 /**
  * 
  * @param mViewPager With this view The associated viewpager
  * @param leftImage The left arrow 
  * @param rightImage Right arrow 
  * @param tabTitle  Tag array, corresponding to the name of each tag 
  * @param count1 Page number of labels displayed 
  * @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();
 }
 //  Initializes the width of the sliding subscript 
 private void initIndicatorWidth() {
  ViewGroup.LayoutParams cursor_Params = iv_nav_indicator
    .getLayoutParams();
  cursor_Params.width = indicatorWidth;
  iv_nav_indicator.setLayoutParams(cursor_Params);
 }
 //  Add top tag 
 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 displacement animation 
       iv_nav_indicator.startAnimation(animation);
       mViewPager.setCurrentItem(checkedId); // ViewPager
                 //  follow 1 since   switch 
       //  Record the current   The distance to the left of the index   distance 
       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);
      }
     }
    });
 }
 /**
  *  Simulate a click event 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 the left and right arrows 
 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);
    }
   }
  }
 }
}


<?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>


<?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" />


Related articles: