阅读背景:

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

来源:互联网 

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 https://blog.csdn.net/rambomatrix/article/details/23525379
 * 2 https://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();
			}
		});
    }
}package cc.wy;

import jav



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: