如何将ViewPager与Horizo​​ntalScrollView同步以使其一起移动 [英] How to synchronize ViewPager with HorizontalScrollView to keep them moving together

查看:114
本文介绍了如何将ViewPager与Horizo​​ntalScrollView同步以使其一起移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager和一个Horizo​​ntalScrollView.我将以编程方式将Horizo​​ntalScrollView移至ViewPager的相同位置,因此它们将以这种方式进行操作:当我将ViewPager滚动至页面1时,Horizo​​ntalScrollView会自动移至位置1.我尝试使用ViewPager.OnPageChangeListenersmoothScrollTo (int x, int y) ,但我不了解如何使用最后一种方法:尤其是在这种情况下,我不了解X和Y的含义.这里的代码:

I have a ViewPager and a HorizontalScrollView. I would to move HorizontalScrollView in the same position of ViewPager, programmatically, so I would they behave in this way: when I scroll ViewPager to page 1, HorizontalScrollView moves automatically to position 1. I've tried using ViewPager.OnPageChangeListener and smoothScrollTo (int x, int y), but I don't understand how to use the last method: in particularly, I don't understand what does it mean X and Y in this case. Here the code:

public class MyClass extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    ViewPager viewPager;
    HorizontalScrollView hscroll;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...

        viewPager = (ViewPager) findViewById(R.id.pager);
        hscroll = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
        viewPager.setOnPageChangeListener(this);

        ...

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        hscroll.smoothScrollTo(position-1, position);
        Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

有什么主意吗?每一个帮助将不胜感激,在此先感谢:)

Any idea? Every help would be greatly appreciated, thanks in advance :)

推荐答案

我是用这种方式解决的,虽然不那么精确和精确,但确实有效.由于我没有找到smoothScrollTo的工作方式,因此我尝试使用smoothScrollBy,这与第一个有所不同,因为它可以将horizo​​ntalScrollView滚动一定数量的像素.因此,即,如果我们知道horizo​​ntalScrollView的每个项目的宽度约为72dp,则可以通过编程将dp转换为像素,最后调用smoothScrollBy.您还必须保存最后一页的上一个位置,以便提供正确的方向来滚动horizo​​ntalScrollView.在这里,我展示了如何修改OnPageSelected方法(precPos是一个初始化为0的整数):

I solved in this way, not so elegant and accurate, but it works. Since I've not found a way to get smoothScrollTo working, I tried using smoothScrollBy, that is a bit different from the first because it scrolls the horizontalScrollView by a certain number of pixels. So, i.e. if we know that the width of every item of our horizontalScrollView is about 72dp, we can convert dp to pixels programmatically and finally call smoothScrollBy. You have also to save the previous position of the last page in order to give the right direction to scroll the horizontalScrollView. Here I show how I have modified the OnPageSelected method (precPos is an Integer initialized to 0):

@Override
    public void onPageSelected(final int position) {
        //hscroll.smoothScrollTo(position, 0);
        hscroll.setSmoothScrollingEnabled(true);

        // convert dp to pixels
        Resources r = getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 72, r.getDisplayMetrics());

        // and found the right direction to scroll
        if (position < precPos) hscroll.smoothScrollBy((int) -px, 0);
        else hscroll.smoothScrollBy((int) px, 0);
        // save the last position
        precPos = position;
}

如果您遇到相同的问题,希望对您有所帮助.所以,我以这种方式解决了,无论如何,如果有人可以向我解释我将如何使用smoothScrollTo,您就可以了:)

I hope this helps if you're facing the same problem. So, I solved in this way, anyway if someone could explain me how I would use smoothScrollTo, you're welcome :)

这篇关于如何将ViewPager与Horizo​​ntalScrollView同步以使其一起移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆