Android Imitation American Group Pull-down menu (commodity selection) Instance code _android

Source: Internet
Author: User
Tags stub

We use a lot of applications platform, the following small series through this article to introduce you to the electric business application platform commonly used in the selection of the Drop-down list of the implementation of the category. First to show you the effect of the picture:

The implementation of a Drop-down list

In fact, there are many ways to achieve, then there is no technical content, but summed up their own practice in the project, but also provide a way of thinking.

First is the list of data, the general data are read from the background, here because there is no background, so write dead on the client:

private void Initmenudata () {menudata = new arraylist<map<string, string= "" >> ();
string[] Menustr = new string[] {"All", "grain and oil", "clothes", "books", "Electronic products", "drinks", "fruit"};
Map<string, string= "" > map; for (int i =, Len = Menustr.length i < len; ++i) {map = new hashmap<string, string= "" > (); Map.put ("Name", menu
Str[i]);
Menudata.add (map);
} menudata = new arraylist<map<string, string= "" >> ();
string[] Menustr = new string[] {"Comprehensive sort", "Lowest distribution fee"};
Map<string, string= "" > map; for (int i =, Len = Menustr.length i < len; ++i) {map = new hashmap<string, string= "" > (); Map.put ("Name", menu
Str[i]);
Menudata.add (map);
} menudata = new arraylist<map<string, string= "" >> ();
string[] Menustr = new string[] {"Preferential activities", "special Events", "Free distribution Fees", "online Payment"};
Map<string, string= "" > map3; for (int i = 0, len = menustr3.length i < len; ++i) {map3 = new hashmap<string, string= "" > (); Map3.put ("Name",
Menustr3[i]);
Menudata3.add (MAP3); }}</string,></string,></map<string,></string,></string,></map<string,></ String,></string,></map<string,>

is to do a simple encapsulation. The implementation of the pop-up list considers the use of Popwindow.

Popmenu = new Popupwindow (Contentview,
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
Popmenu.setoutsidetouchable (true);
Popmenu.setbackgrounddrawable (New bitmapdrawable ());
Popmenu.setfocusable (true);
Popmenu.setanimationstyle (R.style.popwin_anim_style);
Popmenu.setondismisslistener (New Ondismisslistener () {public
void Ondismiss () {
Producttv.settextcolor ( Color.parsecolor ("#5a5959"));
Sorttv.settextcolor (Color.parsecolor ("#5a5959"));
Activitytv.settextcolor (Color.parsecolor ("#5a5959"));
}
);

The data is then encapsulated into the adapter:

MenuAdapter1 = new Simpleadapter (this, menuData1,
R.layout.item_listview_popwin, new string[] {"Name"},
new Int[] {R.ID.LISTVIEW_POPWIND_TV});
MenuAdapter2 = new Simpleadapter (this, menuData2,
R.layout.item_listview_popwin, new string[] {"Name"},
new Int[] {R.ID.LISTVIEW_POPWIND_TV});
MenuAdapter3 = new Simpleadapter (this, menuData3,
R.layout.item_listview_popwin, new string[] {"Name"},
new Int[] {R.ID.LISTVIEW_POPWIND_TV});

Set click on Title Header pop-up list and change the header color

public void OnClick (View v) {
//TODO auto-generated a stub
switch (V.getid ()) {case
r.id.supplier_list _product:
producttv.settextcolor (Color.parsecolor ("#ac"));
Poplistview.setadapter (menuadapter);
Popmenu.showasdropdown (product,,);
MenuIndex =;
break;
Case R.id.supplier_list_sort:
sorttv.settextcolor (Color.parsecolor ("#ac"));
Poplistview.setadapter (menuadapter);
Popmenu.showasdropdown (product,,);
MenuIndex =;
break;
Case r.id.supplier_list_activity:
activitytv.settextcolor (Color.parsecolor ("#ac"));
Poplistview.setadapter (menuadapter);
Popmenu.showasdropdown (product,,);
MenuIndex =;
break;
}
}

The Showasdropdown is designed to allow Popwindow to be positioned just below the product selection heading. So as to achieve the above way.

Finally the complete paste out the code, or quite simple. Finally, a code download link is provided.

public class Mainactivity extends activity implements Onclicklistener {private ListView ListView, Poplistview; private P
Rogressbar ProgressBar;
Private list<map<string, string= "" >> menuData1, MenuData2, menuData3;
Private Popupwindow Popmenu;
Private Simpleadapter MenuAdapter1, MenuAdapter2, MenuAdapter3;
Private LinearLayout product, sort, activity;
Private ImageView Cartiv;
Private TextView Producttv, Sorttv, Activitytv, Titletv;
private int green, grey;
Private String currentproduct, Currentsort, currentactivity;
private int menuIndex = 0;
Private Intent Intent; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_supplier_list);
Findview ();
Initmenudata ();
Initpopmenu ();
private void Initmenudata () {menuData1 = new arraylist<map<string, string= "" >> ();
string[] menuStr1 = new string[] {"All", "grain and oil", "clothes", "books", "Electronic products", "drinks", "fruit"};
Map<string, string= "" > Map1; for (int i = 0, len = menustr1.length; i < Len; ++i) {map1 = new hashmap<string, string= "" > (); Map1.put ("name", Menustr1[i); Menudata1.add (MAP1);} menuData2 = NE
W arraylist<map<string, string= "" >> ();
string[] menuStr2 = new string[] {"Comprehensive sort", "Lowest distribution fee"};
Map<string, string= "" > map2; for (int i = 0, len = menustr2.length i < len; ++i) {map2 = new hashmap<string, string= "" > (); Map2.put ("Name",
Menustr2[i]);
Menudata2.add (MAP2);
} menuData3 = new arraylist<map<string, string= "" >> ();
string[] MenuStr3 = new string[] {"Preferential activities", "special Events", "Free distribution Fees", "online Payment"};
Map<string, string= "" > map3; for (int i = 0, len = menustr3.length i < len; ++i) {map3 = new hashmap<string, string= "" > (); Map3.put ("Name",
Menustr3[i]);
Menudata3.add (MAP3); @Override public void OnClick (View v) {//TODO auto-generated Method Stub switch (V.getid ()) {case R.id.supplier_lis
T_product:productTv.setTextColor (Color.parsecolor ("#39ac69")); Poplistview.setadapter (MenuAdapter1);
Popmenu.showasdropdown (product, 0, 2);
MenuIndex = 0;
Break
Case R.id.supplier_list_sort:sorttv.settextcolor (Color.parsecolor ("#39ac69"));
Poplistview.setadapter (MENUADAPTER2);
Popmenu.showasdropdown (product, 0, 2);
MenuIndex = 1;
Break
Case R.id.supplier_list_activity:activitytv.settextcolor (Color.parsecolor ("#39ac69"));
Poplistview.setadapter (MENUADAPTER3);
Popmenu.showasdropdown (product, 0, 2);
MenuIndex = 2;
Break } protected void Findview () {ListView = (ListView) Findviewbyid (R.ID.SUPPLIER_LIST_LV); product = (LinearLayout) findvi
Ewbyid (r.id.supplier_list_product);
Sort = (linearlayout) Findviewbyid (R.id.supplier_list_sort);
Activity = (LinearLayout) Findviewbyid (r.id.supplier_list_activity);
Producttv = (TextView) Findviewbyid (R.ID.SUPPLIER_LIST_PRODUCT_TV);
Sorttv = (TextView) Findviewbyid (R.ID.SUPPLIER_LIST_SORT_TV);
Activitytv = (TextView) Findviewbyid (R.ID.SUPPLIER_LIST_ACTIVITY_TV);
Titletv = (TextView) Findviewbyid (R.ID.SUPPLIER_LIST_TITLE_TV); Cartiv = (ImageView) Findviewbyid (R.ID.SUPPLIER_LIST_CART_IV);
ProgressBar = (ProgressBar) Findviewbyid (r.id.progress);
Product.setonclicklistener (this);
Sort.setonclicklistener (this);
Activity.setonclicklistener (this);
Cartiv.setonclicklistener (this);
private void Initpopmenu () {initmenudata ();
View Contentview = View.inflate (this, r.layout.popwin_supplier_list, null); Popmenu = new Popupwindow (Contentview, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_
PARENT);
Popmenu.setoutsidetouchable (TRUE);
Popmenu.setbackgrounddrawable (New bitmapdrawable ());
Popmenu.setfocusable (TRUE);
Popmenu.setanimationstyle (R.style.popwin_anim_style); Popmenu.setondismisslistener (New Ondismisslistener () {public void Ondismiss () {Producttv.settextcolor (
Color.parsecolor ("#5a5959"));
Sorttv.settextcolor (Color.parsecolor ("#5a5959"));
Activitytv.settextcolor (Color.parsecolor ("#5a5959"));
}
});
Poplistview = (ListView) contentview. Findviewbyid (R.ID.POPWIN_SUPPLIER_LIST_LV); contentview.fiNdviewbyid (R.id.popwin_supplier_list_bottom). Setonclicklistener (New Onclicklistener () {public void OnClick (View
arg0) {Popmenu.dismiss ();}}); MenuAdapter1 = new Simpleadapter (this, menuData1, R.layout.item_listview_popwin, new string[] {"Name"}, new int[] {r.id
. Listview_popwind_tv}); MenuAdapter2 = new Simpleadapter (this, MenuData2, R.layout.item_listview_popwin, new string[] {"Name"}, new int[] {r.id
. Listview_popwind_tv}); MenuAdapter3 = new Simpleadapter (this, menuData3, R.layout.item_listview_popwin, new string[] {"Name"}, new int[] {r.id
. Listview_popwind_tv}); Poplistview.setonitemclicklistener (New Onitemclicklistener () {public void Onitemclick (Adapterview arg0, View arg1, int POS, long Arg3) {Popmenu.dismiss (); if (MenuIndex = 0) {currentproduct = Menudata1.get (POS). Get ("name"); Titletv.settex
T (currentproduct);
Producttv.settext (currentproduct);
Toast.maketext (Mainactivity.this, Currentproduct, Toast.length_short). Show (); else if (MenuIndex = 1) {CurrentsoRT = Menudata2.get (POS). Get ("name");
Titletv.settext (Currentsort);
Sorttv.settext (Currentsort);
Toast.maketext (Mainactivity.this, Currentsort, Toast.length_short). Show (); else {currentactivity = Menudata3.get (POS). Get ("name"); Titletv.settext (currentactivity); Activitytv.settext (
CurrentActivity);
Toast.maketext (Mainactivity.this, CurrentActivity, Toast.length_short). Show ();
}
}
}); }}</string,></string,></map<string,></string,></string,></map<string, ></string,></string,></map<string,></map<string,>

Second, the display of the loaded circular ProgressBar

Is the kind of load ProgressBar, the circular Progresbar can be implemented with the native bar, but the style is single, before I do this effect the first time always consider the frame animation, but in this way need to have a lot of pictures to link up, so as to achieve trouble, Secondly, the picture is more than the memory. This effect is achieved by changing the animation of the native ProgressBar, very simple:

<progressbar android:id= "@+id/progress" android:indeterminatedrawable= "@drawable/shape_progress" Android: indeterminateduration= "1000" android:layout_centerinparent= "true" android:layout_height= "Wrap_content" Android: Layout_width= "Wrap_content" ></progressbar>

Indeterminatedrawable is the load background picture, indeterminateduration is the speed of rotation. The idea here is to use XML to draw a picture that is circular and has a gradient in the ring. As follows:

<rotate android:fromdegrees= "android:pivotx="% "android:pivoty="% "android:todegrees=" "xmlns:android=" http:// Schemas.android.com/apk/res/android ">
<shape android:innerradiusratio=" "Android:shape=" Ring "Android: thicknessratio= "" Android:uselevel= "false" >
<gradient android:centercolor= "#cdaa" android:centery= "." Android:endcolor= "#ffffff" android:startcolor= "#ac" android:type= "Sweep" android:uselevel= "false" >
</ Gradient></shape>
</rotate>

Rotate set rotation animation, 360 degrees rotation. Shape= "Ring" sets the background to a circle. android:innerradiusratio= "3" Set the inner ring radius, android:thicknessratio= "10" Set the outer ring radius. Finally, in order to make the colors in the ring have a gradient effect, use gradient to set. Gradient can have three different gradient modes, linear, radiant, and scanned. Here type is set to scan. Then set the center point, start color and end color, you can achieve the above effect.

The above content is small series to introduce the Android imitation American Group pull-down menu (commodity purchase) instance code, hope to help everyone!

Related Article

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.