开发者

ListView with multiple buttons, list item can't be clicked

开发者 https://www.devze.com 2023-03-09 16:23 出处:网络
I have a list with two buttons in it. When I want to click a list item it doesn\'t work, but my button is still clickable.

I have a list with two buttons in it. When I want to click a list item it doesn't work, but my button is still clickable. How I can make all buttons include the entire list item to be clickable?

List item:

<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:mode="twoLine">
       
              <Button
                    android:id="@+id/erase"
                    android:layout_width="40dip"
                    android:layout_height="40dip"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/>
              <ImageButton android:id="@+id/soundf"
                    android:layout_width="40dip"
                    android:layout_height="40dip"
                    android:focusable="false"
                    android:focusableInTouchMode="false"/> 
              <TextView android:id="@+id/texxt1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textColor="#CC0"/>
</TwoLineListItem>

Layout containing the ListView:

开发者_开发知识库

    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">
        <Button android:id="@+id/left" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="English to Indonesia"
            android:layout_weight="1" 
            android:background="@drawable/chbutt" />
        <Button android:id="@+id/right" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" 
            android:text="Indonesia to English"
            android:layout_weight="1" 
            android:background="@drawable/chbutt" />
    </LinearLayout>
    
    <ListView android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:id="@+id/history"
        android:headerDividersEnabled="false"
        android:footerDividersEnabled="false"
        android:isScrollContainer="false" />
</LinearLayout>


For Buttons, Checkboxs and ImageViews:

android:focusable="false"

Now both (buttons and rows) of ListView are clickable.

For ImageButtons, you have to set focusable while running, because the constructor of ImageButtons sets it to true. I recommend you using a ImageView instead of a ImageButton.


<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  android:id="@+id/rel1" android:layout_width="fill_parent"
        android:layout_height="wrap_content">

<Button
        android:id="@+id/erase"
        android:layout_marginLeft="6dip"
        android:layout_marginTop="6dip"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:background="@drawable/closebtn"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
   <ImageButton android:id="@+id/soundf"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_below="@+id/erase"
        android:layout_alignLeft="@+id/erase"
        android:background="@drawable/soundinv"
        android:focusable="false"
        android:focusableInTouchMode="false"
        /> 
   <TextView android:id="@+id/texxt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/erase"
        android:layout_alignTop="@+id/erase"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#CC0"
   />

   <TextView android:id="@+id/texxt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/texxt1"
        android:layout_alignLeft="@+id/texxt1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFF"
   />
</RelativeLayout>

now you can also given click event to relative layout.


Just add this to your Java code: holder.yourButton.setFocusable(false); I am using my own cursorAdapter so I am putting the line of code at the end of bindView this should do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消