Android imitates Jingdong Taobao screening and sliding, two ways to achieve

Two days ago, I saw a friend use PopWindow to do a screening and sliding, and it is not troublesome to read the demand. It just happened that there was a shopping mall project recently. I also learned Popwindow a few days ago (the previous side sliding was implemented with DrawerLayout) , I wrote it down, and implemented it with PopWindow and DrawerLayout respectively.

Let's talk about DrawerLayout first. This is a control added by Google under the V4 package. It has been out for a long time, and it is relatively simple to use. . . .


First look at the effect map



It is not troublesome to use, mainly the layout file

<android.support.v4.widget.DrawerLayout
        android:id="@+id/dlShow"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:background="#ffffff"
            android:orientation="vertical"
            android:alpha="1">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:text="brand" />

            <GridView
                android:id="@+id/brand"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:numColumns="3" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:text="price" />

            <GridView
                android:id="@+id/price"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:numColumns="3" />

        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

Of course, if you think this is OK, it must not be displayed. Now let's see how the Java code turns on and off the side slide

   btn_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    dlShow.openDrawer(Gravity.RIGHT);
                } else {
                    dlShow.closeDrawers();
                }
            }
        });

Then add the data you want



Then the effect comes out


The implementation of Popwindow is similar to that of DrawerLayout, and the code is not complicated. First inherit Popwindow and then instantiate these commonplace things. I will not talk about it. The main thing is to set the popwindow part. It is definitely not suitable for full-screen display. a moment), the comments are very detailed

Then it's adding data and the like, just like DrawerLayout, nothing special


then pop up

    btn_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    screen.showAtLocation(MainActivity.this.findViewById(R.id.pop), Gravity.RIGHT, 0, 0);
                    setBackgroundAlpha(0.8f);
                } else {
                    screen.dismiss();
                    setBackgroundAlpha(1f);
                }
            }
        });

Summarize:

DrawerLayout and Popwindow have their own advantages and disadvantages. The advantages of DrawerLayout can be placed in the same layout, no need to re-create the layout and it is easy to use, but data content and the like can only be added in the Activity, which will make the Activity bloated, full of methods, and time-consuming. It is easy to get confused after a long time. Popwindow needs to re-create the layout and set up the popwindow, but it can be customized, and the functions can be customized, and the limitations are small. It's up to you to choose the right one


Github address

CSDN address




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325173100&siteId=291194637