效果如图,很常见的一种布局方式,只是没有采用radiobutton,方法都很多,很多人都自定义写在一个view里面,不过代码量偏大,所以还是xml写吧
这样写下来感觉代码量还是挺少的,而且很好扩展,然后在点击的时候加了个回弹动画,顿时体验好多了
public class MainActivity extends BaseActivity implements IMainView ,ViewPager.OnPageChangeListener{ @BindView(R.id.viewPager) ViewPager viewPager; private MainAdapter adapter; private SparseArraymenus = new SparseArray<>(); public static void start(Context context) { start(context, null); } public static void start(Context context, Intent extras) { Intent intent = new Intent(); intent.setClass(context, MainActivity.class); if (extras != null) { intent.putExtras(extras); } context.startActivity(intent); } @Override public int layoutID() { return R.layout.activity_main; } @Override public void init() { adapter = new MainAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); viewPager.setOffscreenPageLimit(FragmentHelper.getCount()); viewPager.addOnPageChangeListener(this); addTab(findViewById(R.id.home),0); addTab(findViewById(R.id.contact),1); addTab(findViewById(R.id.community),2); addTab(findViewById(R.id.me),3); select(0); } @Override public void success() { } @Override public void error() { } public void click(View view){ int position = 0; switch (view.getId()){ case R.id.home: position = 0; break; case R.id.contact: position = 1; break; case R.id.community: position = 2; break; case R.id.me: position = 3; break; } select(position); } /**添加首页按钮缓存*/ private void addTab(View view,int position){ ViewGroup group = cover(view); CheckBox checkBox = cover(group.getChildAt(0)); checkBox.setChecked(false); menus.put(position,checkBox); } /**选择菜单*/ private void select(int position){ if (menus.get(position).isChecked()) return;//防止执行多次 Log.e("tag",position+""); for (int i = 0;i < menus.size();i++) { CheckBox box = menus.get(i); if (box == null) continue; if (i == position) { box.setChecked(true); }else box.setChecked(false); } viewPager.setCurrentItem(position, false); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { select(position); } @Override public void onPageScrollStateChanged(int state) { }}
public class HomeFragment extends LazyFragment { @BindView(R.id.tv_tag) TextView tv_tag; @Override protected int getLayoutId() { return R.layout.fragment_home; } @Override protected void init() { String tag = getArguments().get("tag").toString(); if (tag != null) tv_tag.setText(tag); } @OnClick({R.id.search}) public void search(View view){ FragmentFactory.start(getActivity(),FragmentFactory.SEARCH); }}
public class BounceView extends android.support.v7.widget.AppCompatCheckBox{ public BounceView(Context context) { super(context); init(); } public BounceView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public BounceView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init(){ setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) startAnimation(); } }); } public void startAnimation() { ValueAnimator anim = ValueAnimator.ofInt(-10,0,6,0); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int curValue = (int) animation.getAnimatedValue(); layout( getLeft(), curValue+((ViewGroup)getParent()).getPaddingTop(), getRight(), curValue+getHeight()+((ViewGroup)getParent()).getPaddingBottom() ); } }); anim.setInterpolator(new BounceInterpolator()); anim.setDuration(500); anim.start(); }}
public class MainAdapter extends FragmentPagerAdapter { public MainAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return FragmentHelper.getFragment(position); } @Override public int getCount() { return FragmentHelper.getCount(); }}
public class FragmentHelper { private static SparseArray> arr = new SparseArray >(); public static LazyFragment getFragment(int pos) { LazyFragment fragment = null; if (null != arr.get(pos)) fragment = arr.get(pos).get(); if (null == fragment) { switch (pos) { case 0: fragment = new HomeFragment(); break; case 1: fragment = new HomeFragment(); break; case 2: fragment = new HomeFragment(); break; case 3: fragment = new HomeFragment(); break; default: break; } Bundle bundle = new Bundle(); bundle.putString("tag",pos+""); fragment.setArguments(bundle); arr.put(pos, new SoftReference (fragment)); } return fragment; } public static int getCount(){ return 4; }}
fragment采用懒加载方式,这个就不说了
public abstract class LazyFragment extends BaseFragment { protected boolean isLoad;//是否加载完 /** * 在这里实现Fragment数据的缓加载. */ @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if(getUserVisibleHint()) { isVisible = true; onVisible(); } else { isVisible = false; } } protected void onVisible(){ if(!isPrepared || !isVisible || !isLoad) { return; } init(); Log.e("tag","init success"); }}
GitHub下载地址: