CenteredTabLayout的实现

时间:2016-07-06 21:07:54

标签: android android-viewpager android-support-library android-tablayout

我需要像TabLayout这样的东西滚动并选择中央选项卡并附加相应的ViewPager性能。 我试过通过编写下一个代码来解决这个问题,但这看起来很奇怪。我确定还有其他方法可以解决这个问题。

public class CenteredTabLayout extends TabLayout {


   public ViewPager mViewPager;
    private int currentWidthOffset = 0;
    private int index = 0;
    private int totalElementsNumber;
    private ArrayList<Integer> elementsWidth = new ArrayList<>();
    private volatile int state = 2;

    public CenteredTabLayout(Context context) {
        super(context);
    }

    public CenteredTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CenteredTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addTab(@NonNull Tab tab) {
        getElementsWidth();
        super.addTab(tab);
    }

    @Override
    public void removeTab(Tab tab) {
        getElementsWidth();
        super.removeTab(tab);

    }

    private void getElementsWidth() {
        elementsWidth.clear();
        totalElementsNumber = ((ViewGroup)getChildAt(0)).getChildCount();
        //Log.i(ScrollableTabsActivityCustom1.TAG, "totalElementsNumber = " + totalElementsNumber);
        for(int i = 0; i < totalElementsNumber; i++) {
            elementsWidth.add(((ViewGroup)getChildAt(0)).getChildAt(i).getWidth());
            //Log.i(ScrollableTabsActivityCustom1.TAG, "elementsWidth.get(i) = " + elementsWidth.get(i));
        }
    }

    CountDownTimer mCountDownTimer = new CountDownTimer(500, 500) {
        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            Log.i(ScrollableTabsActivityCustom1.TAG, "finished");
            View tab = ((ViewGroup)getChildAt(0)).getChildAt(index);
            scrollTo(currentWidthOffset , 0); 
        }
    };

    private void startTimer(final int currentState) {
        if(currentState != state) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    state = currentState;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException pE) {
                        pE.printStackTrace();
                    }
                    mViewPager.setCurrentItem(index);
                    state = 2;
                }
            }).start();
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        getElementsWidth();
        int difference = l - oldl;
        int currentWidthOffsetSave = currentWidthOffset;

        if(state != 1 && difference > 0 && l > (currentWidthOffset + elementsWidth.get(checkIndex(index)) / 2)) {
            do {
                currentWidthOffset += elementsWidth.get(checkIndex(index));
                index++;
                index = checkIndex(index);
                mViewPager.setCurrentItem(index);
                View tab = ((ViewGroup)getChildAt(0)).getChildAt(index);
                scrollTo(currentWidthOffset, 0);
            } while(currentWidthOffsetSave + difference > currentWidthOffset + elementsWidth.get(checkIndex(index)) / 2);

            if(difference > 10) {
                startTimer(0);
            }
        }

        if(state != 0 && difference < 0 && l < (currentWidthOffset - elementsWidth.get(checkIndex(index - 1)) / 2)) {

            do {
                index--;
                index = checkIndex(index);
                currentWidthOffset -= elementsWidth.get(index);
                mViewPager.setCurrentItem(index);
                scrollTo(currentWidthOffset, 0);
            } while(currentWidthOffsetSave - difference < currentWidthOffset - elementsWidth.get(checkIndex(index)) / 2);

            if(difference < 10) {
                startTimer(1);
            }
        }
        mCountDownTimer.cancel();
        mCountDownTimer.start();
    }

    private int checkIndex(int index) {
        if(index < 0) {

            index = 0;
        }
        if(index > totalElementsNumber - 1) {
            index = totalElementsNumber - 1;
        }
        return index;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        View firstTab = ((ViewGroup)getChildAt(0)).getChildAt(0);
        View lastTab = ((ViewGroup)getChildAt(0)).getChildAt(((ViewGroup)getChildAt(0)).getChildCount() - 1);
        ViewCompat.setPaddingRelative(getChildAt(0), (getWidth() / 2) - (firstTab.getWidth() / 2), 0, (getWidth() / 2) - (lastTab.getWidth() / 2) , 0);
    }
}

enter image description here

0 个答案:

没有答案