【问题标题】:OnItemLongClick event of listView AndroidlistView Android 的 OnItemLongClick 事件
【发布时间】:2014-11-12 01:37:48
【问题描述】:

我有一个带有自定义单元格布局的列表视图。实际上它显示的是表格中的数据,有两个按钮,一个用于编辑,另一个用于删除记录。这两个按钮是隐藏的,当长按该行时,这两个按钮就会显示出来。 这是 cell_layout :

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Customer Code and Name "
      android:textSize="16sp"
      android:textColor="#ff000000" />
  <RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">


  <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_marginRight="25dp">
      <TextView
        android:id="@+id/txtCusCode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cus code"
        android:textSize="16sp"
        android:textColor="#ff000000" />
     <TextView
        android:id="@+id/txtCusName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="cus Name"
        android:textSize="16sp"
        android:textColor="#ff000000"
        android:layout_marginLeft="10dp" />

  </LinearLayout>
    <ImageView
        android:id="@+id/imgbtnOrderActions"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/down"
        android:layout_alignParentEnd="false"
        android:clickable="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/test"/>
  </RelativeLayout>

  <TableLayout
    android:id="@+id/tblLayoutOrderAction"
    android:layout_width="fill_parent"
    android:layout_height="0dp">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <ImageView
            android:id="@+id/lmgbtnOrderEdit"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:src="@drawable/edit"
            android:layout_weight="1"
            android:layout_column="1"
            android:clickable="true"
            android:background="#ff00b4df" />
        <ImageView
            android:id="@+id/ImgbtnOrderDelete"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:src="@drawable/delete"
            android:layout_weight="1"
            android:layout_column="2"
            android:background="#ffff625a"
            android:clickable="true" />



        </TableRow>
 </TableLayout>
 </LinearLayout>

这两个按钮在表格布局中,我给它们 0dp 高度来隐藏它们。

这是 listView 的 OnLongItemClick 事件:

 lstviewOrders.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l)
        {
                final TableLayout tblLay = (TableLayout) view.findViewById(R.id.tblLayoutOrderAction);
            TableLayout.LayoutParams lay = new TableLayout.LayoutParams(30, ViewGroup.LayoutParams.MATCH_PARENT);
            tblLay.setLayoutParams(lay);
            return false ;
        }
    });

问题来了。当长按列表视图中的一个项目时,它会显示该项目的编辑和删除按钮,但它也会在下一个第 7 个位置的项目中显示这些按钮。例如,如果我单击位置 3 上的项目,则还会显示 3、10、17、.... 的按钮... 如何解决这个问题???

【问题讨论】:

  • 请发布您的适配器的代码

标签: android android-listview


【解决方案1】:

在我看来,这听起来像是在处理 ListView 的视图回收功能。 This answer 提供了很好的解释。

基本示例:如果一个 ListView 总共有 20 个项目,但只有足够的空间同时在屏幕上显示 4 个,那么 ListView 将只使用 4 个视图对象,但会为列表中的每个项目回收它们。因此,如果您更改视图 2 上的某些内容,然后向下滚动,您会发现此更改也适用于视图 6。这就是使动态视图难以使用 ListViews 的原因。

在上面的示例中,如果您的适配器正在加载视图 6,则在适配器的 getView 方法中,convertView 对象是来自 2 的视图,然后将其重新用于数据元素 6。我会尝试存储按钮是否显示在您的 data 中,并在此方法中重置 convertView,然后根据底层 data 显示/隐藏按钮。

ListView 处理显示底层数据的焦点,但不一定在视图中编辑该数据。

您可以尝试跳过 getView 中尝试使用 convertView 的部分,以便您始终创建新视图,但我发现这可能会导致其他一些意外的 UI 体验。祝你好运!

【讨论】:

    【解决方案2】:

    出现问题是因为列表视图单元格正在重用。 当您显示特定单元格的按钮并滚动列表视图时,重用该单元格的项目也将显示按钮。

    为了避免这个问题,你可以做的是通过任何 int 变量在适配器中占据一个位置,并在适配器中长按时更新位置。

    在适配器的 getView 方法中放置位置检查项目是否具有相同的位置将显示按钮,否则它将设置可见性 GONE。

    就像在适配器中一样:-

    int selectedposition = -1; 
     public View getView(.........){ 
    // your code 
     if (position == selectedposition){
        button.setVisibility(VISIBLE);
     }
    else{ 
          button.setVisibility(GONE);
     }
    
    return convertedView;
    
    }
    

    【讨论】:

    • 什么是 selectedPosition ?你能解释一下吗
    • 我从前两天就陷入了这个问题
    • 在 onItemLongClick 方法中,它是 "i"
    • 在 onItemLongClick 方法中,它是“i”,您必须通过 onItemLongClick 方法在适配器中设置它。
    • "i" 是被点击项的位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多