当前位置: 代码迷 >> Android >> Android PopupWindow嵌套ListView(仿照分类下拉菜单效果)
  详细解决方案

Android PopupWindow嵌套ListView(仿照分类下拉菜单效果)

热度:61   发布时间:2016-04-28 02:05:57.0
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.   
  7.        <TextView   
  8.                 android:id="@+id/tv_text_age"  
  9.                 android:layout_width="wrap_content"  
  10.                 android:layout_height="wrap_content"  
  11.                 android:background="@drawable/choolse_bg"  
  12.                 android:text="不限"  
  13.                 android:paddingLeft="12dp"  
  14.                 android:textColor="#9d72d2"  
  15.                 android:layout_centerInParent="true"  
  16.                 android:gravity="center_vertical"  
  17.                 />  
  18. </RelativeLayout></span>  

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