Android 自定义tabbar 用viewPage实现
2019-03-26 本文已影响0人
fordG
- 底部tabbar使用的 JPTabBar
activity中的布局
<com.example.fordgao.travel.baseUI.TabViewPage
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/viewPage"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/line"
android:background="@color/colorAccent"/>
<TextView
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/line"
app:layout_constraintTop_toBottomOf="@id/viewPage"
app:layout_constraintBottom_toTopOf="@+id/tabbar"/>
<com.jpeng.jptabbar.JPTabBar
android:id="@+id/tabbar"
android:layout_width="match_parent"
android:layout_height="56dp"
app:layout_constraintBottom_toBottomOf="parent"
android:background="#fff"
/>
TabViewPage 去掉滑动手势
package com.example.fordgao.travel.baseUI;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
public class TabViewPage extends ViewPager {
public TabViewPage(Context context) {
super(context);
}
public TabViewPage(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
@Override
public void setCurrentItem(int item) {
super.setCurrentItem(item, false);
}
}
activity代码
JPTabBar jpTabBar;
@Titles
private static final String[] mTitles = {"首页","订机票","我的"};
@SeleIcons
private static final int[] mSeleIcons = {R.mipmap.tab_home_selected,R.mipmap.tab_air_selected,R.mipmap.tab_mine_selected};
@NorIcons
private static final int[] mNormalIcons = {R.mipmap.tab_home, R.mipmap.tab_air, R.mipmap.tab_mine};
ViewPager viewPager;
List<Fragment> mFragmentList = new ArrayList<Fragment>();
MainPagerAdapter mViewPagerFragmentAdapter;
FragmentManager mFragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Tool.setStatusBarColor(this, R.color.colorNav);
overridePendingTransition(R.anim.anim_alpha_show, R.anim.view_stay);
jpTabBar = findViewById(R.id.tabbar);
jpTabBar.setSelectedColor(ContextCompat.getColor(this, R.color.tabSColor));
jpTabBar.setTabTextSize(12);
viewPager = findViewById(R.id.viewPage);
mFragmentManager = getSupportFragmentManager();
Fragment_home home = new Fragment_home();
Fragment_air air = new Fragment_air();
Fragment_mine mine = new Fragment_mine();
((ArrayList<Fragment>) mFragmentList).add(home);
((ArrayList<Fragment>) mFragmentList).add(air);
((ArrayList<Fragment>) mFragmentList).add(mine);
mViewPagerFragmentAdapter = new MainPagerAdapter(mFragmentManager, (ArrayList<Fragment>) mFragmentList);
viewPager.setAdapter(mViewPagerFragmentAdapter);
viewPager.setCurrentItem(0);
jpTabBar.setTabListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int index) {
viewPager.setCurrentItem(index);
}
@Override
public boolean onInterruptSelect(int index) {
return false;
}
});
}
import java.util.ArrayList;
public class MainPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> list;
//通过构造获取list集合
public MainPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.list=list;
}
//设置每一个的内容
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
//设置有多少内容
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
}
Fragment基类使用懒加载数据
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public abstract class BaseLazyFragment extends Fragment {
private boolean isFirst; // 是否首次加载
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(getLayoutId(), container, false);
}
@LayoutRes
protected abstract int getLayoutId();
/**
* 懒加载
*/
protected abstract void onLazyLoad();
@Override
public void onResume() {
super.onResume();
if (!isFirst) {
initUiAndListener();
onLazyLoad();
isFirst = true;
}
}
/**
* 控件初始化和监听
*/
protected abstract void initUiAndListener();
}
在子类Fragment中
import base.components.BaseLazyFragment;
public class FragmentHome extends BaseLazyFragment {
@Override
protected int getLayoutId() {
return R.layout.fragment_home;
}
@Override
protected void onLazyLoad() {
}
@Override
protected void initUiAndListener() {
}
}