Android Popwindow下面的ListView的item点击事件怎么响应

 我来答
理腐樱莲激0Z
2015-03-25 · 超过28用户采纳过TA的回答
知道答主
回答量:63
采纳率:0%
帮助的人:34.9万
展开全部
package com.example.myselfpopupdemo;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity {
private ListView list;
private ImageButton popButton;
private String[] popInfo = new String[] { "测试1", "测试2 ", "测试3", "测试4" };
private PopupWindow pWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
LayoutInflater inflater = getLayoutInflater();
View v = inflater.inflate(R.layout.list, null);
pWindow = new PopupWindow(v, 120,LayoutParams.WRAP_CONTENT);
pWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pWindow.setOutsideTouchable(true);
pWindow.setFocusable(true);
pWindow.update();
this.list = (ListView) v.findViewById(R.id.list);
this.popButton = (ImageButton) findViewById(R.id.btn_title_popmenu);
this.popButton.setOnClickListener(new popOnClickListenerImpl());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this, R.layout.pomenu_item, popInfo);
this.list.setAdapter(adapter);
this.list.setOnItemClickListener(new OnItemClickListenerImpl());
}
private class popOnClickListenerImpl implements OnClickListener{

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
pWindow.showAsDropDown(v);
}
}
private class OnItemClickListenerImpl implements OnItemClickListener{

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
pWindow.dismiss();
Toast.makeText(MainActivity.this, ""+popInfo[position], 2000).show();
}

}
}
通过 inflater.inflate找到ListView所在的父组件,再list = (ListView) v.findViewById(R.id.list);通过这个找到ListView,之后就可以对ListView添加点击事件!
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式