1

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?

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130

2 Answers2

0

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.

kevinl
  • 4,194
  • 6
  • 37
  • 55
0

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.

Community
  • 1
  • 1
Mahendran
  • 2,719
  • 5
  • 28
  • 50
  • mahe madhi, thank you for the answer. Just one question, isn't your example just like I set clickable and focusable to true and false in the xml file? I tried that and it didn't work. I think it's not a problem of notifyDataSetChanged, because it works ok until I move the scroll. Anyway, thank you very much. – sheldoncooper Oct 19 '11 at 18:27