今天讲一个很简单的功能,就是可以切换的选项卡功能,很多app都有类似这种效果,实现的方法也有很多,这里我采用TabLayout加上ViewPager来实现,这里我做了一个封装,相当于一个工具类来着,哪个地方需要用到都可以使用,使用上我的那个封装类就可以了,我的封装类名是TabViewPager.java
先看效果图:可手动左右滑动也可以取消手动滑动
再看使用方法:需要先在自己的项目中引入我的TabViewPager.java类
现在布局中引用:
然后代码中使用:
这样就实现我们要的功能了,就这么简单,TabViewPager.java类中还提供了其他可以根据用户自己需要来设置的方法,比如有 setViewPagerCanScroll(boolean isScroll) 是否需要手滑切换功能
setTabPreloadRange(int range) 设置ViewPager预加载范围
setTitles(String[] titles) 设置标题
setTabTextColors(int normalColor, int selectedColor) 设置标题颜色
removeTabAt(int position) 设置默认tab的位置
setSelectedTabIndicatorColor(int color) 设置tab下面横线的颜色
很简单,内容就这么多,以下是完整代码。
public class TabViewPager extends FrameLayout {
private TabLayout mTabLayout;
private NoScrollViewPager mViewPager;
private TabFragmentPagerAdapter myPagerAdapter;
private final Context mContext;
private FrameLayout mFrameLayout;
public TabViewPager(@NonNull Context context) {
super(context);
this.mContext = context;
createView();
}
public TabViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
createView();
}
public TabViewPager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
createView();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public TabViewPager(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.mContext = context;
createView();
}
/**
* 设置不需要手滑功能
*/
public void setViewPagerCanScroll() {
mViewPager.setScrollAble();
}
private void createView() {
mFrameLayout = new FrameLayout(mContext);
LinearLayout linearLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams mm = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
mFrameLayout.addView(linearLayout, mm);
mTabLayout = new TabLayout(mContext);
LinearLayout.LayoutParams mw = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.addView(mTabLayout, mw);
mViewPager = new NoScrollViewPager(mContext);
mViewPager.setOffscreenPageLimit(3);
mViewPager.setId(R.id.basiclib_create_id);
linearLayout.addView(mViewPager, mw);
addView(mFrameLayout);
}
/**
* 设置ViewPager预加载范围
*
* @param range 范围
*/
public void setTabPreloadRange(int range) {
mViewPager.setOffscreenPageLimit(range);
}
//必须重写 初始化界面
public void initView(FragmentManager fm) {
myPagerAdapter = new TabFragmentPagerAdapter(fm,
mContext);
}
public void setTitles(String[] titles) {
myPagerAdapter.setTitles(titles);
}
public void setFragments(List<Fragment> fragments) {
myPagerAdapter.setFragments(fragments);
}
public void show() {
mViewPager.setAdapter(myPagerAdapter);
mTabLayout.setupWithViewPager(mViewPager);
}
//设置tab字体的颜色
public void setTabTextColors(int normalColor, int selectedColor) {
mTabLayout.setTabTextColors(normalColor, selectedColor);
}
//移动到那个位置
public void removeTabAt(int position) {
mTabLayout.removeTabAt(position);
}
//选择当前是哪一页
public void changeTabAt(int pos) {
mViewPager.setCurrentItem(pos);
}
//设置tab的下标颜色
public void setSelectedTabIndicatorColor(int color) {
mTabLayout.setSelectedTabIndicatorColor(color);
}
class TabFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] mTabTitles;
private final Context mContext;
private List<Fragment> mFragments;
TabFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
void setTitles(String[] titles) {
mTabTitles = titles;
}
void setFragments(List<Fragment> fragments) {
mFragments = fragments;
}
@Override
public int getCount() {
return mFragments.size();
}
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return mTabTitles[position];
}
}
class NoScrollViewPager extends ViewPager {
private boolean scrollAble = true;
void setScrollAble() {
this.scrollAble = false;
}
public NoScrollViewPager(@NonNull Context context) {
super(context);
}
public NoScrollViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return scrollAble && super.onInterceptTouchEvent(ev);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent ev) {
return scrollAble && super.onTouchEvent(ev);
}
}
public void dismiss() {
if (myPagerAdapter != null) {
myPagerAdapter = null;
}
if (mFrameLayout != null) {
mFrameLayout = null;
}
}
}