简体   繁体   中英

When scrolling a listview List Items not clickable

I have a problem with a ListView that happens only when I have scrolled down to see my list items.

Here is my main xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:id="@+id/AddAlarm"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:clickable="true"
    android:layout_margin="10dp" >
<ImageView
    android:src="@drawable/add"
    android:gravity="right"
    android:layout_width="30dp"
    android:layout_height="30dp" 
    android:layout_marginRight="0dp"/>            
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="20dp"
    android:gravity="left"
    android:textSize="25dp"
    android:text="@string/add_alarm" />
</LinearLayout>     
<ListView
    android:id="@+id/ListaAlarmas"
    android:scrollbars="vertical"
    android:focusable="false" android:clickable="false"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

And this is my Row item xml loaded in the above ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ToggleButton
    android:id="@+id/CheckBox"
    android:textOn=""
    android:textOff=""
    android:focusableInTouchMode="false"
    android:focusable="false"
    android:layout_gravity="center"
    android:background="@drawable/clock_off"
    android:layout_margin="10dp"
    android:layout_height="45dp"
    android:layout_width="45dp" />  
<View
    android:id="@+id/firstDivider"
    android:layout_height="fill_parent"
    android:focusableInTouchMode="false"
    android:focusable="false"
    android:layout_width="2dp"
    android:background="#999999" />       
<LinearLayout
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_vertical"
    android:focusableInTouchMode="false"
    android:focusable="false"
    android:layout_weight="3"
    android:layout_marginLeft="30dp" >
    <TextView
        android:id="@+id/TextoHora"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"/>
    <TextView
        android:id="@+id/TextoRepetir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 </LinearLayout>    

The first LinearLayout is clickable (AddAlarm) and it simply adds another item to the ListView. Every item in the ListView is composed by a ToggleButton and 2 TextViews. At first the ToggleButton wasn't clickable, but I solved by adding:

android:focusableInTouchMode="false"
android:focusable="false"

So, everything works fine, until there are so many items in the list that I need to scroll down to view them. In fact, it works fine until I actually scroll down. It works fine even when there are more items in the ListView but I don't scroll down to see them.

The problem is when scrolling, the items on the ListView are not clickable any more. It won't use the setOnItemClickListener method, and even won't get focus (the orange background).

Can anyone figure out what is the problem?

you'll need to set the listview's xml as follows: android:descendantFocusability="blocksDescendants" then you can click. this is because the toggle button exists in your list item.

This may solve your problem or may not be the answer. You can try this in your custom adapter This will disable the focus (Orange Highlighting).

public CustomAdapterView(Context context,YourModel data)
{
    super( context );
    setOnClickListener((OnClickListener) context);
    setClickable(true);
    setFocusable(false);
}

If you are using notifyDataSetChanged to update the ListView It will not properly work with CustomAdapter. Refer this post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM