I
I
Igor2014-06-09 09:29:32
Android
Igor, 2014-06-09 09:29:32

How to prevent state_pressed from propagating to LinearLayout child?

Given a ListView. Each item of this ListView contains:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/fl_item"
             android:orientation="horizontal"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@drawable/back_list_item"

        >
<LinearLayout
                      android:layout_width="0dip" android:layout_height="fill_parent"
                      android:layout_weight="1"
                      android:orientation="vertical"
                      android:paddingRight="5dip"
                      android:paddingLeft="5dip"
                      android:paddingBottom="5dip"
                      >
            <TextView android:id="@+id/tv_title"
                      android:layout_width="fill_parent" android:layout_height="wrap_content"
                      android:singleLine="false"
                      android:layout_weight="1"
                      android:text="Lenovo A 1000"
                      android:textSize="14sp"
                      android:textStyle="normal"
                      android:maxLines="3"
                      />
</LinearLayout>
<ImageView android:id="@+id/toBasketImageView"
                android:layout_width="wrap_content" android:layout_height="fill_parent"
                android:scaleType="fitCenter"
                android:src="@drawable/item_add_to_basket"
/>
</LinearLayout>

ImageView containing drawable item_add_to_basket:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/to_basket_2_white" />
    <item android:state_pressed="false" android:drawable="@drawable/to_basket_1_white" />
</selector>

Each item must handle clicks (setOnItemClickListener is called) and also the ImageView with id toBasketImageView must handle clicks. It turns out that the ImageView shows the pressed state both when you click on it and when you click on the rest of the item.
How to avoid it? How to show pressed state only when ImageView is clicked?
PS. I tried adding clickable="true" to the LinearLayout - the state_pressed state is not passed to the ImageView but then the OnItemClickListener doesn't work either.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Marat S, 2014-06-23
@aratj

if still relevant, then one of the options is
to write your own components
for example.

package widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

public class ImageViewEx extends ImageView {

  public ImageViewEx(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
  }

  @Override
    public void setPressed(boolean pressed) {
        if (pressed && getParent() instanceof View && ((View) getParent()).isPressed()) {
            return;
        }
        super.setPressed(pressed);
    }
}

By the way, as far as I remember, this behavior on versions from 3.0 has already been fixed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question