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

后端 未结 1 1843
攒了一身酷
攒了一身酷 2021-02-09 00:57

I have a Custom List view that works perfectly. Now, I want to have a drop down menu for each item in the list so that when the user taps on any item of the list,the drop down

相关标签:
1条回答
  • 2021-02-09 01:26

    Check This:

    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>
    

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

    Popup 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>
    

    Refer This: Android List view layout Similar to Google play

    0 讨论(0)
提交回复
热议问题