ListPopupWindow使用完整示例(二)——自定义ListPopupWindow

MainActivity如下:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cc.wy;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.app.Activity;  
  5. import android.app.ActionBar.LayoutParams;  
  6. import android.content.Context;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.AdapterView;  
  11. import android.widget.Button;  
  12. import android.widget.ListPopupWindow;  
  13. import android.widget.Toast;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15. /** 
  16.  * Demo描述: 
  17.  * ListPopupWindow使用完整示例(二)——自定义ListPopupWindow 
  18.  *  
  19.  * 参考资料: 
  20.  * 1 http://blog.csdn.net/rambomatrix/article/details/23525379 
  21.  * 2 http://blog.csdn.net/jsnrwzm/article/details/14408835 
  22.  *   Thank you very much 
  23.  *    
  24.  */  
  25. public class MainActivity extends Activity {  
  26.     private Context mContext;  
  27.     private Button mButton;  
  28.     private ArrayList<String> mArrayList;  
  29.     private ListPopupWindow mListPopupWindow;  
  30.     private ListPopupWindowAdapter mListPopupWindowAdapter;  
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.         init();  
  36.     }  
  37.       
  38.     private void init(){  
  39.         mContext=this;  
  40.         mArrayList=new ArrayList<String>();  
  41.         mArrayList.add("第一个子项");  
  42.         mArrayList.add("第二个子项");  
  43.         mArrayList.add("第三个子项");  
  44.         mListPopupWindow=new ListPopupWindow(mContext);  
  45.         //自定义Adapter  
  46.         mListPopupWindowAdapter=new ListPopupWindowAdapter(mArrayList, mContext);  
  47.         mListPopupWindow.setAdapter(mListPopupWindowAdapter);  
  48.         //mListPopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));  
  49.         mListPopupWindow.setWidth(200);  
  50.         mListPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);  
  51.         mListPopupWindow.setOnItemClickListener(new OnItemClickListener() {  
  52.             @Override  
  53.             public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {  
  54.                 Toast.makeText(mContext, "点击了"+mArrayList.get(position), Toast.LENGTH_SHORT).show();  
  55.             }  
  56.         });  
  57.         mButton=(Button) findViewById(R.id.button);  
  58.         mButton.setOnClickListener(new OnClickListener() {  
  59.             @Override  
  60.             public void onClick(View v) {  
  61.                 //指定anchor  
  62.                 mListPopupWindow.setAnchorView(v);  
  63.                 mListPopupWindow.show();  
  64.             }  
  65.         });  
  66.     }  
  67. }  


ListPopupWindowAdapter如下:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package cc.wy;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.TextView;  
  11.   
  12. public class ListPopupWindowAdapter extends BaseAdapter {  
  13.     private ArrayList<String> mArrayList;  
  14.     private Context mContext;  
  15.     public ListPopupWindowAdapter(ArrayList<String> list, Context context) {  
  16.         super();  
  17.         this.mArrayList = list;  
  18.         this.mContext = context;  
  19.     }  
  20.   
  21.     @Override  
  22.     public int getCount() {  
  23.         if (mArrayList == null) {  
  24.             return 0;  
  25.         } else {  
  26.             return this.mArrayList.size();  
  27.         }  
  28.     }  
  29.   
  30.     @Override  
  31.     public Object getItem(int position) {  
  32.         if (mArrayList == null) {  
  33.             return null;  
  34.         } else {  
  35.             return this.mArrayList.get(position);  
  36.         }  
  37.     }  
  38.   
  39.     @Override  
  40.     public long getItemId(int position) {  
  41.         return position;  
  42.     }  
  43.   
  44.     @Override  
  45.     public View getView(final int position, View convertView, ViewGroup parent) {  
  46.         ViewHolder holder = null;  
  47.         if (convertView == null) {  
  48.             holder = new ViewHolder();            
  49.             convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null, false);          
  50.             holder.itemTextView = (TextView) convertView.findViewById(R.id.itemTextView);  
  51.             convertView.setTag(holder);  
  52.         } else {  
  53.             holder = (ViewHolder) convertView.getTag();  
  54.         }  
  55.           
  56.         if (this.mArrayList != null) {  
  57.             final String itemName = this.mArrayList.get(position);  
  58.             if (holder.itemTextView != null) {  
  59.                 holder.itemTextView.setText(itemName);  
  60.             }  
  61.         }  
  62.   
  63.         return convertView;  
  64.   
  65.     }  
  66.   
  67.     private class ViewHolder {  
  68.         TextView itemTextView;  
  69.     }  
  70.   
  71. }  


main.xml如下:

 

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <Button  
  7.         android:id="@+id/button"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerInParent="true"  
  11.         android:text="点击弹出自定义ListPopupWindow" />  
  12.   
  13. </RelativeLayout>  


item.xml如下:

 

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     android:layout_width="fill_parent"  
    4.     android:layout_height="fill_parent">  
    5.   
    6.     <TextView  
    7.         android:id="@+id/itemTextView"  
    8.         android:layout_width="fill_parent"  
    9.         android:layout_height="wrap_content"  
    10.          />  
    11.   
    12. </RelativeLayout>  
posted @ 2016-12-01 22:00  天涯海角路  阅读(723)  评论(0)    收藏  举报