VerticalTabLayout + ViewPager 竖着的 TabLayout

由于项目需要如下图,类似于平板中设置界面,今天做一个 demo 记录一下。
VerticalTabLayout + ViewPager 竖着的 TabLayout_第1张图片

1.在 build.gradle 添加如下:

compile 'q.rorbin:VerticalTabLayout:1.2.5'

2.布局文件:


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

3.MainActivity.java

package com.gyq.verticaltablelayouttest;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;

import q.rorbin.verticaltablayout.VerticalTabLayout;

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);
    }
}

4.适配器

package com.gyq.verticaltablelayouttest;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

/**
 * 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];
    }
}

5.注解

package com.gyq.verticaltablelayouttest.base;


import com.gyq.verticaltablelayouttest.R;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@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;

}

6.BaseFragment.java

package com.gyq.verticaltablelayouttest;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyq.verticaltablelayouttest.base.ActivityFragmentInject;

/**
 * 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;
    }
}

7.SmartFragmentFactory.java

package com.gyq.verticaltablelayouttest;

import com.gyq.verticaltablelayouttest.fragment.AlarmRecordFragment;
import com.gyq.verticaltablelayouttest.fragment.DoorOpenRecordFragment;
import com.gyq.verticaltablelayouttest.fragment.LeaveMsgFragment;
import com.gyq.verticaltablelayouttest.fragment.RemoteVideoFragment;

import java.util.HashMap;

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

public class SmartFragmentFactory {
    private static HashMap mBaseFragments = new HashMap();
    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;
    }
}

8.MyToolBarClickListener.java

package com.gyq.verticaltablelayouttest;

import android.view.View;

/**
 * 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);
}

9.普通的Fragment

package com.gyq.verticaltablelayouttest.fragment;

import android.os.Message;

import com.gyq.verticaltablelayouttest.BaseFragment;
import com.gyq.verticaltablelayouttest.R;
import com.gyq.verticaltablelayouttest.base.ActivityFragmentInject;

/**
 * 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() {

    }
}

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

你可能感兴趣的:(Android开源框架)