嘿,很抱歉我英语不好。。。
我的ListFragment中有一个带有自定义适配器的ListView。在我的布局中,我有一个按钮。按钮和整个项目/行都是可点击的。我做到了ImageButton的android:clickable="false"
。正如您在getView((方法中看到的那样,我捕捉到了一个点击操作。
public class ArrayAdapterListView extends BaseAdapter {
private final ArrayList<String> arrayList;
private final Context context;
private ArrayList<String> pWListName, pW2ListName;
private ArrayList<Integer> pWListIconID;
private ListPopupWindow listPopupWindow, listPopupWindow2;
private boolean isItemHold = false;
ViewHolder viewHolder = new ViewHolder();
public ArrayAdapterListView(ArrayList<String> list, Context context){
this.arrayList = list;
this.context = context;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint({"ClickableViewAccessibility", "InflateParams"})
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.fragment_file_browser_row, null);
viewHolder = createViewHolder(view);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.textView.setText(arrayList.get(position));
if(selectedItemIndex.isEmpty()) {
for (int i = 0; i < getCount(); i++) {
selectedItemIndex.add(null);
}
}
if(isItemSelectedOnIndex.isEmpty()){
for (int i = 0; i < getCount(); i++){
isItemSelectedOnIndex.add(false);
}
}
view.setOnTouchListener((v, event) -> {
viewHolder.imageView = v.findViewById(R.id.checkedImage);
if(event.getAction() == MotionEvent.ACTION_DOWN){
index = position;
if(L)Log.d("fileBrowser", "index: " + index);
}
//Auswahl betätigen
if (event.getAction() == MotionEvent.ACTION_UP && isItemHold && !isItemSelectedOnIndex.get(index)) {
isItemHold = false;
isItemSelectedOnIndex.set(index, true);
if(L)Log.d("fileBrowser", "Auswahl getätigt: isItemHold = false; isItemSelected = true");
selectedItemIndex.set(index, index);
if(L)Log.d("fileBrowser", "getätigte Auswahl: " + selectedItemIndex);
viewHolder.imageView.setImageResource(R.drawable.ic_checkbox_checked);
}
//Auswahl löschen
else if(event.getAction() == MotionEvent.ACTION_UP && isItemSelectedOnIndex.get(index)){
isItemHold = false;
selectedItemIndex.set(index, null);
if(L)Log.d("fileBrowser", "getätigte Auswahl2: " + selectedItemIndex);
isItemSelectedOnIndex.set(index, false);
if(L)Log.d("fileBrowser", "Auswahl weg gemacht: isItemSelected = false");
viewHolder.imageView.setImageResource(R.drawable.ic_checkbox);
}
return false;
});
view.setOnLongClickListener(v -> {
if(!isItemHold) {
isItemHold = true;
if(L)Log.d("fileBrowser", "Long Click getätigt: isItemHold = true");
}
return false;
});
listPopupWindow = new ListPopupWindow(context);
listPopupWindow2 = new ListPopupWindow(context);
viewHolder.imageButton.setOnTouchListener((v, event) -> {
listPopupWindow.dismiss();
showListPopupWindow(v);
return false;
});
return view;
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/listview_row_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:padding="20dp"
android:background="?android:attr/selectableItemBackground"
tools:ignore="ExtraText">
//
android:background="?android:attr/selectableItemBackground"
oder
activatedBackgroundIndicator
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/checkedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/file_text"
android:src="@drawable/ic_checkbox"
/>
<TextView
android:id="@+id/file_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:text="@string/beispieltext"
app:layout_constraintStart_toEndOf="@id/checkedImage"
app:layout_constraintEnd_toStartOf="@id/btnListPopupWindow"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginLeft="@dimen/default_gap"
android:layout_marginStart="@dimen/default_gap"
android:gravity="center_vertical"
/>
<ImageButton
android:id="@+id/btnListPopupWindow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_baseline_more_vert_24_inv_col"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/ffnet_popupwindow"
android:clickable="false"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
现在我想知道你是否可以在不选择整个视图的情况下点击按钮,但仍然注意到背景上的点击(我知道我在ListView中的位置(我对我的代码的任何其他解决方案都持开放态度,直到我在点击ImageButton以外的任何东西时得到可见的反馈。可见的Feeback应该覆盖整个项目。
我自己解决了这个问题。我已经使ImageButton和ImageView可以再次点击和聚焦,并找到了一个解决方案,我仍然可以获得项目位置(即使我没有点击它(:
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
index = listView.getPositionForView(parentRow);
来源:点击按钮获取列表查看项目位置
现在我可以在不点击行的情况下处理这个位置了。