赞
踩
支持自定义Indicator大小
支持自定义Indicator位置
支持Indicator设置圆角
支持Tab设置Badge
支持Adapter的方式创建Tab
多种Tab高度设置模式
Tab支持android:state_selected
很方便的和ViewPager结合使用
很方便的和Fragment结合使用
1. 在 build.gradle 添加如下:
compile 'q.rorbin:VerticalTabLayout:1.2.5'
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>
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);
}
}
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];
}
}
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;
}
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;
}
}
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;
}
}
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);
}
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() {
}
}
OK ,大致就是这样啦!!! 有啥疑问欢迎留言。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。