简体   繁体   English

Android 水平滚动视图内 ListView 的垂直滚动

[英]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.我有一个用于列表视图的自定义 ArrayAdapter,它位于水平滚动视图内。水平滚动工作正常,但对于垂直滚动,我不得不做一些技巧。 I just want to know if its a good idea since listView is already optimized for vertical scrolling.?我只想知道这是否是个好主意,因为 listView 已经针对垂直滚动进行了优化。? 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. hack 基本上是为 scrollView(父类)捕获 touchEvent 并将 touchEvent 传播到 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谢谢

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.您的水平滚动视图位于父 class 中,因此只能识别滚动视图而不识别列表视图的触摸事件。 So if you want the list view to scroll, the way you did is correct.因此,如果您希望列表视图滚动,您的做法是正确的。

In addition to your code, I've modified it to where there is only a ScrollView and multiple ImageView items inside.除了您的代码之外,我还对其进行了修改,使其内部只有一个 ScrollView 和多个 ImageView 项目。

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;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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