2014-04-04 65 views
0

我有一個imageview的並且需要知道在RelativeLayout的屏幕大小和位置,使彈出對話框右側在RelativeLayout的。如何在父傳位置的RelativeLayout

ImageView的是RelativeLayout的內部和監聽器是一個轉接器內,並需要傳遞的信息正確

public View getView(int position, View convertView, ViewGroup parent) { 
ViewHolder vHolder; 
if (convertView != null) 
    vHolder = (ViewHolder) convertView.getTag(); 
else { 
    convertView = mInflater.inflate(R.layout.car_list, null); 
    vHolder = new ViewHolder(); 
    vHolder.textView = ((TextView) convertView.findViewById(R.id.tvCarListText)); 
    vHolder.textView2 = ((TextView) convertView.findViewById(R.id.tvCarDescriptionListText)); 
    vHolder.imageView = ((ImageView) convertView.findViewById(R.id.ivCarListMenuIcon)); 
    vHolder.relativeLayout = ((RelativeLayout) convertView.findViewById(R.id.relativeLayout)); 
    convertView.setTag(vHolder); 


    vHolder.imageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final Rect rect = new Rect(); 
      v.getGlobalVisibleRect(rect); 
      showCarEditDialog(rect); //THIS NEED TO PASS RELATIVELAYOUT POSITION ON SCREEN<-------------        
     } 
    }); 
} 
return convertView; 
} 

回答

0

我設法找到了答案,但仍然不像我想象的不完全適合在在RelativeLayout的

 vHolder.imageView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       final View parent = (View)v.getParent(); 
       final int[] screenLocation = new int[2]; 
       parent.getLocationOnScreen(screenLocation); 
       final int width = parent.getWidth(); 
       final int height = parent.getHeight(); 
       showCarEditDialog(Integer.parseInt(v.getTag().toString()), screenLocation[0], screenLocation[1], width, height); 
      } 
     }); 

does not fit ok

和對話框代碼

private void showCarEditDialog(int position, int x,int y,int width,int height) { 
    final Dialog mDialog = new Dialog(this.context); 
    LayoutInflater mInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final LinearLayout mDialogView = (LinearLayout) mInflater.inflate(R.layout.car_edit, null); 
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    mDialog.setContentView(mDialogView); 
    WindowManager.LayoutParams lp=mDialog.getWindow().getAttributes(); 
    lp.x=x;lp.y=y;lp.width=width;lp.height=height;lp.gravity= Gravity.TOP | Gravity.LEFT; 
    lp.dimAmount=0; 
    mDialog.getWindow().setAttributes(lp); 
    mDialog.show(); 
} 
+0

仍然需要一些幫助,我如何使它適合相對佈局 – rnascimento13

+0

還有另一種方式獲取除view.getLocationOnScreen(screenLocation)之外的位置?????? – rnascimento13