【发布时间】: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 次点击监听器的项目制作一个列表视图。
- 删除事件>当用户单击图像视图时(在项目中右对齐)
- 编辑事件> 当用户单击项目中的任何位置时,除了将用于启动删除事件的图像视图。
【问题讨论】:
-
请从图像视图中删除 android:focusable="false"
-
如果您想在项目中的任何位置进行编辑,请在“llContainer”LinearLayout 上添加点击事件
-
尝试将 android:clicklable="true" 添加到 list_item 的根相对布局
-
将
android:descendantFocusability="blocksDescendants"添加到项目根目录。 -
@GaneshPatil 删除焦点没有帮助
标签: android listview bottom-sheet