java Android:ScrollView 内的 Horizo​​ntalScrollView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12074950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:30:11  来源:igfitidea点击:

Android: HorizontalScrollView inside ScrollView

javaandroid

提问by vladexologija

I have multiple HorizontalScrollViewsinside a ScrollView. Horizontal scroll isn't smooth at all. I have to scroll almost perfectly horizontally for scrolling to work. Is there a simple fix to tweak this ??? Thanks!

我在ScrollView 中有多个Horizo​​ntalScrollViews。水平滚动根本不流畅。我必须几乎完美地水平滚动才能滚动工作。有没有一个简单的修复来调整这个???谢谢!

Multiple HorizontalScrollViews inside a single ScrollView

单个 ScrollView 中的多个 Horizo​​ntalScrollView

采纳答案by Yousef Zakher

You can use Recycler view with Staggered layout manager

您可以将 Recycler 视图与交错布局管理器一起使用

 StaggeredGridLayoutManager  staggeredGridLayoutManager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.HORIZONTAL);

 RecyclerViewAdapter recyclerViewAdapter = newRecyclerViewAdapter(this);

 recyclerView.setAdapter(recyclerViewAdapter); //Don't miss to initialize your adapter

回答by David

This class creates a ScrollView containing a HorizontalScrollView combined into one class. You can put stuff inside it using the AddChild() method. The dispatchTouchEvent overide keeps the scrolling smooth so you can pan around with a single slide of the finger.

此类创建一个包含合并为一个类的 Horizo​​ntalScrollView 的 ScrollView。您可以使用 AddChild() 方法将内容放入其中。dispatchTouchEvent 覆盖保持滚动平滑,因此您可以通过手指的单次滑动来平移。

(I recently used this to wrap a programmatically created TextView)

(我最近用它来包装一个以编程方式创建的 TextView)

class MultiScrollView extends ScrollView
{           
 public HorizontalScrollView hscroll;

 public MultiScrollView ( Context context ) 
 { 
   super( context );
 }

 public void AddChild( View child ) 
 {                                              
   hscroll.addView( child );
 }

 @Override
 public boolean dispatchTouchEvent( MotionEvent event ) 
 {
   hscroll.dispatchTouchEvent(event);
   onTouchEvent(event);
   return true;
 }
}

回答by vladexologija

I've found the solution and still can't believe that this is what you have to do to make this work normal! Just added blank onClickListenerto the each item in the HorizontalScrollView:

我已经找到了解决方案,但仍然无法相信这是您必须做的事情才能使这项工作正常进行!刚刚向Horizo​​ntalScrollView 中的每个项目添加了空白 onClickListener

    item.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    });

After this slide is really smooth, both upwards and downwards.

之后这张幻灯片真的很流畅,无论是向上还是向下。

回答by Ailon V

If you are using the horizontal scroll view solution from (http://www.dev-smart.com/archives/34) the solution for the cross focus problem between the scroll viewand the list viewis blocking the focus to the scroll viewonce you have focus on the list view.

如果您使用的是 ( http://www.dev-smart.com/archives/34) 中的水平滚动视图解决方案,则滚动视图列表视图之间的交叉焦点问题的解决方案会阻塞滚动视图的焦点一旦您专注于列表视图。

From a technical point of view you should add the following line to the onScrollfunction inside the HorizontalListView class.

从技术角度来看,您应该将以下行添加到onScrollHorizo​​ntalListView 类中的函数中。

getParent().requestDisallowInterceptTouchEvent(true);

Hope this helps.

希望这可以帮助。

回答by handhand

While David's answer works, it has a downside. It passes ScrollView's MotionEvent object directly to HorizontalScrollView.onTouchEvent(), so if HorizontalScrollView or its children try to get the event coordinates, they will get the wrong coordinates which based on ScrollView.

虽然大卫的回答有效,但它有一个缺点。它将 ScrollView 的 MotionEvent 对象直接传递给 Horizo​​ntalScrollView.onTouchEvent(),因此如果 Horizo​​ntalScrollView 或其子项尝试获取事件坐标,他们将获取基于 ScrollView 的错误坐标。

My solution:

我的解决方案:

public class CustomScrollView extends ScrollView{

    /*************skip initialization*************/

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e){
        //returning false means ScrollView is not interested at any events,
        //so ScrollView's onTouchEvent() won't be called,
        //and all of the events will be passed to ScrollView's child
        return false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        //manually call ScrollView's onTouchEvent(),
        //the vertical scrolling happens there.
        onTouchEvent(ev);
        //dispatch the event,
        //ScrollView's child will have every event.
        return super.dispatchTouchEvent(ev);
    }
}

Just wrap this CustomScrollView around the HorizontalScrollView in your layout file.

只需将此 CustomScrollView 包裹在布局文件中的 Horizo​​ntalScrollView 周围即可。

回答by Guykun

In general, you shouldn't be using nested ScrollViews in Android at all, the behaviour of scrolling in this way is unnatural too.

一般来说,你根本不应该在 Android 中使用嵌套的 ScrollViews,以这种方式滚动的行为也是不自然的。

You may want to rethink your layout design, is it anything that couldn't be achieved with an expandable list?

您可能想重新考虑您的布局设计,是否有可扩展列表无法实现的功能?