view from back does not respond to click android

2019-04-14 07:49发布

I have a gridview with items consisting of different views, among which an ImageButton which may or may not be present (depending on the case). I implemented onItemClick for each item inside the grid, but I also need to have a toast showing up when the info button is present (and pressed). Everything worked fine, except the fact that the items which contain the info button will not respond to onItemClick anymore, they only respond to the info icon being pressed. How can I make those items respond accordingly both to click on the info button and on other areas?

Here is the code for onItemClick():

@Override
public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {
        getDayStatus((Date) _monthVectorList.get(0).get(arg2));
        _listenerDateSelect.onDispatchDateSelect((Date) _monthVectorList.get(0).get(arg2)); 
}

and here is the imageButton for Info:

<ImageButton
    android:layout_width="15dp"
    android:layout_height="15dp"
    android:id="@+id/tv_day_info"
    android:layout_gravity="center"
    android:gravity="right|center"
    android:layout_marginLeft="1dp"
    android:onClick="infoToast"
    android:focusable="false"
    />

Also, here is the code for info toast:

public void infoToast(View view){
        Date date = (Date) view.getTag();
    List<String> dayDescription = getDayStatus(date);
    Context context = (Context) getApplicationContext();
    int duration = Toast.LENGTH_SHORT;
    CharSequence text = (String) dayDescription.get(2);

    Toast.makeText(context, text, duration).show();
    }

I tried setting the focus off to the imageButton, but it still does not work. I attach an image to get a better depiction of the issue: http://oi42.tinypic.com/2rcxrv6.jpg

3条回答
做个烂人
2楼-- · 2019-04-14 08:10

try with:

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

inside imagebutton

查看更多
趁早两清
3楼-- · 2019-04-14 08:15

add this to root layout of ImageButton

android:descendantFocusability="blocksDescendants"
查看更多
放荡不羁爱自由
4楼-- · 2019-04-14 08:24

That because your "info Button" has gained focus form your gridview item, you can only click on the button, in this case is 'info Button'. However, you could also click on your gridview item by set the "info Button" NOT focusable. Try this:

<Button
        android:id="@+id/your_info_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="info Button"
        android:focusable="false"
        android:focusableInTouchMode="false" />

Hope this helps.

查看更多
登录 后发表回答