我真的碰到這個問題.. 我有一個列表視圖,其中包含一個按鈕,當點擊該按鈕時,它應該顯示一個彈出。我已經實現了列表視圖,但彈出菜單沒有顯示在點擊的按鈕位置..下面列表視圖與按鈕和彈出
public View getView(int position, View convertView, final ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
vi.findViewById(R.id.statusImage).setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x=(int) event.getX();
y=(int) event.getY();
showPopup(x,y);// calls popup
return false;
public void showPopup(int xk , int ys) {
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout,null);
pwindo = new PopupWindow(layout, 300, 250, true);
pwindo.showAtLocation(layout, Gravity.NO_GRAVITY, xk,ys);
pwindo.setOutsideTouchable(true);
pwindo.setTouchable(true);
pwindo.setBackgroundDrawable(new BitmapDrawable());
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
pwindo.dismiss();
return true;
}
});
}
public class MyAdapter extends BaseAdapter{
private Activity activity;
private PopupWindow pwindo;
Point p;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
String value="0";
ImageButton imgBn ;
int x,y;
public MyAdapter (Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
/*
public boolean onTouchEvent(MotionEvent event) {
// MotionEvent object holds X-Y values
if(event.getAction() == MotionEvent.ACTION_DOWN) {
x=(int) event.getX();
y=(int) event.getY();
}
return true;
}*/
public Object getItem(int position) {
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
return (song.get(JobsListing.KEY_ID));
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, final ViewGroup parent) {
View vi=convertView;
if(convertView==null)
/*
vi = inflater.inflate(R.layout.list_row, null);
View imgView = vi.findViewById(R.id.statusImage);
imgView.setTag(position);
*/
vi = inflater.inflate(R.layout.list_row, null);
ImageView button = (ImageView) vi.findViewById(R.id.statusImage);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
button.setTag(position);
button.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int y;
if(v.getTag() == null) {
y=100;
} else {
int position = (Integer) v.getTag();
y = (1+position)*v.getHeight();
}
int x= (int) v.getRight();
showPopup(x,y);// calls popup
return false;
}
});
/*
vi.findViewById(R.id.statusImage).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//int OFFSET_X = 30;
// int OFFSET_Y = 30;
showPopup();
//LayoutInflater inflater = (LayoutInflater) vi.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// View layout = inflater.inflate(R.layout.popup_layout,null);
//pwindo = new PopupWindow(layout, 300, 250, true);
//pwindo.showAtLocation(layout, Gravity.RIGHT, p.x + OFFSET_X, p.y + OFFSET_Y);
//pwindo.setOutsideTouchable(true);
// pwindo.setTouchable(true);
// pwindo.setBackgroundDrawable(new BitmapDrawable());
}
});*/
TextView artistd = (TextView)vi.findViewById(R.id.artistd);
TextView titleB = (TextView)vi.findViewById(R.id.titleB);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
String fontPaths = "fonts/HelveticaNeue-Bold.otf";
Typeface tfs = Typeface.createFromAsset(activity.getAssets(), fontPaths);
title.setTypeface(tfs);
artist.setTypeface(tfs);
titleB.setTypeface(tfs);
artistd.setTypeface(tfs);
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(JobsListing.KEY_TITLE));
artist.setText(song.get(JobsListing.KEY_ARTIST));
artistd.setText(song.get(JobsListing.KEY_PLACE));
titleB.setText(song.get(JobsListing.KEY_CLIENTNAME));
return vi;
}
public void showPopup(int xk , int ys) {
System.out.println("xk>>>"+xk+"xk<<<<"+ys);
LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout,null);
pwindo = new PopupWindow(layout, 300, 250, true);
pwindo.showAtLocation(layout, Gravity.NO_GRAVITY, xk,ys);
pwindo.setOutsideTouchable(true);
pwindo.setTouchable(true);
layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
pwindo.dismiss();
return true;
}
});
}
}
嘗試使用'onClickListener' – Hein 2013-05-09 15:41:39
帖子'showPopup(INT X,int y)對'代碼 – Pragnani 2013-05-09 15:43:56
我想點擊按鈕,這就是易建聯的確切位置使用setOnTouchListener – user2291423 2013-05-09 15:44:18