1. 程式人生 > >Android PopupWindow巢狀ListView(模仿分類下拉選單效果)

Android PopupWindow巢狀ListView(模仿分類下拉選單效果)

1:佈局檔案

[java] view plaincopyprint?
  1. <span style="font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.  >  
  6.        <TextView   
  7.                 android:id="@+id/tv_text_age"
  8.                 android:layout_width="wrap_content"
  9.                 android:layout_height="wrap_content"
  10.                 android:background="@drawable/choolse_bg"
  11.                 android:text="不限"
  12.                 android:paddingLeft="12dp"
  13.                 android:textColor="#9d72d2"
  14.                 android:layout_centerInParent="true"
  15.                 android:gravity="center_vertical"
  16.                 />  
  17. </RelativeLayout></span>  
[html] view plaincopyprint?
  1. package com.jackie.popupwindow;  
  2. import java.util.ArrayList;  
  3. import java.util.List;  
  4. import android.app.Activity;  
  5. import android.graphics.Color;  
  6. import android.graphics.drawable.ColorDrawable;  
  7. import android.os.Bundle;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.view.ViewGroup;  
  12. import android.view.ViewGroup.LayoutParams;  
  13. import android.widget.AdapterView;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. import android.widget.BaseAdapter;  
  16. import android.widget.ListView;  
  17. import android.widget.PopupWindow;  
  18. import android.widget.TextView;  
  19. public class MainActivity extends Activity implements OnClickListener {  
  20.     private TextView tv_text_age;  
  21.     private PopupWindow pop;  
  22.     ListView listView = null;  
  23.     private List<String> datas;  
  24.     private SelectAgeAdapter mSelectAdapter;  
  25.     private LayoutInflater inflater;  
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.         initData();  
  31.         inflater = LayoutInflater.from(this);  
  32.         tv_text_age = (TextView) findViewById(R.id.tv_text_age);  
  33.         tv_text_age.setOnClickListener(this);  
  34.     }  
  35.     private void initData() {  
  36.         datas = new ArrayList<String>();  
  37.         for(int 0; i 50; i++){  
  38.             datas.add("HelloWorld - " + i);  
  39.         }  
  40.     }  
  41.     @Override  
  42.     public void onClick(View v) {  
  43.         if(v.getId() == R.id.tv_text_age){  
  44.             if(pop == null) {  
  45.                 listView = new ListView(this);  
  46.                 listView.setDividerHeight(1);  
  47.                 listView.setBackgroundResource(R.drawable.kge_feek_bg);  
  48.                 listView.setCacheColorHint(0x00000000);  
  49.                 mSelectAdapter = new SelectAgeAdapter();  
  50.                 pop = new PopupWindow(listView, tv_text_age.getWidth(), LayoutParams.WRAP_CONTENT, true);  
  51.             }  
  52.             listView.setAdapter(mSelectAdapter);  
  53.             pop.setBackgroundDrawable(new ColorDrawable(Color.RED));
  54.             pop.showAsDropDown(tv_text_age, 0, 0);  
  55.             listView.setOnItemClickListener(new OnItemClickListener() {  
  56.                 @Override  
  57.                 public void onItemClick(AdapterView<?> parent, View view,  
  58.                         int position, long id) {  
  59.                     tv_text_age.setText(datas.get(position));  
  60.                     dismissPopWindow();  
  61.                 }  
  62.             });  
  63.         }  
  64.     }  
  65.     public void dismissPopWindow() {  
  66.         if(pop != null && pop.isShowing()){  
  67.             pop.dismiss();  
  68.         }  
  69.     }  
  70.     class SelectAgeAdapter extends BaseAdapter {  
  71.         @Override  
  72.         public int getCount() {  
  73.             return datas != null&&datas.size() 0 ? datas.size() : 0;  
  74.         }  
  75.         @Override  
  76.         public Object getItem(int position) {  
  77.             return datas.get(position);  
  78.         }  
  79.         @Override  
  80.         public long getItemId(int position) {  
  81.             return position;  
  82.         }  
  83.         @Override  
  84.         public View getView(int position, View convertView, ViewGroup parent) {  
  85.             ViewHolder holder;  
  86.             if (convertView == null) {  
  87.                 holder = new ViewHolder();  
  88.                 convertView = inflater.inflate(R.layout.item, null);  
  89.                 holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);  
  90.                 convertView.setTag(holder);  
  91.             } else {  
  92.                 holder = (ViewHolder) convertView.getTag();  
  93.             }  
  94.             holder.tv_name.setText(datas.get(position));  
  95.             return convertView;  
  96.         }  
  97.     }  
  98.     class ViewHolder {  
  99.         TextView tv_name;  
  100.     }  
  101. }