0

Hey and sorry for my bad English...

I have a ListView in my ListFragment with a custom Adapter. In my Layout I have an Button. The Button and the whole item/row is clickable. I made that happen with android:clickable="false" for the ImageButton. As you can see in the getView() method, I catch a click action.

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;
}

Here is my layout:

<?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>

Now I want to know if you can get the button Clickable without selecting the whole view, but still notice the click on the background (that I know where I am in my ListView). I am open to any other solution of my code until I have a visible feedback while I am clicking on anything else than the ImageButton. And the visible Feeback should go over the whole item.

  • Can you perhaps add an image so we better understand? – Sarah Khan Apr 23 '21 at 14:43
  • I apologize, but unfortunately I can not share pictures because I have too little reputation. In my ListView row I have a checkbox on the left side. Then comes a TextView with the content of my ListView. On the right side is an ImageButton with three dots, which opens a PopupWindow that then has several actions. When I click on the three dots (ImageButton) the grey click animation is executed. I want the animation to be executed only when I click outside the ImageButton on my view. But when I click on the ImageButton, the click should still be perceived, but without the animation. – chrissiWHG Apr 23 '21 at 14:58

1 Answers1

0

I have solved the problem for myself. I have made the ImageButton and the ImageView clickable and focussable again and found a solution that I still get the item position (even if I dont clicking on it):

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

Source: Get listview item position on button click

Now I can handle the position without clicking on the row anymore.