博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
菜单布局记录篇
阅读量:5245 次
发布时间:2019-06-14

本文共 7266 字,大约阅读时间需要 24 分钟。

 

效果如图,很常见的一种布局方式,只是没有采用radiobutton,方法都很多,很多人都自定义写在一个view里面,不过代码量偏大,所以还是xml写吧

这样写下来感觉代码量还是挺少的,而且很好扩展,然后在点击的时候加了个回弹动画,顿时体验好多了

public class MainActivity extends BaseActivity implements IMainView ,ViewPager.OnPageChangeListener{    @BindView(R.id.viewPager)    ViewPager viewPager;    private MainAdapter adapter;    private SparseArray
menus = 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) { }}
MainActivity
View Code
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();    }}
View Code
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();    }}
View Code
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; }}
View Code

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");    }}
View Code

 

GitHub下载地址:

转载于:https://www.cnblogs.com/LiuZhen/p/7865625.html

你可能感兴趣的文章
Kruskal基础最小生成树
查看>>
浅谈算法和数据结构: 一 栈和队列
查看>>
Java内部类详解
查看>>
【hdu 1429】胜利大逃亡(续)
查看>>
图论-次短路求法
查看>>
What's New for Visual C# 6.0
查看>>
ExtJs学习笔记之ComboBox组件
查看>>
关于收费软件
查看>>
getopt_long
查看>>
TensorFlow MNIST CNN 代码
查看>>
javascript之Style物
查看>>
JSON跨域解决方案收集
查看>>
SSH框架整合总结
查看>>
图的深度优先遍历
查看>>
C# 之 提高WebService性能大数据量网络传输处理
查看>>
md5sum命令详解
查看>>
[bzoj1004] [HNOI2008] Cards
查看>>
原生HttpClient详细使用示例
查看>>
几道面试题
查看>>
Factory Design Pattern
查看>>