[英]Listview click listener inside Item is not working in bottom sheet
我有一个列表视图,它位于Bottomsheet 内。 一切正常。 但是现在我已经改变了 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 的一些解决方案,即使用以下属性
但他们都没有为我工作。
我想要的是:
我想用基本上有 2 个点击侦听器的项目制作一个列表视图。
我想正如你所说的,你有 Motion/Touch 事件的实现,我有一些提示给你。
我担心你在你的实现中消耗了触摸事件。 你需要返回false。 以便其他工作继续进行。
例如:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
请检查,如果您犯了返回 true 的错误。 这将表明您已经消费了该事件并且没有进一步的操作要进行。 删除它并返回false。
首先,使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() 函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.