這篇文章將為大家詳細講解有關(guān)Android中怎么利用popupwindow顯示listview,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
10余年的長洲網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整長洲建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“長洲網(wǎng)站設(shè)計”,“長洲網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
1、創(chuàng)建一個popupwindow,并設(shè)置相應(yīng)的樣式。
Java代碼
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)); //初始化按鈕 submit = (Button) v.findViewById(R.id.submit); submit.setOnClickListener(submitListener); cancel = (Button) v.findViewById(R.id.cancel); cancel.setOnClickListener(cancelListener); //初始化listview,加載數(shù)據(jù)。 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, 500,260); } //設(shè)置整個popupwindow的樣式。 window.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop)); //使窗口里面的空間顯示其相應(yīng)的效果,比較點擊button時背景顏色改變。 //如果為false點擊相關(guān)的空間表面上沒有反應(yīng),但事件是可以監(jiān)聽到的。 //listview的話就沒有了作用。 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(); // 在每次獲取點擊的item時將對于的checkbox狀態(tài)改變,同時修改map的值。 vHollder.cBox.toggle(); MyAdapter.isSelected.put(position, vHollder.cBox.isChecked()); } };
給按鈕添加監(jiān)聽事件:
Java代碼
OnClickListener submitListener = new OnClickListener() { @Override public void onClick(View v) { //這兒可以寫提交數(shù)據(jù)的代碼。 closeWindow(); } }; OnClickListener cancelListener=new OnClickListener(){ @Override public void onClick(View v){ closeWindow(); } }; private void closeWindow(){ //將每個checkbox的標(biāo)記改為false,以便下次彈出window時是初始的狀態(tài)。 for (int j = 0; j < MyAdapter.isSelected.size(); j++) { MyAdapter.isSelected.put(j, false); ViewHolder vHollder = (ViewHolder) list.getChildAt(j).getTag(); vHollder.cBox.setChecked(false); } //提交數(shù)據(jù)時關(guān)閉popupwindow。 if (window != null) { window.dismiss(); } }
在layout中新建popupwindow.xml文件,具體內(nèi)容如下,主要是對window的布局:
Java代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/tip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="18dip" android:background="@drawable/rounded_corners_list" android:text="這是一個popupWindow的例子"/> <!-- 如果layout_width的值為fill_parent時,居中要用android:gravity="center"--> <ListView android:id="@+id/lv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/rounded_corners_list"/> <LinearLayout android:orientation="horizontal" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_corners_list"> <Button android:id="@+id/submit" android:layout_width="100dip" android:layout_height="50dip" android:text="提交"/> <Button android:id="@+id/cancel" android:layout_width="100dip" android:layout_height="50dip" android:text="取消"/> </LinearLayout> </LinearLayout>
新建rounded_corners_pop.xml,用于自定義窗口的樣式文件,具體內(nèi)容如下:
Java代碼
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffffff" /> <stroke android:width="3dp" color="#ffff8080" /> <corners android:radius="10dp" /> <padding android:left="3dp" android:top="3dp" android:right="3dp" android:bottom="3dp" /> </shape>
這個就可以實現(xiàn)圓角的樣式,周圍的白邊是通過在白的樣式上面疊加黑色的來實現(xiàn)的。 其他樣式文件大家可以參考上面的rounded_corners_pop.xml自己寫。
2、在main.xml中添加按鈕,一個用于顯示window,一個用于隱藏window
Java代碼
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lmain" > <Button android:id="@+id/myButton1" android:layout_width="100dip" android:layout_height="50dip" android:text="顯示"/> <Button android:id="@+id/myButton2" android:layout_width="100dip" android:layout_height="50dip" android:text="隱藏"/> </LinearLayout>
在activity中初始化這兩個按鈕,并添加監(jiān)聽事件:
Java代碼
OnClickListener bPop = new OnClickListener() { @Override public void onClick(View v) { popAwindow(v); } }; OnClickListener boff = new OnClickListener() { @Override public void onClick(View v) { if(window!=null){ window.dismiss(); } } };
關(guān)于Android中怎么利用popupwindow顯示listview就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
新聞名稱:Android中怎么利用popupwindow顯示listview
文章網(wǎng)址:http://www.rwnh.cn/article36/psgssg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站設(shè)計、用戶體驗、網(wǎng)站導(dǎo)航、建站公司、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)