Felix
Felix

Reputation: 89576

ListView items not clickable with HorizontalScrollView inside

I have quite a complicated ListView. Each item looks something like this:

> LinearLayout (vertical)
  > LinearLayout (horizontal)
    > include (horizontal LinearLayout with two TextViews)
    > include (ditto)
    > include (ditto)
  > TextView
  > HorizontalScrollView (this guy is my problem)
    > LinearLayout (horizontal)

In my activity, when an item is created (getView() is called) I add dynamic TextViews to the LinearLayout inside the HorizontalScrollView (besides filling the other, simpler stuff out). Amazingly, performance is pretty good.

My problem is that when I added the HorizontalScrollView, my list items became unclickable. They don't get the orange background when clicked and they don't fire the OnItemClickedListener I have set up (to do a simple Log.d call).

How can I make my list items clickable again?


Edit: setting android:descendantFocusability="blocksDescendants" on the topmost LinearLayout seems to work. I'd like to know if there are other ways, though: what if I want focusable items in my list items?

Upvotes: 6

Views: 5911

Answers (3)

android:descendantFocusability="blocksDescendants"

didn't help for me. So

So, i realized listener patern.

public interface EventListItemOnClickListener {
    public void itemClicked();
} 

And in adapter notify all listeners

private List<EventListItemOnClickListener> listeners;
protected void notifyOnClick(int position){
  for(int i=0; i<listeners.size();++i)
    listeners.get(i).itemClicked((Event)events.get(position));
}
...
    @Override
    public void onClick(View v) {
      notifyOnClick(position);
    }
...

Upvotes: 0

when i applied HorizontalScrollView to my TableLayout scrolling is working fine but unable to click on list item My layout is as follwos

Linearlayout HorizontalScrollView TableLayout ......... /TableLayout /HorizontalScrollView /LinearLayout

i applied android:descendantFocusability="blocksDescendants" to my top most linearlayout Any Help

Upvotes: 0

Felix
Felix

Reputation: 89576

Using android:descendantFocusability="blocksDescendants" on the topmost LinearLayout did the trick. Elements inside can still be made "clickable", they're just not focusable (i.e. you can't click them on a non-touchscreen device). Good enough for me.

Upvotes: 9

Related Questions