【问题标题】:How to make a half view not clickable如何使半视图不可点击
【发布时间】:2021-07-17 16:58:30
【问题描述】:

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

我的 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>

现在我想知道您是否可以在不选择整个视图的情况下获得 Clickable 按钮,但仍然注意到背景上的点击(我知道我在 ListView 中的位置)。 我对我的代码的任何其他解决方案持开放态度,直到我在单击除 ImageButton 之外的任何其他内容时获得可见的反馈。可见的反馈应该覆盖整个项目。

【问题讨论】:

  • 您能否添加一张图片以便我们更好地理解?
  • 我很抱歉,但很遗憾我无法分享图片,因为我的声誉太低了。在我的 ListView 行中,左侧有一个复选框。然后是带有我的 ListView 内容的 TextView。右边是一个带有三个点的 ImageButton,它打开一个 PopupWindow,然后有几个动作。当我单击三个点(ImageButton)时,将执行灰色单击动画。我希望仅当我在视图上的 ImageButton 外部单击时才执行动画。但是当我点击 ImageButton 时,应该仍然可以感知到点击,但没有动画。

标签: android listview imagebutton ontouchlistener clickable


【解决方案1】:

我自己解决了这个问题。我已经使 ImageButton 和 ImageView 再次可点击和聚焦,并找到了一个我仍然可以获得项目位置的解决方案(即使我没有点击它):

View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
index = listView.getPositionForView(parentRow);

来源:Get listview item position on button click

现在我可以处理该位置而无需再单击该行了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-03
    相关资源
    最近更新 更多