content: package anna.howtocelebratewintersolstice;
  
  /**
   * Copyright (C) 2015 nshmura
   * Copyright (C) 2015 The Android Open Source Project
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
          import android.content.Context;
          import android.support.v4.view.ViewPager;
          import android.support.v7.widget.LinearLayoutManager;
          import android.support.v7.widget.RecyclerView;
          import android.util.AttributeSet;
  
  public class RecyclerTabLayout extends RecyclerView {
  
      //protected static final float POSITION_THRESHOLD_ALLOWABLE = 0.001f;
  
      protected LinearLayoutManager mLinearLayoutManager;
      protected ViewPager mViewPager;
      protected Adapter<?> mAdapter;
  
      protected float mOldPositionOffset;
  
      public RecyclerTabLayout(Context context) {
          this(context, null);
      }
  
      public RecyclerTabLayout(Context context, AttributeSet attrs) {
          this(context, attrs, 0);
      }
  
      public RecyclerTabLayout(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          setWillNotDraw(false);
  
          mLinearLayoutManager = new LinearLayoutManager(getContext());
          mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
          setLayoutManager(mLinearLayoutManager);
      }
  
      public void setUpWithAdapter(RecyclerTabLayout.Adapter<?> adapter) {
          mAdapter = adapter;
          mViewPager = adapter.getViewPager();
          if (mViewPager.getAdapter() == null) {
              throw new IllegalArgumentException("ViewPager does not have a PagerAdapter set");
          }
          mViewPager.addOnPageChangeListener(new ViewPagerOnPageChangeListener(this));
          setAdapter(adapter);
      }
  
  
      protected void scrollToTab(int position, float positionOffset, boolean fitIndicator) {
          int scrollOffset = 0;
  
          mLinearLayoutManager.scrollToPositionWithOffset(position, scrollOffset);
          mOldPositionOffset = positionOffset;
      }
  
  
      protected static class ViewPagerOnPageChangeListener implements ViewPager.OnPageChangeListener {
  
          private final RecyclerTabLayout mRecyclerTabLayout;
  
          public ViewPagerOnPageChangeListener(RecyclerTabLayout recyclerTabLayout) {
              mRecyclerTabLayout = recyclerTabLayout;
          }
  
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
              mRecyclerTabLayout.scrollToTab(position, positionOffset, false);
          }
  
  
          @Override
          public void onPageSelected(int position) {
              // empty class
          }
  
          @Override
          public void onPageScrollStateChanged(int state) {
              //empty class
          }
      }
  
      public static abstract class Adapter<T extends RecyclerView.ViewHolder>
              extends RecyclerView.Adapter<T> {
  
          protected ViewPager mViewPager;
  
          public Adapter(ViewPager viewPager) {
              mViewPager = viewPager;
          }
  
          public ViewPager getViewPager() {
              return mViewPager;
          }
      }
  
  }