美文网首页Androidandroid基础IT互联网那些事儿
ListView调用按钮删除某一行item

ListView调用按钮删除某一行item

作者: 八怪不姓丑 | 来源:发表于2016-09-24 18:58 被阅读4448次

    ListView布局包括checkbox(复选框)、img、title(商品名称)、type(分类)、两个Button(加减)、三个textview(原价,单价,数量),imgDelete(delete)


    这里写图片描述

    xml:

    <?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="90dp"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal">
        <!-- 小对勾的布局 -->
        <CheckBox
            android:id="@+id/shopping_lv_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_10"
            android:focusable="false"
            android:focusableInTouchMode="false" />
        <!-- 图片布局 -->
        <ImageView
            android:id="@+id/shopping_lv_img"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_margin="10dp"
            android:scaleType="centerCrop"
            android:src="@mipmap/tuceng1" />
        <!-- 商品名称和价格的布局 -->
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!-- 商品名称的布局 -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dp_5"
                android:gravity="center_vertical">
    
                <TextView
                    android:id="@+id/shopping_lv_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="连衣裙女夏季"
                    android:textSize="@dimen/sp_14" />
    
                <TextView
                    android:id="@+id/shopping_lv_moneyDel"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginEnd="20dp"
                    android:text="1111"
                    android:textSize="@dimen/sp_14" />
    
            </RelativeLayout>
            <!-- 价格的布局 -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/shopping_lv_content"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="500g"
                    android:textSize="12sp" />
    
                <TextView
                    android:id="@+id/shopping_lv_money"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignTop="@+id/shopping_lv_content"
                    android:layout_gravity="bottom"
                    android:layout_marginEnd="20dp"
                    android:text="¥66"
                    android:textColor="@color/balance_bg"
                    android:textSize="16sp" />
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="33dp"
                android:gravity="center_vertical"
                android:orientation="horizontal">
                <!-- 加号 -->
                <ImageView
                    android:id="@+id/shopping_lv_sub"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="@drawable/add_button_shape"
                    android:src="@mipmap/icon_minus_3x" />
    
                <TextView
                    android:id="@+id/shopping_lv_number"
                    android:layout_width="30dp"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="@dimen/dp_10"
                    android:layout_marginStart="10dp"
                    android:layout_toEndOf="@+id/shopping_lv_sub"
                    android:gravity="center"
                    android:inputType=""
                    android:text="1"
                    android:textSize="13sp" />
                <!-- 减号-->
                <ImageView
                    android:id="@+id/shopping_lv_add"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignTop="@+id/shopping_lv_sub"
                    android:layout_marginLeft="@dimen/dp_10"
                    android:layout_toEndOf="@+id/shopping_lv_number"
                    android:background="@drawable/add_button_shape"
                    android:src="@mipmap/icon_add_3x" />
    
                <ImageView
                    android:id="@+id/shopping_lv_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_marginEnd="20dp"
                    android:layout_marginLeft="@dimen/dp_110"
                    android:layout_marginRight="@dimen/dp_15"
                    android:src="@mipmap/btn_delete_3x" />
            </RelativeLayout>
    
    
        </LinearLayout>
    </LinearLayout>
    
    

    获取listview中的imgDelete控件,执行删除单条纪录的操作。
    ShoppingCartListViewAdapter:

     private View.OnClickListener onDelItem;//删除单条item的接口
    
    ...
    
    
      public View getView(final int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if (view == null) {
                holder = new ViewHolder();
                view = LayoutInflater.from(this.context).inflate(R.layout.shopping_listview_item, null, true);
              //实例化item获取控件
                holder.shopping_lv_delete = (ImageView) view.findViewById(R.id.shopping_lv_delete);
                //设置回调监听
                holder. shopping_lv_delete.setOnClickListener(onDelItem);
                holder.shopping_lv_delete.setTag(i);
                view.setTag(holder);
    
            } else {
                holder = (ViewHolder) view.getTag();
            }
    
    
     class ViewHolder {
            
            ImageView shopping_lv_delete;
        }
        
        /**
        *定义监听接口
        */
     public void setOnDelItem(View.OnClickListener onDelItem) {
            this.onDelItem = onDelItem;
        }
    

    Activity:

    private ShoppingCartListViewAdapter adapter;
    private ListView shopping_listview;
    private List<ShoppingCartListView> list = new ArrayList<ShoppingCartListView>();
    
    ...
    
    shopping_lv_delete = (ImageView) findViewById(R.id.shopping_lv_delete);
    shopping_lv_delete.setOnClickListener(this);
    

    onCreate方法中设置适配器的监听,

     adapter = new ShoppingCartListViewAdapter(getActivity(), list);
            shopping_listview.setAdapter(adapter);
            **adapter.setOnDelItem(this);**
    

    通过Tag的position,获取当前点击的item,
    这里使用了popupMenu。在点击删除的时候会弹出确认删除框

    
        if (tag != null && tag instanceof Integer) { 
                        int position = (Integer) tag;
                        showPopupMenu(view, position);
                    }
    
        //删除弹窗
        public void showPopupMenu(final View view, final int position) {
            final PopupMenu popup = new PopupMenu(getActivity(), view);
            popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        case R.id.remove:
                            list.remove(position);
                            adapter.notifyDataSetChanged();
                            break;
                    }
                    return false;
                }
            });
            popup.show();
        }
    

    popup

    <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@+id/remove"
                android:title="@string/delete"/>
        </menu>
    
    

    相关文章

      网友评论

      • 0d3681ad6949:通过Tag的position,获取当前点击的item,
        这里使用了popupMenu。在点击删除的时候会弹出确认删除框

        if (tag != null && tag instanceof Integer) {
        int position = (Integer) tag;
        showPopupMenu(view, position);
        }
        请问这里的tag是怎么得到的
        八怪不姓丑: Object tag = view.getTag();
        八怪不姓丑:case R.id.shopping_lv_delete:
        //解决问题:如何知道你点击的按钮是哪一个列表项中的,通过Tag的position
        if (tag != null && tag instanceof Integer) {
        int position = (Integer) tag;
        showPopupMenu(view, position);
        }
        break;
      • 0d3681ad6949:if (tag != null && tag instanceof Integer) {
        int position = (Integer) tag;
        showPopupMenu(view, position);
        }
        请问这段代码是写在哪里的
        八怪不姓丑: @花与爱丽丝xs 是的
        0d3681ad6949: @八怪不姓丑 是在回调函数里面吗
        八怪不姓丑:@花与爱丽丝xs activity啊

      本文标题:ListView调用按钮删除某一行item

      本文链接:https://www.haomeiwen.com/subject/deixyttx.html