Android PopupWindow uses region and school selection level-2 Association, androidpopupwindow

Source: Internet
Author: User

Android PopupWindow uses region and school selection level-2 Association, androidpopupwindow

Recently, when I was working on a social APP, I wanted users to select their school based on their region during registration. Because users manually enter the school, various problems may occur, this is not conducive to subsequent statistics on user information. So I decided to set it on the client. Users only need to select the region. The first idea is to use PopupWindow and use a pop-up box for users to choose. The implementation result is as follows:

The following describes how to implement it (data is obtained from the network, Gson is used for JSON parsing, and Volley is used for the network library)

Project Structure:

1. Create a layout file: view_select_province_list.xml, which mainly includes a TextView (used to display the title) and two ListView (display area ListView by default, hide SchoolListView)

<? 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"> <TextView android: 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"/> <ListView android: 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> <ListView android: 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

Private void initPopView () {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 (popView, width, height); ColorDrawable dw = new ColorDrawable (0x30000000); mPopWindow. setBackgroundDrawable (dw); mPopWindow. setFocusable (true); mPopWindow. setTouchable (true); mPopWindow. setOutsideTouchable (true); // allow canceling loadProvince (); mPopWindow on the outside. setOnDismissListener (listener );}

To cancel PopupWindow by clicking a blank area, you must set

ColorDrawable dw = new ColorDrawable (0x30000000 );

MPopWindow. setBackgroundDrawable (dw );

MPopWindow. setOutsideTouchable (true );

3. Display PopupWindow

private void showPopWindow() 
{ mPopWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);}

4. Download the region and school data (volley and gson are used to parse the data here)

private void loadProvince() {        mRequestQueue = Volley.newRequestQueue(this);        GsonRequest<Province> request = new GsonRequest<Province>(Request.Method.POST, Config.PROVINCE_URL,                Province.class, new Response.Listener<Province>() {            @Override            public void onResponse(Province response) {                if (response.getData() != null && response.getError_code() == 0) {                    mProvinceAdapter.setList(response.getData());                    mProvinceAdapter.notifyDataSetChanged();                }            }        }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {            }        }, this);        mRequestQueue.add(request);    }    private void loadSchool() {        mRequestQueue = Volley.newRequestQueue(this);        GsonRequest<School> request = new GsonRequest<>(Request.Method.POST, Config.SCHOOL_URL + provinceId, School.class,                new Response.Listener<School>() {                    @Override                    public void onResponse(School response) {                        if (response.getData() != null && response.getError_code() == 0){                            mSchoolAdapter.setList(response.getData());                            mSchoolAdapter.notifyDataSetChanged();                        }                    }                }, new Response.ErrorListener() {            @Override            public void onErrorResponse(VolleyError volleyError) {            }        }, this);        mRequestQueue.add(request);    }

5. Set the ListView Item Click Event

/*** ListView Item Click Event */AdapterView. OnItemClickListener itemListener = new AdapterView. OnItemClickListener () {@ Override public void 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 the region mSchoolListView. setVisibility (View. VISIBLE); // display the school loadSchool ();} else if (parent = mSchoolListView) {SchoolList schoolName = (SchoolList) mSchoolListView. getItemAtPosition (position); mSelectSchool. setText (schoolName. getSchool_name (); mPopWindow. dismiss ();}}};

6. Set the OnDismissListener in PopupWindow to directly access the school rather than the region when you try again.

/*** Listening event for the disappearance of popWindow */PopupWindow. onDismissListener listener = new PopupWindow. onDismissListener () {@ Override public void onDismiss () {mTitle. setText ("select region"); mProvinceListView. setVisibility (View. VISIBLE); // display the region mSchoolAdapter. setList (new ArrayList <SchoolList> (); // you can set an empty List to avoid selecting the corresponding school mSchoolAdapter in the region. notifyDataSetChanged (); mSchoolListView. setVisibility (View. GONE); // hide the school }};

7. Well, the selection of a PopupWindow-based secondary linkage pop-up box is complete, and the ListView Adapter is not pasted here, the write method is the same as the adapter we usually use. I have open-source code on my GitHub. If you need it, download it.

GitHub address: https://github.com/tonycheng93/PopWindow

 

 

 

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.