On click of Android listview item's child elements (like button) are getting focus

Deadly 提交于 2019-12-24 10:39:40

问题


I have a listview. Each item in listview is customized. Each item has

text view1

text view2 button1

text view1

text view2 button1

text view1

text view2 button1

on click of item buttons are getting focus. I don't want to get the focus of button even though i have given the following attributes of button in each listview item's layout android:focusable="false" android:focusableInTouchMode="false" Any one please help me out this


回答1:


Try This:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:focusable="false"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content">

</LinearLayout>

Just Focuable false on entire the Layout.




回答2:


Before some days i have face same problem. But now i have solve it.

Do like following.

Make One class called DontPressWithParentButton which extends Button like below:

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

public class DontPressWithParentButton extends Button {

    public DontPressWithParentButton(Context context) {
        super(context);
    }

    public DontPressWithParentButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DontPressWithParentButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

}

Now, in your item row xml, define Button as like below (add your package in place of YOUR_APP_PACKAGE)

<YOUR_APP_PACKAGE.DontPressWithParentButton android:id="@+id/deleteBtn" android:layout_width="100dp"
            android:layout_height="wrap_content" android:layout_alignParentRight="true"
            android:textColor="#FFFFFF"
            android:text="Approve"
            android:focusable="false"
            android:focusableInTouchMode="false"
            />

Now reference id in your java file like below:

DontPressWithParentButton deleteGameBtn=(DontPressWithParentButton) findViewById(R.id.deleteGameBtn);

Now run the project. You will get what you want.

comment me if you have any query.




回答3:


public class DontPressWithParentButton extends Button {

public DontPressWithParentButton(Context context) {
    super(context);
}

public DontPressWithParentButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public DontPressWithParentButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

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

Add the following element in layout file

<DontPressWithParentButton />

Hope this works fine without any flaws




回答4:


Add android:descendantFocusability="blocksDescendants" so that child View(row ) can be blocked.

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants" />

And if you want the vise wersa than add focusable and focusableInTouchMode to false to imageButton/Button in item.xml



来源:https://stackoverflow.com/questions/11339036/on-click-of-android-listview-items-child-elements-like-button-are-getting-foc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!