zoukankan      html  css  js  c++  java
  • Android 开发学习进程0.14 Bindview recyclerview popwindow使用 window类属性使用

    BindView ButterKnife

    优势
    绑定组件方便,使用简单
    处理点击事件方便,如adapter中的viewholder
    同时父组件绑定后子组件无需绑定
    注意
    在setcontentview之后使用,且子空间不可再使用static final属性

    在不改变按钮图片大小的情况,扩大点击事件,

    在较低Android版本此方法可能有问题,即src和setbackground的区别,同样可以设置背景图片,但src仅将图片资源加载,不做其他处理,而setbackground会使图片自适应与按钮大小,但也有具体属性设置,imagebutton相比常规button对图片设置更加详细,但对字处理较弱。

    recycleview 使用方法

    recyclerview设置滚动条自动将点击项滚动到中间方法

    • 重写linerlayout类,如下代码:
        public CenterLayoutManager(Context context) {
            super(context);
        }
       public CenterLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }
        public CenterLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }
        @Override
        public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
            RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());
            smoothScroller.setTargetPosition(position);
            startSmoothScroll(smoothScroller);
        }
    
        private static class CenterSmoothScroller extends LinearSmoothScroller {
    
            CenterSmoothScroller(Context context) {
                super(context);
            }
            @Override
            public int calculateDtToFit(int viewStart, int viewEnd, int boxStart, int boxEnd, int snapPreference) {
                return (boxStart + (boxEnd - boxStart) / 2) - (viewStart + (viewEnd - viewStart) / 2);
            }
        }
    }
    

    注意重写lanerlayoutmanager类的方法,与绘制组件较相似

    • 在点击事件中设置centersmoothscroller的方法
    • 同时在记得为recycler适配器设置上你的自定义布局

    recycleview 相关属性

    • overscrollmode:never 设置滑动到边缘无动画效果
    • overscrollFooter 设置滑动底部颜色背景
    • overscrollHeader 设置滑动头部颜色背景
    • scrollbar="none" 设置是否显示滚动条

    Popwindow配合recycler使用设置

    popwindow一般不直接使用,配合recycler可以有更加多变的使用方法,popwindow与dialog传统对话框区别可以在任意位置显示,更加灵活,
    与recycler结合

            View view = LayoutInflater.from(getContext()).inflate(R.layout.pop_classifyfragment,null);
            RecyclerView recyclerView =(RecyclerView) view.findViewById(R.id.pop_classifyfragment_list);
            recyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
            Classify_PopAdapter popAdapter =new Classify_PopAdapter(getContext(),mList);
            recyclerView.setAdapter(popAdapter);
            popAdapter.setClickListener(new AdapterClickListener() {
                @Override
                public void click(int flag, int position) {
                }
            });
            mPopWindow = new PopupWindow(view,
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                mPopWindow.setContentView(view);
                mPopWindow.showAtLocation(pop_btn, Gravity.NO_GRAVITY, 187, 72);
                MWindowManager.init(getActivity()).lightoff();
                mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    //在dismiss中恢复透明度
                    public void onDismiss() {
                        MWindowManager.init(getActivity()).lighton();
                    }
                });
    }
    

    原理为计算每一项的中心线位置,与窗口的中心线位置相减,计算出偏移量。而中心线位置由 (尾-头)/2+头 计算得出。
    一般为额外写一个类,不直接在activity中写,会造成冗余。
    同时还需建立adapter类,便于设置数据,layoutparams还可设置popwindow的大小,展示时通过showatlocation后两个属性设置与原点偏移量,如第三个属性为gravity.no_gravity默认会在左上角显示原点,所以应根据实际情况设置参数

    MWindow.init 方法

    activity并不直接控制视图,控制视图的为window类,它有接口MWindow供使用,使用MWindow.init.lightoff() 方法和类似lighton()方法可以实现popwindwo打开关闭时主activity状态的明暗变化,此时注意界面层次为activity、popwindow的父布局如linerlayout、popwindow的子控件。

  • 相关阅读:
    第十二章学习笔记
    UVa OJ 107 The Cat in the Hat (戴帽子的猫)
    UVa OJ 123 Searching Quickly (快速查找)
    UVa OJ 119 Greedy Gift Givers (贪婪的送礼者)
    UVa OJ 113 Power of Cryptography (密文的乘方)
    UVa OJ 112 Tree Summing (树的求和)
    UVa OJ 641 Do the Untwist (解密工作)
    UVa OJ 105 The Skyline Problem (地平线问题)
    UVa OJ 100 The 3n + 1 problem (3n + 1问题)
    UVa OJ 121 Pipe Fitters (装管子)
  • 原文地址:https://www.cnblogs.com/baimiyishu/p/13069721.html
Copyright © 2011-2022 走看看