Android Development Tips-Implementing a reusable Actionsheet menu

Source: Internet
Author: User

In the previous article, "Android Development Tips-Using dialog to implement the Actionsheet menu of imitation QQ," the implementation of this menu, the next will be changed to a reusable control library.

This article original, reproduced please indicate the source:

http://blog.csdn.net/maosidiaoxian/article/details/46324941

For the reusable control library to be implemented, I need it to have the following two points:

    1. Remote dependencies can be added (regardless of usage in eclipse)
    2. Flexible to configure
Implementation code for the detach library

For the 1th, the need to do is to create a new library module in Android Studio, and then put the relevant implementation code, resources to drag the past, and the library bid, some strings and other unused resources deleted, Keep the androidmanifest.xml clean (keep only the permissions, claims, etc.) that are used. Here, the androidmanifest of our library is finally left only:

<manifest    package="com.githang.android.actionsheet">    <application/></manifest>

Note that the application attributes in the node are allowBackup to be removed, otherwise it will let some people who do not know how to resolve the conflict with several attributes in tools, facing the merger conflict of Androidmanifest.xml at a loss. labeland icon attributes are not needed here, so it is also removed.

Then add the package name of the module that you created at the beginning with the app . Demo suffix, com.githang.andorid.actionsheet just as our library package name (this is purely personal habits, you can not do so.) I think it's better to differentiate the library from the demo in the name of the package, and I don't want to add one more .library package to the library.

After the separation, the project is this:
https://github.com/msdx/ActionSheet/commit/934b73bc3e2d1504c9b13e87649ce388c59f4613

After the separation, we can package our library into AAR and upload it to jcenter so that others can use it in a way that adds remote dependencies. How to publish to Jcenter, you can see me this blog: "Use Gradle release Android open source project to Jcenter".

But now, we are only doing the most basic work, because, you can not ask everyone to a library requirements are exactly the same as you, there may be some people's needs, need to change some of the properties of the UI or something. So next, we need to make our library flexible to configure.

Free-to-configure properties

Most of our properties are defined in XML. To change these properties, there are two main ways of doing this:
1. Provide some interface on the UI of the view in Java code so that the third party can set it by calling it.
2. A reference to a property is used in a layout file instead of using its value directly.

If you are writing custom controls (by inheriting view), then you may also need to customize some properties so that others can add them in the XML when they are in use. Since we are not writing this type of control here, we do not need to use it, and we will not dwell on it here.

Keep talking about the two methods above. In Android, using XML to define the layout is to separate the layout code from the logic code, so the first way I try to do this is to use less (in the case of a dynamic setting or a simpler setting in Java code). Let's talk about the implementation of the second way.

There are three controls we define in XML, one is the ListView, the other is the button, and the third is the ListView item. So first of all, create a new Attrs.xml property file in the Values folder, and define three attributes in format it, yes reference . As follows:

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <attr name="actionsheetlist" format="Reference"/>    <attr name="Actionsheetcancel" format="Reference"/>     <attr name="Actionsheetitem" format="Reference"/ ></Resources>

Next, change the previously written layout file to use ?attr the way it declares the property.

Menu_item.xml

<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android"          android:id="@android:id/text1"          style="?attr/ActionSheetItem" />

Dialog_action_sheet.xml

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  android:layout_width  =" match_parent " android:layout_height  =" match_parent " android:orientation  =" vertical ";     <ListViewandroid:id= "@+id/menu_items"android:layout_width=" Match_parent "android:layout_height=" wrap_content "style="? attr/ Actionsheetlist "android:listselector=" @android: Color/transparent "/>                                            <buttonandroid:id="@+id/menu_cancel"style="? attr/ Actionsheetcancel "/>                </linearlayout>

Then declare the style of these properties in our style:

    <style name="Actionsheetlist"><item name= "android:d ivider">#c9dddddd</Item > <item name= "android:d ividerheight">1px </Item></style>    <style name="Actionsheetitem"><Item name="Android: Layout_width ">match_parent</Item> <Item name="Android: Layout_height ">45DP</Item> <Item name="Android: TextSize ">18SP</Item> <Item name="Android: Gravity ">Center</Item> <Item name="Android: TextColor ">@color/menu_text</item>     </style>    <style name="Actionsheetcancel" parent="Actionsheetitem"> <item name= "android: Layout_margintop">8dp</item
      > <item name= "android: Layout_marginbottom">8dp</ Item> <item name= "android: Background">@ drawable/menu_item_single</item>      </style>

Note that the name of the style here does not require the same name as declared in attr, where I am just too lazy to think of other names, really.

After doing so, others will need one more step when they are in use: In his styler apptheme (depending on the theme specified in androidmanifest) node, the following items need to be added:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <item name="ActionSheetList">@style/ActionSheetList</item>        <item name="ActionSheetItem">@style/ActionSheetItem</item>        <item name="ActionSheetCancel">@style/ActionSheetCancel</item>    </style>

If he wants to modify the value of the property himself, just write the corresponding style and change the value in the item above to the style he wrote.

For the menu changes, we will change to here.
The next step is to refactor the Actionsheetdialog to provide an API that can be set up as follows:

    • Animate when display and hide are set
    • Setting the background of a menu

The first is the background of the menu, which has been said to have a total of four backgrounds. So the class that defines a menu background:

    /** * Menu Background * /     Public Static  class menubackground {         Public intTop Public intMiddle; Public intBottom Public intSingle Public Menubackground() {} Public Menubackground(intTopintMiddle,intBottomintSingle) { This. top = top; This. middle = middle; This. bottom = bottom; This. Single = single; }    }

Then define a menu background object, give it an initial value, and define a method for setting the background:

    privatenew MenuBackground(R.drawable.menu_item_top,            R.drawable.menu_item_middle, R.drawable.menu_item_bottom, R.drawable.menu_item_single);    publicvoidsetMenuBackground(intintintint single) {        mMenuBg.top = top;        mMenuBg.middle = middle;        mMenuBg.bottom = bottom;        mMenuBg.single = single;    }

Finally the configuration of the remaining animations. Define two ways to set the display and hide animation individually, and note that in the method of hiding the animation, you want to set the callback for the end of the animation, and hide our menu here.

The code refactoring for animation settings is as follows:

    Private void Initanim(Context context)        {Setshowanimation (animationutils.loadanimation (context, r.anim.translate_up));    Setdismissanimation (animationutils.loadanimation (context, r.anim.translate_down)); }/** * @param animation Showing animation. * @since 0.2 * *     Public void setshowanimation(Animation Animation)    {Mshowanim = animation; }/** * @param animation dismissing animation. * @since 0.2 * *     Public void setdismissanimation(Animation Animation)        {Mdismissanim = animation; Mdismissanim.setanimationlistener (NewAnimation.animationlistener () {@Override             Public void Onanimationstart(Animation Animation) {            }@Override             Public void Onanimationend(Animation Animation)            {dismissme (); }@Override             Public void onanimationrepeat(Animation Animation)    {            }        }); }

Now that our library has been refactored, it exposes the appropriate APIs so that others can customize some of the properties. It also provides a way to configure the layout file, which is configured through a style.

Finally is the version number, changed the Readme, released a new version, hit tag.

The project concludes as follows:
https://github.com/msdx/ActionSheet/tree/0.2

Android Development Tips-Implementing a reusable Actionsheet menu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.