【问题标题】:Listview click listener inside Item is not working in bottom sheet项目内的Listview单击侦听器在底部工作表中不起作用
【发布时间】:2023-04-08 05:13:02
【问题描述】:

我有一个列表视图,它位于底部表内。一切正常。但是现在我改变了 listview Item 的设计 现在我有 Listview 有两个文本视图和一个图像视图,它们将被视为删除按钮。

list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/llContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
         >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/tvLocationName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:maxLines="1"
                android:singleLine="true"
                android:textColor="@color/primary_text"
                android:layout_gravity="center_vertical|left"
                android:gravity="left"
                android:textAppearance="?android:textAppearanceSmall"
                android:paddingLeft="15dp"/>

            <android.support.v7.widget.AppCompatImageView
                android:id="@+id/ivDelete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                app:srcCompat="@drawable/wrapped_ic_clear"
                android:focusable="false"
                />
        </LinearLayout>


        <TextView
            android:id="@+id/tvLocationAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text=""
            android:maxLines="1"
            android:singleLine="true"
            android:textColor="@color/secondary_text"
            android:layout_gravity="center_vertical|left"
            android:gravity="left"
            android:textSize="12sp"
            android:paddingLeft="15dp"/>



    </LinearLayout>

</RelativeLayout>

在getview中我正在这样做

@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {


    final LocationServiceModel mModel = getItem(position);

    //Check if an existing view is being reused, otherwise inflate the view
    final ViewHolder viewHolder; // view lookup cache stored in tag
    if (convertView == null) {
        //If there's no view to re-use, inflate a brand new view for row
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(resourceLayout, parent, false);

        viewHolder.tvLocationName = (TextView) convertView.findViewById(R.id.tvLocationName);
        viewHolder.tvLocationAddress = (TextView) convertView.findViewById(R.id.tvLocationAddress);

        viewHolder.ivDelete = (AppCompatImageView) convertView.findViewById(R.id.ivDelete);
        viewHolder.llContainer= (LinearLayout) convertView.findViewById(R.id.llContainer);

        convertView.setTag(viewHolder);
    } else {
        // View is being recycled, retrieve the viewHolder object from tag
        viewHolder = (ViewHolder) convertView.getTag();
    }






    viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            listener.onDeleteFavoriteStore(mModel);
        }
    });

    viewHolder.llContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            listener.onFavoriteStoreItemClick(mModel);
        }
    });

    viewHolder.tvLocationName.setText(mModel.getFriendlyName() + "");

    if(mModel.getAddress()!=null && !mModel.getAddress().equalsIgnoreCase("null")) {
        viewHolder.tvLocationAddress.setText(mModel.getAddress());
    }else {
        viewHolder.tvLocationAddress.setText("");
    }


    return convertView;
}




private static class ViewHolder {
    TextView tvLocationName, tvLocationAddress;
    AppCompatImageView ivDelete;

    LinearLayout llContainer;
}

列表视图

 <ListView
    android:id="@+id/lvLocations"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:minHeight="200dp"
   />

但我的点击事件不起作用。

我尝试使用 SO 的一些解决方案,即使用以下属性

  • android:focusable="false"
  • android:descendantFocusability="blocksDescendants"

但没有一个对我有用。

我想要什么:

我想用基本上有 2 次点击监听器的项目制作一个列表视图。

  1. 删除事件>当用户单击图像视图时(在项目中右对齐)
  2. 编辑事件> 当用户单击项目中的任何位置时,除了将用于启动删除事件的图像视图。

【问题讨论】:

  • 请从图像视图中删除 android:focusable="false"
  • 如果您想在项目中的任何位置进行编辑,请在“llContainer”LinearLayout 上添加点击事件
  • 尝试将 android:clicklable="true" 添加到 list_item 的根相对布局
  • android:descendantFocusability="blocksDescendants" 添加到项目根目录。
  • @GaneshPatil 删除焦点没有帮助

标签: android listview bottom-sheet


【解决方案1】:

我认为正如您所说您已经实现了 Motion/Touch 事件,我有一些提示给您。

恐怕您在实现中会消耗触摸事件。您需要返回错误。以便其他工作继续进行。

例如:

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}

请检查,如果您犯了返回 true 的错误。这将表明您已经消费了该事件并且没有进一步的操作要进行。删除它并返回 false。

【讨论】:

    【解决方案2】:

    首先,让ListView的父布局可点击且可聚焦为true

    android:clickable="true"
    android:focusable="true"
    

    botton_sheet_dialog.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/dashBoard_bottomSheetContainer_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bottom_sheet_background"
        android:clickable="true"
        android:focusable="true"
        android:padding="10dp"
       app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    
        <ListView
            android:id="@+id/myListView_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    

    之后,使listview rowlayout可点击和聚焦,删除imageview可点击和聚焦。

    row_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/llContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:clickable="true"
            android:focusable="true">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <TextView
                    android:id="@+id/tvLocationName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text=""
                    android:maxLines="1"
                    android:singleLine="true"
                    android:textColor="@color/primary_text"
                    android:layout_gravity="center_vertical|left"
                    android:gravity="left"
                    android:textAppearance="?android:textAppearanceSmall"
                    android:paddingLeft="15dp"/>
    
                <android.support.v7.widget.AppCompatImageView
                    android:id="@+id/ivDelete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    app:srcCompat="@drawable/wrapped_ic_clear"
                    android:clickable="true"
                    android:focusable="true"/>
            </LinearLayout>
    
    
            <TextView
                android:id="@+id/tvLocationAddress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text=""
                android:maxLines="1"
                android:singleLine="true"
                android:textColor="@color/secondary_text"
                android:layout_gravity="center_vertical|left"
                android:gravity="left"
                android:textSize="12sp"
                android:paddingLeft="15dp"/>
        </LinearLayout>
    
    </RelativeLayout>
    

    在适配器中这样做,

    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    
    
        final LocationServiceModel mModel = getItem(position);
    
        //Check if an existing view is being reused, otherwise inflate the view
        final ViewHolder viewHolder; // view lookup cache stored in tag
        if (convertView == null) {
            //If there's no view to re-use, inflate a brand new view for row
            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(resourceLayout, parent, false);
    
            viewHolder.tvLocationName = (TextView) convertView.findViewById(R.id.tvLocationName);
            viewHolder.tvLocationAddress = (TextView) convertView.findViewById(R.id.tvLocationAddress);
    
            viewHolder.ivDelete = (AppCompatImageView) convertView.findViewById(R.id.ivDelete);
            viewHolder.llContainer= (LinearLayout) convertView.findViewById(R.id.llContainer);
    
            convertView.setTag(viewHolder);
        } else {
            // View is being recycled, retrieve the viewHolder object from tag
            viewHolder = (ViewHolder) convertView.getTag();
        }
    
    
    
    
    
    
        viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ListView) parent).performItemClick(v, position, 0);
            }
        });
    
        viewHolder.llContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ListView) parent).performItemClick(v, position, 0);
            }
        });
    
        viewHolder.tvLocationName.setText(mModel.getFriendlyName() + "");
    
        if(mModel.getAddress()!=null && !mModel.getAddress().equalsIgnoreCase("null")) {
            viewHolder.tvLocationAddress.setText(mModel.getAddress());
        }else {
            viewHolder.tvLocationAddress.setText("");
        }
    
    
        return convertView;
    }
    
    
    
    
    private static class ViewHolder {
        TextView tvLocationName, tvLocationAddress;
        AppCompatImageView ivDelete;
    
        LinearLayout llContainer;
    }
    

    在您的活动中的 ListViw 上添加 onItemClickListener。

    ListView listView = findViewById(R.id.lvLocations);
    
    ///set your adaper here
    
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
       @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        long viewId = view.getId();
    
        if (viewId == R.id.ivDelete) {
            Toast.makeText(MainActivity.this, "Delete Item", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Edit item", Toast.LENGTH_SHORT).show();
        }
    }
    });
    

    其实performItemClick()函数会触发listView中的onItemClick()函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多