Android Popupwindow Use the region, schools choose two-level linkage

Source: Internet
Author: User

Recently in a social app, I want users to choose their own school according to the region when registering, because the user entered the school manually, there may be a variety of problems, not conducive to the subsequent statistics of user information. Then decided to set up the client, the user as long as the region to choose the best. The first idea is to use the Popupwindow, with the frame of the way to let users choose. Let the implementation have the following effects:

Here's how it's implemented (data is obtained from the network, JSON parsing uses Gson, network library is volley)

Engineering Structure:

1, create a layout file: View_select_province_list.xml, mainly includes a textview (used to display the title) and two ListView (Default display Area ListView, Hidden Schoollistview)

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:orientation= "Vertical"><TextViewAndroid:id= "@+id/list_title"Android:layout_width= "Match_parent"Android:layout_height= "50DP"Android:background= "#3b3b3b"Android:gravity= "Center"Android:text= "Select Region"Android:textcolor= "#ffffff"Android:textsize= "16SP"/><ListviewAndroid:id= "@+id/province"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_gravity= "Center"Android:background= "#e4e4e4"Android:divider= "#aeaeae"Android:dividerheight= "1DP"></Listview><ListviewAndroid:id= "@+id/school" Android:visibility=" Gone "  Android:layout_width=" match_parent " Android:layout_height= "wrap_content"  Android: Layout_gravity= "center"  Android:background= "#e4e4e4"  Android:divider=" #aeaeae " Android:dividerheight= "1DP" ></listview></ Linearlayout>          

2. Initialize Popupwindow

PrivatevoidInitpopview () {parent =This. GetWindow (). Getdecorview (); View Popview = View.inflate (This, r.layout.view_select_province_list,Null); Mtitle =(TextView) Popview.findviewbyid (r.id.list_title); Mprovincelistview =(ListView) Popview.findviewbyid (r.id.province); Mschoollistview =(ListView) Popview.findviewbyid (R.id.school); Mprovincelistview.setonitemclicklistener (ItemListener); Mschoollistview.setonitemclicklistener (ItemListener); Mprovinceadapter =New Provinceadapter (This); Mprovincelistview.setadapter (Mprovinceadapter); Mschooladapter =New Schooladapter (This); Mschoollistview.setadapter (Mschooladapter);int width = getresources (). Getdisplaymetrics (). Widthpixels * 3/4; int height = getresources (). Getdisplaymetrics (). Heightpixels * 3/5; mpopwindow = new Popupwindow (Popvie W, width, height); Colordrawable DW = new colordrawable (0x30000000); mpopwindow.setbackgrounddrawable (DW); Mpopwindow.setfocusable (true); Mpopwindow.settouchable (true); Mpopwindow.setoutsidetouchable ( true); // allow external clicks to cancel loadprovince (); Mpopwindow.setondismisslistener (listener);}     

Where you want to make Popupwindow click on an empty area to cancel, you must set

Colordrawable DW = new colordrawable (0x30000000);

Mpopwindow.setbackgrounddrawable (DW);

Mpopwindow.setoutsidetouchable (true);

3. Display Popupwindow


{mpopwindow.showatlocation (parent, gravity.center, 0, 0);}

4. Download regional and school data (volley and Gson parsing data are used here)

PrivatevoidLoadprovince () {mrequestqueue = Volley.newrequestqueue (This); gsonrequest<province> request =New gsonrequest<province>(Request.Method.POST, Config.province_url, province.ClassNew response.listener<province>() {@OverridePublicvoidOnresponse (province response) {if (Response.getdata ()! =Null && response.geterror_code () = = 0{mprovinceadapter.setlist (Response.getdata ()); mprovinceadapter.notifydatasetchanged ();}} },NewResponse.errorlistener () {@OverridePublicvoidOnerrorresponse (Volleyerror volleyerror) {}},This); Mrequestqueue.add (Request); }PrivatevoidLoadschool () {mrequestqueue = Volley.newrequestqueue (This); gsonrequest<school> request =New Gsonrequest<> (Request.Method.POST, Config.school_url + Provinceid, SCHOOL.Classnew Response.listener<school> () {@Override public void  Onresponse (School response) {if (Response.getdata ()! = null && response.geterror_code () = = 0new Response.errorlistener () {@Override public void Onerrorresponse (Volleyerror volleyerror) {}}, this  

5. Set the item click event for the ListView

/*** ListView Item Click event*/Adapterview.onitemclicklistener ItemListener =NewAdapterview.onitemclicklistener () {@OverridePublicvoid Onitemclick (adapterview<?> parent, view view,int position, long ID) { if (Parent == Mprovincelistview) {Provincelist Provincename = (Provincelist) mprovincelistview.getitematposition (position); Provinceid = provincename.getprovince_id (); Mtitle.settext ("Select School" ); Mprovincelistview.setvisibility (View.gone);//Hide Area mschoollistview.setvisibility (view.visible);//Show School LoadSchool ( ); } else if (Parent == (Schoollist) Mschoollistview.getitematposition (position); Mselectschool.settext (Schoolname.getschool_name ()); Mpopwindow.dismiss (); } } };

6, Popupwindow set Ondismisslistener, the purpose is to re-enter the choice of time, directly into the school rather than the choice of the region

/**     * Popwindow disappear Listener event */         newvoid Ondismiss () {mtitle.settext ("Select Region"); Mprovincelistview.setvisibility (view.visible);//Display Area mschooladapter.setlist (new arraylist<schoollist> ());//Set an empty list, to avoid the re-selection of schools after the pop-up or just the region corresponding to the school mschooladapter.notifydatasetchanged (); Mschoollistview.setvisibility (View.gone);//Hide School}};        

7, good, such a popupwindow based on the two-level linkage bullet box selection is complete, among them, there is a ListView adapter here I did not post it, the wording and our usual adapter. I've got the code open to my GitHub and there's a need to download it.

GitHub Address: Https://github.com/tonycheng93/PopWindow

Android Popupwindow Use the region, schools choose two-level linkage

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.