am.widget.tabstrip.TabStripView.java Source code

Java tutorial

Introduction

Here is the source code for am.widget.tabstrip.TabStripView.java

Source

/*
 * Copyright (C) 2018 AlexMofer
 *
 * 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.
 */

package am.widget.tabstrip;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;

import am.widget.basetabstrip.R;

/**
 * ?
 */
@SuppressWarnings("unused")
@ViewPager.DecorView
public abstract class TabStripView extends View {

    private final TabStripHelper mHelper = new TabStripHelper(this);

    public TabStripView(Context context) {
        super(context);
        initView(context, null);
    }

    public TabStripView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public TabStripView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, @Nullable AttributeSet attrs) {
        final TypedArray custom = context.obtainStyledAttributes(attrs, R.styleable.TabStripView);
        mHelper.set(custom.getResourceId(R.styleable.TabStripView_tsvViewPager, NO_ID),
                custom.getBoolean(R.styleable.TabStripView_tsvAutoFindViewPager, true),
                custom.getBoolean(R.styleable.TabStripView_tsvClickSmoothScroll, false));
        custom.recycle();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        mHelper.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mHelper.onDetachedFromWindow();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final boolean touch = mHelper.onTouchEvent(event);
        final boolean result = super.onTouchEvent(event);
        return touch || result;
    }

    @Override
    public boolean performClick() {
        final boolean click = mHelper.performClick();
        final boolean result = super.performClick();
        if (result)
            return true;
        if (click) {
            playSoundEffect(SoundEffectConstants.CLICK);
            return true;
        }
        return false;
    }

    /**
     * 
     *
     * @param position     ?
     * @param smoothScroll ?
     * @return ??
     */
    public boolean performClick(int position, boolean smoothScroll) {
        if (mHelper.isBoundViewPager()) {
            mHelper.setCurrentItem(position, smoothScroll);
            return true;
        }
        return false;
    }

    /**
     * ?
     *
     * @param first        ?
     * @param second       ?
     * @param smoothScroll ?
     */
    public void performDoubleClick(int first, int second, boolean smoothScroll) {
    }

    /**
     * ?
     *
     * @param pager ViewPager
     */
    protected void onAttachedToViewPager(@NonNull ViewPager pager) {
    }

    /**
     * 
     *
     * @param pager ViewPager
     */
    protected void onDetachedFromViewPager(@NonNull ViewPager pager) {
    }

    /**
     * ViewPagerPagerAdapter
     *
     * @param oldAdapter PagerAdapter
     * @param newAdapter PagerAdapter
     */
    protected void onViewPagerAdapterChanged(@Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
    }

    /**
     * ViewPagerPagerAdapter??
     */
    protected void onViewPagerAdapterDataChanged() {
    }

    /**
     * ViewPager?
     *
     * @param position ??
     * @param offset   ??
     */
    protected void onViewPagerScrolled(int position, float offset) {
    }

    /**
     * ViewPager?
     *
     * @param position ??
     */
    protected void onViewPagerItemSelected(int position) {
    }

    /**
     * ViewPager??
     *
     * @param state ?
     */
    protected void onViewPagerScrollStateChanged(int state) {
    }

    /**
     * ViewPager??
     *
     * @param position ?
     * @param offset   ??
     */
    protected abstract void onViewPagerChanged(int position, float offset);

    /**
     * ??
     *
     * @param id       ID
     * @param position ??{@link PagerAdapter#POSITION_NONE}??
     * @param tag      ?
     */
    protected void onObservableChangeNotified(int id, int position, @Nullable Object tag) {
    }

    /**
     * ??
     *
     * @param downX ACTION_DOWN X??
     * @param downY ACTION_DOWN Y??
     * @param upX   ACTION_UP X??
     * @param upY   ACTION_UP Y??
     * @return ?
     */
    protected int getClickedPosition(float downX, float downY, float upX, float upY) {
        return PagerAdapter.POSITION_NONE;
    }

    /**
     * ??
     *
     * @param respond ?
     */
    protected void setRespondClick(boolean respond) {
        mHelper.setRespondClick(respond);
    }

    /**
     * ???
     *
     * @param respond ??
     */
    protected void setRespondDoubleClick(boolean respond) {
        mHelper.setRespondDoubleClick(respond);
    }

    /**
     * ?
     *
     * @param observable ?
     */
    protected void setObservable(TabStripObservable observable) {
        mHelper.setObservable(observable);
    }

    /**
     * ??
     * {@link #performClick(int, boolean)}????
     *
     * @return ??
     */
    protected boolean isDoubleClick() {
        return mHelper.isDoubleClick();
    }

    /**
     * ?
     *
     * @param position ??
     * @return 
     */
    @Nullable
    protected CharSequence getPageTitle(int position) {
        return mHelper.getPageTitle(position);
    }

    /**
     * ?
     *
     * @return 
     */
    protected int getPageCount() {
        return mHelper.getPageCount();
    }

    /**
     * ?ViewPager
     *
     * @param pager ?ViewPager
     */
    public void bindViewPager(ViewPager pager) {
        mHelper.bindViewPager(pager);
    }

    /**
     * 
     *
     * @param force ?
     */
    public void updateView(boolean force) {
        mHelper.updateView(force);
    }

    /**
     * ?
     *
     * @param smoothScroll ?
     */
    protected void setClickSmoothScroll(boolean smoothScroll) {
        mHelper.setClickSmoothScroll(smoothScroll);
    }
}