Android development learning process 0.14 bindview recyclerview popwindow

Time:2021-1-24

BindView ButterKnife

advantage
The binding component is convenient and easy to use
It is convenient to handle click events, such as viewholder in adapter
At the same time, after the parent component is bound, the child component does not need to be bound
be careful
It is used after setcontentview, and the static final attribute can no longer be used in the subspace

Expand the click event without changing the size of the button image,

In the lower Android version, there may be a problem with this method, that is, the difference between SRC and setbackground can also set the background image, but SRC only loads the image resources without other processing, while setbackground will make the image adaptive to the button size, but it also has specific attribute settings. Imagebutton has more detailed image settings than conventional button, but weaker word processing.

How to use recycleview

Recyclerview sets the scroll bar to automatically scroll the selected items to the middle method

  • Rewrite the linerlayout class as follows:
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);
        }
    }
}

Note that the method of overriding the lanerlayoutmanager class is similar to drawing components

  • Method of setting centersmoothscroller in click event
  • At the same time, remember to set up your custom layout for the recycler adapter

Related properties of recycleview

  • Overscrollmode: never sets the slide to edge without animation
  • Overscrollfooter sets the sliding bottom color background
  • Overscrollheader sets the sliding head color background
  • Scroll bar = “None” sets whether scroll bars are displayed

Popwindow with recycler to use settings

Generally, popwindow is not used directly. With the help of recycler, you can use it more flexibly. Different from the traditional dialog box, popwindow can be displayed in any position, which is more flexible,
Combine with 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() {
                //Restore transparency in dismiss
                public void onDismiss() {
                    MWindowManager.init(getActivity()).lighton();
                }
            });
}

The principle is to calculate the center line position of each item and subtract it from the center line position of the window to calculate the offset. The center line position is calculated by (tail head) / 2 + head.
Generally, an extra class is written instead of directly in the activity, which will cause redundancy.
At the same time, an adapter class needs to be established to facilitate data setting. Layoutparams can also set the size of popwindow. When displaying, the offset from the origin can be set through the last two properties of showatlocation. For example, the third property is gravity.no_ Gravity will display the origin in the upper left corner by default, so the parameters should be set according to the actual situation

MWindow.init method

Activity does not directly control the view. The window class controls the view. It has an interface mwindow for use MWindow.init.lightoff () method and similar lighton () method can realize the light and dark change of the main activity state when popwindow is opened and closed. At this time, pay attention to the parent layout of activity and popwindow, such as linelayout and child control of popwindow.