为自定义 ListView 的每个项目添加一个下拉菜单

Add a drop down menu for each item of a Custom ListView

我有一个完美的自定义列表视图。 现在,我想为列表中的每个项目创建一个下拉菜单,以便当用户点击列表中的任何项目时,下拉菜单显示两个选项 - 编辑和删除。根据用户的选择处理列表视图项。

我想知道给每个listview项目添加下拉菜单的方法。

xml 列表项文件-

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="120dp"
android:background="#FFFFFF">
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:id="@+id/chkitem"
    android:layout_alignParentLeft="true"
   />
<TextView android:id="@+id/textTitle"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:textSize="15dp"
    android:textColor="#000000"
    android:textStyle="bold"
    android:gravity="fill"
    android:layout_toRightOf="@+id/chkitem"
    />
<TextView
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:id="@+id/detail"
    android:textSize="15dp"
    android:textStyle="italic"
    android:gravity="fill"
    android:layout_toRightOf="@+id/chkitem"
    android:layout_below="@+id/textTitle"/>
    </RelativeLayout>

适配器class

 public class list_addr_adapter extends ArrayAdapter<list_addr> {

Context context;
int layoutResourceId;



public list_addr_adapter(Context context, int layoutResourceId,          List<list_addr> items) {
    super(context, layoutResourceId, items);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    //  this.listener=callback;
}

/*private view holder class*/
private class ViewHolder {

    TextView txtTitle;
    TextView txtDetail;

}
ViewHolder holder = null;

public View getView(int position, View convertView, ViewGroup parent) {
    final list_addr lists = getItem(position);
    final int pos=position;


    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.items_sav_addr2, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle);
        holder.txtDetail = (TextView) convertView.findViewById(R.id.detail);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();






    holder.txtTitle.setText(lists.getTitle());
    holder.txtDetail.setText(lists.getDetail());

    return convertView;
}

  }

我希望它是这样的 -

检查这个:

Xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#FFFFFF">

<CheckBox
    android:id="@+id/chkitem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/textTitle"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_toRightOf="@+id/chkitem"
    android:gravity="fill"
    android:textColor="#000000"
    android:textSize="15dp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/detail"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_below="@+id/textTitle"
    android:layout_toRightOf="@+id/chkitem"
    android:gravity="fill"
    android:textSize="15dp"
    android:textStyle="italic" />

<ImageView
    android:id="@+id/imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerInParent="true"
    android:src="@mipmap/ic_launcher" />// Replace Image

</RelativeLayout>

适配器Class:

public class list_addr_adapter extends ArrayAdapter<list_addr> {

Context context;
int layoutResourceId;



public list_addr_adapter(Context context, int layoutResourceId,          List<list_addr> items) {
    super(context, layoutResourceId, items);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    //  this.listener=callback;
}

/*private view holder class*/
private class ViewHolder {

    TextView txtTitle;
    TextView txtDetail;
    ImageView imageview;

}
ViewHolder holder = null;

public View getView(int position, View convertView, ViewGroup parent) {
    final list_addr lists = getItem(position);
    final int pos=position;


    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.items_sav_addr2, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle);
        holder.txtDetail = (TextView) convertView.findViewById(R.id.detail);
        holder.imageview = (ImageView) convertView.findViewById(R.id.imageview);

        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtTitle.setText(lists.getTitle());
    holder.txtDetail.setText(lists.getDetail());

    try {
        holder.imageview.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            switch (v.getId()) {
                case R.id.imageview:

                    PopupMenu popup = new PopupMenu(getApplicationContext(), v);
                    popup.getMenuInflater().inflate(R.menu.clipboard_popup,
                            popup.getMenu());
                    popup.show();
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {

                            switch (item.getItemId()) {
                                case R.id.edit:

                                    //Or Some other code you want to put here.. This is just an example.
                                    Toast.makeText(getApplicationContext(), " Install Clicked at position " + " : " + position, Toast.LENGTH_LONG).show();

                                    break;
                                case R.id.delete:

                                    Toast.makeText(getApplicationContext(), "Add to Wish List Clicked at position " + " : " + position, Toast.LENGTH_LONG).show();

                                    break;

                                default:
                                    break;
                            }

                            return true;
                        }
                    });

                    break;

                default:
                    break;
            }


        }
    });

} catch (Exception e) {

    e.printStackTrace();
}

return convertView;
}

 }

弹出menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/install"
    android:title="Install" />
<item
    android:id="@+id/addtowishlist"
    android:title="Add to wishlist" />
</menu>

参考这个:Android List view layout Similar to Google play