我有一个listview,它位于Bottomsheet中。一切都很好。但是现在我已经更改了listview项目的设计现在我有了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
<ListView
android:id="@+id/lvLocations"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:minHeight="200dp"
/>
但我的点击事件无效。
我尝试过使用SO的一些解决方案,即使用以下属性
但他们没有为我工作。
我想要的是什么:
我想用基本上有2个点击监听器的项目制作一个列表视图。
答案 0 :(得分:0)
我认为,正如您所说,您已经实施了Motion / Touch活动,我会为您提供一些提示。
我担心您在实施中消耗触摸事件。你需要返回false。这样其他工作就可以继续下去了。
例如:
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
请检查,如果你犯了这个错误的回复真实。这将表明您已经消耗了该事件,并且没有进一步的操作。删除它并返回false。
答案 1 :(得分:0)
首先,使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行布局可点击和可聚焦,并删除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()函数。