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

时间:2022-03-20 20:49:03

MainActivity如下:

package cc.wy;

import java.util.ArrayList;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListPopupWindow;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/**
 * Demo描述:
 * ListPopupWindow使用完整示例(二)——自定义ListPopupWindow
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/rambomatrix/article/details/23525379
 * 2 http://blog.csdn.net/jsnrwzm/article/details/14408835
 *   Thank you very much
 *   
 */
public class MainActivity extends Activity {
	private Context mContext;
	private Button mButton;
	private ArrayList<String> mArrayList;
	private ListPopupWindow mListPopupWindow;
	private ListPopupWindowAdapter mListPopupWindowAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    
    private void init(){
    	mContext=this;
    	mArrayList=new ArrayList<String>();
    	mArrayList.add("第一个子项");
    	mArrayList.add("第二个子项");
    	mArrayList.add("第三个子项");
    	mListPopupWindow=new ListPopupWindow(mContext);
    	//自定义Adapter
    	mListPopupWindowAdapter=new ListPopupWindowAdapter(mArrayList, mContext);
    	mListPopupWindow.setAdapter(mListPopupWindowAdapter);
    	//mListPopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_launcher));
    	mListPopupWindow.setWidth(200);
    	mListPopupWindow.setHeight(LayoutParams.WRAP_CONTENT);
    	mListPopupWindow.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
				Toast.makeText(mContext, "点击了"+mArrayList.get(position), Toast.LENGTH_SHORT).show();
			}
		});
    	mButton=(Button) findViewById(R.id.button);
    	mButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//指定anchor
				mListPopupWindow.setAnchorView(v);
				mListPopupWindow.show();
			}
		});
    }
}

ListPopupWindowAdapter如下:
package cc.wy;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListPopupWindowAdapter extends BaseAdapter {
	private ArrayList<String> mArrayList;
	private Context mContext;
	public ListPopupWindowAdapter(ArrayList<String> list, Context context) {
		super();
		this.mArrayList = list;
		this.mContext = context;
	}

	@Override
	public int getCount() {
		if (mArrayList == null) {
			return 0;
		} else {
			return this.mArrayList.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (mArrayList == null) {
			return null;
		} else {
			return this.mArrayList.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();			
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null, false);		
			holder.itemTextView = (TextView) convertView.findViewById(R.id.itemTextView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (this.mArrayList != null) {
			final String itemName = this.mArrayList.get(position);
			if (holder.itemTextView != null) {
				holder.itemTextView.setText(itemName);
			}
		}

		return convertView;

	}

	private class ViewHolder {
		TextView itemTextView;
	}

}

main.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="点击弹出自定义ListPopupWindow" />

</RelativeLayout>

item.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/itemTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

</RelativeLayout>