如何使半视图不可点击

问题描述

嘿,对不起,我的英语不好...

我的 ListFragment 中有一个带有自定义适配器的 ListView。 在我的布局中,我有一个按钮。按钮和整个项目/行是可点击的。我做到了 android:clickable="false" 用于 ImageButton。 正如您在 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);

来源:Get listview item position on button click

现在我可以不用再点击行来处理位置了。