Illegal state on click imageview with background (

2019-09-02 10:05发布

问题:

I have appwidget with collection (listview). Source widget xml layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/widget_list_background"
    android:orientation="vertical"
    android:paddingBottom="3dip"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:paddingTop="3dip" >
<!-- Header -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <!-- App icon -->

    <ImageView
        android:id="@+id/widget_list_icon"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:layout_marginRight="3dip"
        android:src="@drawable/airplane" />

    <!-- Mailbox -->

    <TextView
        android:id="@+id/widget_list_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="2"
        android:text="john@foo.com"
        android:textColor="@color/widget_text_primary" />

    <!-- Unread count -->

    <TextView
        android:id="@+id/widget_list_unread"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="3dip"
        android:text="12"
        android:textColor="@color/widget_text_secondary"
        android:textStyle="bold" />
</LinearLayout>

<!-- Overflow menu -->

<ImageView
    android:id="@+id/widget_list_overflow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/simple_button_holo"
    android:paddingLeft="4dip"
    android:paddingRight="4dip"
    android:src="@drawable/toolbar_overflow" />

<!-- Line -->

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="1dip"
    android:layout_marginBottom="3dip"
    android:layout_marginTop="3dip"
    android:background="@color/widget_list_line" />

<!-- Listview with messages -->

<ListView
    android:id="@+id/widget_email_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Removed -->

<TextView
    android:id="@+id/widget_list_removed"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/widget_removed_account"
    android:textColor="@color/widget_text_primary"
    android:textSize="18dip"
    android:textStyle="bold"
    android:visibility="gone" />

<!-- Empty list -->

<TextView
    android:id="@+id/widget_list_empty_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/widget_inbox_empty"
    android:textColor="@color/widget_text_primary"
    android:textSize="18dip"
    android:textStyle="bold"
    android:visibility="gone" />

</LinearLayout>

At layout exists imageview (widget_list_overflow). I set imageview background: simple_button_holo

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="true"
          android:drawable="@drawable/simple_button_focused_holo" />

    <item android:state_pressed="true"
          android:drawable="@drawable/simple_button_pressed_holo" />

    <item android:drawable="@android:color/transparent" />
</selector>

Issue: ImageView highlights on click not only on ImageView, but on whole layout, where ImageView contains. But why not only on click on ImageView?

PS: Thanks codeMagic :)

EDIT: (UPD1) I create RemoteViews as:

myview = new RemoteViews(packName, R.layout.mywidget); 
Intent svcIntent = new Intent(ctx, WidgetListService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
myview.setEmptyView(R.id.widget_email_list, R.id.widget_list_empty_view);
myview.setRemoteAdapter(R.id.widget_email_list, svcIntent);
aAppWidgetManager.updateAppWidget(_widgetId, myview);

I don't use any PendingIntent, only set TextView text and hide/show some views. Also logcat on click is empty.

回答1:

Hm, solution was easy. Now I add pending intent on click ImageView (widget_list_overflow) and issue disappeared.