Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Vertical scrolling for ListView inside Horizontal scrollView

I have a custom ArrayAdapter for a listView which is inside a horizontal scrollView.The horizontal scrolling works fine but for vertical scrolling I had to do some hacks. I just want to know if its a good idea since listView is already optimized for vertical scrolling.? Is there a way to scroll without this hack ?

The hack basically is to capture touchEvent for scrollView(parent class) and propagate the touchEvent to ListView.

scrolLView.setOnTouchListener(new OnTouchListener(){

        @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
            lv.setSmoothScrollbarEnabled(true);
            lv.dispatchTouchEvent(arg1);
        }
});

This causes scrolling to happen and things work. I just want to know if there are certain more things i need to take in to account.

Thanks

like image 686
hamish Avatar asked Jul 20 '11 21:07

hamish


2 Answers

Your horizontal scroll view is in parent class, so the touch event will be recognized only for the scroll view and not for the list view. So if you want the list view to scroll, the way you did is correct.

like image 196
Mohit Verma Avatar answered Oct 24 '22 00:10

Mohit Verma


In addition to your code, I've modified it to where there is only a ScrollView and multiple ImageView items inside.

ScrollView _sv = (ScrollView)findViewById(R.id.scroller);
_sv.setOnTouchListener(new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        _iv.setScrollContainer(true);
        _iv.dispatchTouchEvent(event);
        return false;
    }
});
like image 39
slybloty Avatar answered Oct 24 '22 00:10

slybloty