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

简介: MainActivity如下: package cc.wy;import java.util.ArrayList;import android.

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>


相关文章
|
7月前
|
网络性能优化 调度 网络虚拟化
配置HQoS示例
HQoS简介 HQoS通过多级队列进一步细化区分业务流量,对多个用户、多种业务等传输对象进行统一管理和分层调度,在现有的硬件环境下使设备具备内部资源的控制策略,既能够为高级用户提供质量保证,又能够从整体上节约网络建设成本。 交换机的HQoS主要通过流队列和用户队列实现。
127 7
|
4月前
|
搜索推荐
示例
【8月更文挑战第27天】示例。
34 2
|
7月前
|
传感器 数据处理
示例三、光照度测试仪
示例三、光照度测试仪
64 1
组件配置示例
组件配置示例
57 3
|
关系型数据库 Java 数据库连接
SpringMyBatis解析1-使用示例
MyBatis使用介绍   MyBatis的详细使用介绍  http://www.cnblogs.com/xrq730/category/796495.html   建立PO  public class Person { private String id; private String name; //set get 方法、、、 } 建立Mapper mapper是数据库操作的映射文件,也就是我们常说的dao文件。
971 0
|
XML Java 数据格式
SpringRMI解析1-使用示例
Java远程方法调用,即JavaRMI(Java Remote Method Invocation),是Java编程语言里一种用于实现远程过程调用的应用程序编程接口。它使客户机上的运行的程序可以调用远程服务器上的对象。
785 0

相关实验场景

更多