Android development learning process 0.14 bindview recyclerview popwindow


BindView ButterKnife

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) {
   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);
    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
        RecyclerView.SmoothScroller smoothScroller = new CenterSmoothScroller(recyclerView.getContext());

    private static class CenterSmoothScroller extends LinearSmoothScroller {

        CenterSmoothScroller(Context context) {
        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(;
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),4));
        Classify_PopAdapter popAdapter =new Classify_PopAdapter(getContext(),mList);
        popAdapter.setClickListener(new AdapterClickListener() {
            public void click(int flag, int position) {
        mPopWindow = new PopupWindow(view,
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            mPopWindow.showAtLocation(pop_btn, Gravity.NO_GRAVITY, 187, 72);
            mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                //Restore transparency in dismiss
                public void onDismiss() {

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.