当前位置:   article > 正文

【Android -- 开源库】VerticalTabLayout 的基本使用

verticaltablayout

在这里插入图片描述

一、特性

  • 支持自定义Indicator大小

  • 支持自定义Indicator位置

  • 支持Indicator设置圆角

  • 支持Tab设置Badge

  • 支持Adapter的方式创建Tab

  • 多种Tab高度设置模式

  • Tab支持android:state_selected

  • 很方便的和ViewPager结合使用

  • 很方便的和Fragment结合使用

二、效果图

这里写图片描述

三、使用

1. 在 build.gradle 添加如下:

compile 'q.rorbin:VerticalTabLayout:1.2.5'
  • 1

2. 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/main_bg"
    tools:context="com.gyq.verticaltablelayouttest.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.2"
            android:gravity="center">

            <q.rorbin.verticaltablayout.VerticalTabLayout
                android:id="@+id/vertical_tab"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="40dp"
                android:layout_marginRight="40dp"
                app:indicator_color="#30000000"
                app:indicator_corners="20dp"
                app:indicator_gravity="fill"
                app:indicator_width="20dp"
                app:tab_height="50dp"
                app:tab_margin="20dp"
                app:tab_mode="scrollable" />
        </LinearLayout>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/icon_split_line" />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

3. MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ViewPager viewpager;
    private VerticalTabLayout tablayout;

    private MyPagerAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        tablayout = (VerticalTabLayout) findViewById(R.id.vertical_tab);

        viewpager.setAdapter(mAdapter);

        tablayout.setupWithViewPager(viewpager);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4. 适配器

/**
 * Created by gyq on 2018/2/28 15:53
 */

public class MyPagerAdapter extends FragmentPagerAdapter {
    private String[] mTitles = {"远程对讲","报警记录","开门记录","留言记录"};
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public BaseFragment getItem(int position) {
        BaseFragment fragment = SmartFragmentFactory.createFragment(position);
        return fragment;
    }

    @Override
    public int getCount() {
        return mTitles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

5. 注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface ActivityFragmentInject {

    /**
     * 顶部局的id
     *
     * @return
     */
    int contentViewId() default R.layout.activity_main;

    /**
     * 是否存在NavigationView
     *
     * @return
     */
    boolean hasNavigationView() default false;

    /**
     * 是否存在Toolbar
     *
     * @return
     */
    boolean hasToolbar() default false;

    /**
     * toolbar的标题id
     *
     * @return
     */
    int toolbarTitle() default -1;

    /**
     * 左边文本资源id
     * @return
     */
    int toolbarLeftText() default -1;

    /**
     * 右边文本资源id
     * @return
     */
    int toolbarRightText() default -1;

    /**
     * 左边图片资源id
     * @return
     */
    int toolbarLeftIcon() default -1;

    /**
     * 右边图片资源id
     * @return
     */
    int toolbarRightIcon() default -1;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

6. BaseFragment.java

/**
 * Created by gyq on 2018/2/28 15:55
 */

public abstract class BaseFragment extends Fragment implements MyToolBarClickListener {

    protected static final int DEFAULT_LOADING_TIME = 2000;
    protected static final int WHAT_LOAD = 1;
    protected static final int WHAT_LOAD_FINISH = 2;
    protected static final int WHAT_REFRESH = 3;

    protected Activity mActivity;
    protected Context mContext;
    protected View mRootView;

    protected ActivityFragmentInject annotation;
    protected int contentViewId;

    protected Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            toHandleMessage(msg);
        }
    };


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mContext = getContext();
        if (!getClass().isAnnotationPresent(ActivityFragmentInject.class)) {
            throw new RuntimeException("must use ActivityFragmentInitParams.class");
        }
        annotation = getClass().getAnnotation(ActivityFragmentInject.class);
        contentViewId = annotation.contentViewId();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = View.inflate(mContext, contentViewId, null);
        }
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        initViewNData();
        return mRootView;
    }

    protected void initViewNData() {
        if (annotation.hasToolbar()) {
            //initToolbar();
        }
        findViewAfterViewCreate();
        initDataAfterFindView();
    }

    protected void initToolbar() {

    }

    protected abstract void toHandleMessage(Message msg);

    protected abstract void findViewAfterViewCreate();

    protected abstract void initDataAfterFindView();


    @Override
    public void leftTextClick(View view) {
    }

    @Override
    public void leftIconClick(View view) {
    }

    @Override
    public void rightTextClick(View view) {
    }

    @Override
    public void rightIconClick(View view) {
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return true;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92

7. SmartFragmentFactory.java

/**
 * Created by gyq on 2018/2/28 15:54
 */

public class SmartFragmentFactory {
    private static HashMap<Integer, BaseFragment> mBaseFragments = new HashMap<Integer, BaseFragment>();
    public static BaseFragment createFragment(int position){
        BaseFragment baseFragment = mBaseFragments.get(position);

        if (baseFragment == null) {
            switch (position) {
                case 0:
                    baseFragment = new RemoteVideoFragment();
                    break;
                case 1:
                    baseFragment = new AlarmRecordFragment();
                    break;
                case 2:
                    baseFragment = new DoorOpenRecordFragment();
                    break;
                case 3:
                    baseFragment = new LeaveMsgFragment();
                    break;
            }

        }

        mBaseFragments.put(position, baseFragment);
        return baseFragment;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

8. MyToolBarClickListener.java

/**
 * Created by gyq on 2018/2/28 15:56
 */

public interface MyToolBarClickListener {
    void leftTextClick(View view);
    void leftIconClick(View view);
    void rightTextClick(View view);
    void rightIconClick(View view);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

9. 普通的Fragment

/**
 * Created by gyq on 2018/2/28 15:57
 */
@ActivityFragmentInject(
        contentViewId = R.layout.fragment_tab,
        hasNavigationView = false)
public class LeaveMsgFragment extends BaseFragment {
    @Override
    protected void toHandleMessage(Message msg) {

    }

    @Override
    protected void findViewAfterViewCreate() {

    }

    @Override
    protected void initDataAfterFindView() {

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

OK ,大致就是这样啦!!! 有啥疑问欢迎留言。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/684024
推荐阅读
相关标签
  

闽ICP备14008679号