Android 开发学习进程0.14 recyclerview滚动位置问题 popwindow使用 window类属性

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();
                }
            });
}

同时注意popupwindow 在activity生命周期的表现,popupwindow需要附着在某一组件上,因此需要activity或fragment 的view加载完成再调用显示,否则可能因关闭等造成崩溃如下

android.view.WindowManager$BadTokenException: Unable to add window -- token null for displayid = 0 is not valid; is your activity running?

解决方法有两种:一、在activity中重写 onWindowFocusChange方法

@Override  
public void onWindowFocusChanged(boolean hasFocus) {  
    super.onWindowFocusChanged(hasFocus);  
    if (isFirst){  
        isFirst=false;  
        window.showAtLocation(view, Gravity.CENTER,0,0);  
    }  
}  

二、异步显示 在fragment或activity中使用

getWindow().getDecorView().post(new Runnable() {  
            @Override  
            public void run() {  
                window.showAtLocation(view, Gravity.CENTER,0,0);  
            }  
        });  

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

MWindow.init 方法

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

window 的各种属性

无论是activity fragment 或dialog都是在window上显示的,style决定了许多属性,如 dialogwindowfloating=true表示窗口浮动,此时设置dialog的大小将不起作用,window将被设置成wrapcontent适应大小,如在dialogFragment中可以在onstart方法中设置window的Layout的parameter中修改大小。但此时的修改会影响statusbar的颜色,可以让window高度减去statubar的高度,可以显示下面的statubar。

posted @ 2020-06-08 23:39  百密一疏  阅读(647)  评论(0编辑  收藏  举报