您的位置:一分快三全天精准计划 > 1分快3全天计划官方网站-编程 > 获取当前点击的item

获取当前点击的item

2019-09-09 16:51

ListView布局包括checkbox、img、title、type、两个Button、三个textview,imgDelete

图片 1这里写图片描述

xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="90dp" android:background="@color/white" android:gravity="center_vertical" android:orientation="horizontal"> <!-- 小对勾的布局 --> <CheckBox android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp_10" android:focusable="false" android:focusableInTouchMode="false" /> <!-- 图片布局 --> <ImageView android: 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: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="连衣裙女夏季" android:textSize="@dimen/sp_14" /> <TextView android: 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: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="500g" android:textSize="12sp" /> <TextView android: 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: android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/add_button_shape" android:src="@mipmap/icon_minus_3x" /> <TextView android: 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: 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: 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; view.setTag; } 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;

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

 adapter = new ShoppingCartListViewAdapter(getActivity; shopping_listview.setAdapter; **adapter.setOnDelItem;**

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

 if (tag != null && tag instanceof Integer) { int position =  tag; showPopupMenu(view, position); }

 //删除弹窗 public void showPopupMenu(final View view, final int position) { final PopupMenu popup = new PopupMenu(getActivity; 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; adapter.notifyDataSetChanged(); break; } return false; } }); popup.show(); }

popup

<?xml version="1.0" encoding="utf-8"?> <menu xmlns:andro> <item android: android:title="@string/delete"/> </menu>

本文由一分快三全天精准计划发布于1分快3全天计划官方网站-编程,转载请注明出处:获取当前点击的item

关键词: