Button inside ListView not clickable

折月煮酒 提交于 2019-12-06 10:13:15

Insert the attribute android:descendantFocusability="blocksDescendants" in the Parent Layout declaration of each list item. The xml should be as follows:

<?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="wrap_content"
android:orientation="vertical" 
android:descendantFocusability="blocksDescendants"
android:background="#C0101010">

    <TextView
    android:id="@+id/event_list_separator"
    style="?android:attr/listSeparatorTextViewStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="separator"
    android:textColor="@android:color/white" />

    <LinearLayout
    android:id="@+id/event_list_element"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFFFFFFF"
    android:padding="6dip" >

        <ImageView
        android:id="@+id/event_list_element_icon"
        android:layout_width="26dip"
        android:layout_height="60dip"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO" />

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

            <TextView
            android:id="@+id/event_list_element_firstLine"
            android:layout_width="match_parent"
            android:layout_height="25dip"
            android:text="item_header"
            android:textSize="18sp" />

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dip"
            android:orientation="horizontal" >

                <TextView
                android:id="@+id/event_list_element_secondLine"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:text="Description"
                android:textSize="14sp" />

                <Button
                android:id="@+id/event_list_element_button_1"
                android:layout_width="132dip"
                android:layout_height="match_parent"
                android:drawableLeft="@drawable/ic_button1"
                android:text="Participate"
                android:textStyle="bold"
                android:textSize="14sp"
                />


                <Button
                android:id="@+id/event_list_element_button_2"
                android:layout_width="110dip"
                android:layout_height="match_parent"
                android:ellipsize="marquee"
                android:drawableLeft="@drawable/ic_button2"
                android:singleLine="true"
                android:text="No thanks"
                android:textStyle="bold"
                android:gravity="center_vertical"
                android:textSize="14sp" 
                />

                <TextView
                android:id="@+id/event_list_element_additional_text"
                android:layout_width="100dip"
                android:layout_height="match_parent"
                android:ellipsize="marquee"
                android:singleLine="true"
                android:gravity="center_vertical"
                android:text="sample"
                android:textStyle="bold"
                android:textSize="14sp" /> 
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

I found the solution! I wanted to update the list of events periodically (right now it's a thread running every x milliseconds, later I want to switch that to only update the event list when there is a change). Anyway, the code was (inside my main activity):

private BroadcastReceiver _bReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(RECEIVE_EVENT)) {
            Bundle bundle = intent.getExtras();
            Event event = (Event) bundle.get("event");
            showEvent(event);
        }
    }
};

private void showEvent(final Event event){
    final Context context = this;

    runOnUiThread(new Runnable() {
        public void run() {
            final ListView listview = (ListView) findViewById(R.id.event_list);

            EventAdapter adapter = new EventAdapter(context, id.event_list, getEventList());
            listview.setAdapter(adapter);     
        }
    });
}

Setting the adapter each time is certainly not the right approach. Once I changed that to only set the adapter once, it worked like suggested using android:descendantFocusability="blocksDescendants"

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