Android Development Learning Process 0.14

BindView ButterKnife

Easy to bind components and use
Handle click events easily, such as viewholder in adapter
At the same time, child components do not need to be bound when parent components are bound
Be careful
Used after setcontentview, and subspace cannot use static final attribute anymore

Expand the click event without changing the size of the button picture.

In the lower Android versions, there may be a problem with this approach, that is, the difference between src and setbackground s, which can also set background pictures, but src only loads picture resources without any other processing. Setbackgroundmakes pictures adaptive to button size, but there are also specific property settings. Image buttons have more detailed picture settings than regular buttons, but have less word processing.

recyclerview Settings Scrollbar automatically scrolls clicks to intermediate 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 how to override the lanerlayoutmanager class, similar to drawing components

  • Method of setting centersmoothscroller in click event
  • Also remember to set your custom layout for the recycler adapter

Popwindow with recycler usage settings

Popwindow is not usually used directly. It can be used more flexibly with recycler. The difference between popwindow and dialog traditional dialog box can be displayed anywhere, which is more flexible.
Combining 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, subtract it from the center line position of the window, and calculate the offset.The center line position is calculated from the (tail-head)/2+head.
Typically, writing an extra class instead of directly in the activity causes redundancy.
An adapter class is also created to facilitate setting up the data, and layoutparams can also set the size of the popwindow and set the offset from the origin through the last two properties of showatlocation, such as the third property beingGravity.no_Gravity displays the origin in the upper left corner by default, so you should set the parameters as appropriate

MWindow.init Method

Active does not directly control the view. The view is controlled by the window class, which has an interface MWindow for use.MWindow.init.lightoff() and lighton() methods can change the main activity state when popwindwo is turned on or off, noting that the parent layout of the interface level is activity, popwindow, such as linerlayout, popwindow child controls.

Tags: Android ButterKnife Attribute less

Posted on Mon, 08 Jun 2020 13:19:15 -0400 by jacinthe