Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use popupwindow to display listview in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use popupwindow to display listview in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use popupwindow to display listview in Android".

1. Create a popupwindow and set the corresponding style.

Java code

Private void popAwindow (View parent) {if (window = = null) {LayoutInflater lay = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE); View v = lay.inflate (R.layout.popupwindow, null); v.setBackgroundDrawable (getResources (). GetDrawable (R.drawable.rounded_corners_view)) / / initialize button submit = (Button) v.findViewById (R.id.submit); submit.setOnClickListener (submitListener); cancel = (Button) v.findViewById (R.id.cancel); cancel.setOnClickListener (cancelListener); / / initialize listview to load data. List= (ListView) v.findViewById (R.id.lv); MyAdapter adapter=new MyAdapter (Main.this); list.setAdapter (adapter); list.setItemsCanFocus (false); list.setChoiceMode (ListView.CHOICE_MODE_MULTIPLE); list.setOnItemClickListener (listClickListener); window = new PopupWindow (v, 500260) } / / style the entire popupwindow. Window.setBackgroundDrawable (getResources (). GetDrawable (R.drawable.rounded_corners_pop)); / / make the space in the window show its corresponding effect, and compare the background color change when you click button. / / if there is no response on the surface of the space related to clicking for false, but the event can be monitored. / / listview will have no effect. Window.setFocusable (true); window.update (); window.showAtLocation (parent, Gravity.CENTER_VERTICAL, 0,0);} OnItemClickListener listClickListener = new OnItemClickListener () {@ Override public void onItemClick (AdapterView parent, View view, int position, long id) {ViewHolder vHollder = (ViewHolder) view.getTag () / / each time you get the item of a click, the status of the checkbox will be changed, and the value of map will be modified. VHollder.cBox.toggle (); MyAdapter.isSelected.put (position, vHollder.cBox.isChecked ();}}

Add a listening event to the button:

Java code

OnClickListener submitListener = new OnClickListener () {@ Override public void onClick (View v) {/ / you can write the code to submit the data here. CloseWindow ();}}; OnClickListener cancelListener=new OnClickListener () {@ Override public void onClick (View v) {closeWindow ();}}; private void closeWindow () {/ / change the tag for each checkbox to false so that the next time the window pops up, it will be in the initial state. For (int j = 0; j < MyAdapter.isSelected.size (); jacks +) {MyAdapter.isSelected.put (j, false); ViewHolder vHollder = (ViewHolder) list.getChildAt (j). GetTag (); vHollder.cBox.setChecked (false);} / / turn off popupwindow when submitting data. If (window! = null) {window.dismiss ();}}

Create a new popupwindow.xml file in layout. The details are as follows, mainly about the layout of window:

Java code

Create a new rounded_corners_pop.xml to customize the style file of the window. The details are as follows:

Java code

This can achieve the fillet style, and the surrounding white edges are achieved by overlaying black on top of the white style. For other style files, you can refer to the above rounded_corners_pop.xml to write by yourself.

2. Add buttons to main.xml, one to show window and one to hide window

Java code

Initialize these two buttons in activity and add listening events:

Java code

OnClickListener bPop = new OnClickListener () {@ Override public void onClick (View v) {popAwindow (v);}}; OnClickListener boff = new OnClickListener () {@ Override public void onClick (View v) {if (windowcake null) {window.dismiss ();} Thank you for your reading, the above is "how to use popupwindow to display listview in Android", after the study of this article, I believe you have a deeper understanding of how to use popupwindow to display listview in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report