列表视图与按钮和弹出窗口

时间:2013-05-09 15:39:41

标签: android listview

我真的很震惊这个问题.. 我有一个列表视图,其中包含一个按钮,当单击该按钮时,它应显示一个弹出窗口。我已经实现了列表视图,但弹出窗口没有显示在单击的按钮位置..

下面给出的示例代码

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


        });

}

}

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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();