2013-05-09 107 views
0

我真的碰到这个问题.. 我有一个列表视图,其中包含一个按钮,当点击该按钮时,它应该显示一个弹出。我已经实现了列表视图,但弹出菜单没有显示在点击的按钮位置..下面列表视图与按钮和弹出

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; 
      } 


     }); 

} 

}

+0

尝试使用'onClickListener' – Hein 2013-05-09 15:41:39

+0

帖子'showPopup(INT X,int y)对'代码 – Pragnani 2013-05-09 15:43:56

+0

我想点击按钮,这就是易建联的确切位置使用setOnTouchListener – user2291423 2013-05-09 15:44:18

回答

1

event.getY()返回Y相对于父母的位置,所以我们必须找到被点击的行的位置和每行的高度来计算y位置。

此代码显示弹出点击在该行的ImageView与littele修改这将是您的使用。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder = null; 
    if(convertView == null) { 
     LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.list_row, null); 
     holder = new ViewHolder(); 
     convertView.setTag(holder); 

     holder.imgView = (ImageView) convertView.findViewById(R.id.txtViewClientName); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.imgView.setTag(position); 

    holder.imgView.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      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; 
     } 

     private void showPopup(int x, int y) { 
       LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View layout = inflater.inflate(R.layout.popup_layout,null); 

       final PopupWindow pwindo = new PopupWindow(layout, 200, 300, true); 
       pwindo.showAtLocation(layout, Gravity.NO_GRAVITY, x, y); 
       pwindo.setOutsideTouchable(true); 
       pwindo.setTouchable(true); 


       layout.setOnTouchListener(new OnTouchListener() { 

        @Override 
        public boolean onTouch(View arg0, MotionEvent arg1) { 
         pwindo.dismiss(); 
         return true; 
        } 
       }); 

      } 

    }); 

    return convertView; 
} 

private static class ViewHolder { 
    public ImageView imgView = null; 
} 

// popup_layout 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="200dp" 
android:layout_height="200dp" 
android:background="@android:color/darker_gray" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:text="Popup" > 
</TextView> 

</LinearLayout> 


    /// list_row layout 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/statusImage" 
    android:layout_width="wrap_content" 
    android:src="@drawable/ic_launcher" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 

</LinearLayout> 
+0

此代码使用图像视图,您可以使用list_row layout.if任何帮助请通知我的按钮。这个显示弹出图片视图被点击。 – 2013-05-09 18:25:26

+0

感谢名单..但它始终显示一个位置对我来说... – user2291423 2013-05-10 04:56:03

+0

可你给我你的适配器类 – 2013-05-10 04:58:05

0

使用此给出了弹出

示例代码:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage(x + " - " + Y) 
       .setPositiveButton("ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

        } 
       }) 
builder.create(); 
+0

嗨,我正在使用android弹出 – user2291423 2013-05-09 15:49:48

+0

你没有写你的代码,如果showPopup(x,y)。 – 2013-05-09 15:55:35