Android Popwindow下面的ListView的item点击事件怎么响应
展开全部
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添加点击事件!
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添加点击事件!
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询