Android 如何使用另一个平行的Horizo​​ntalScrollView 滚动Horizo​​ntalScrollView

Android How to scroll HorizontalScrollView with scroll of another parellel HorizontalScrollView

我在顶部 header 视图中有 HorizontalScrollView,我正在以编程方式添加一些与顶部 Horizo​​ntalScrollView 平行的 Horizo​​ntalScrollViews。当我滚动到 ScrollView 顶部时,所有其他的都应该随之滚动,反之亦然。如何做到这一点? Here is image for better understanding

可以将scrollViews设置为onTouchListener,让其他scrollViews跟着滚动。很久以前我也有同样的情况。你可以做这个技巧,例如:

 scrollOne = (HorizontalScrollView)findViewById(R.id.horizontal_one);
 scrollTwo = (HorizontalScrollView)findViewById(R.id.horizontal_two);

 scrollTwo.setOnTouchListener(new OnTouchListener(){

@Override
public boolean onTouch(View view, MotionEvent event) {
    // TODO Auto-generated method stub

    int scrollX = view.getScrollX();
    int scrollY = view.getScrollY();

    scrollOne.scrollTo(scrollX, scrollY);
                return false;
    }

});

那是我对那个问题的解答:listening to scroll events horizontalscrollview android

对我来说效果很好...

<ScrollView 

    android:id="@+id/ScrollView02" 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:layout_below="@+id/one"
            android:layout_marginBottom="51dp"

            >


<TableLayout
    android:id="@+id/videotable"
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"
    android:background="#ffffff"

     android:layout_marginTop="5dp"
     android:cacheColorHint="#00000000"

  />

</ScrollView>

一样动态地将你的子水平滚动视图添加到这个table布局中
pubs = (TableLayout)findViewById(R.id.videotabls);
        pubs.setStretchAllColumns(true);
        pubs.bringToFront();

HorizontalScrollView hz=new HorizontalScrollView(video.this);   
        lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        hz.setBackgroundColor(Color.parseColor("#e6e6e6"));
        hz.setLayoutParams(lp);  
        TableRow tr =  new TableRow(video.this);
tr.setBackgroundColor(Color.parseColor("#e6e6e6"));

        tr.setId(10);

//add all subitems in each layout if reqiuired here

hz.addView(rowlayout);//row layout is a sub layout
        pubs.addView(hz);
        pubs.addView(tr); 

我通过获取滚动更改事件来解决。也流畅滚动。

ArrayList<HorizontalScrollView> scrArrayList=new ArrayList<HorizontalScrollView>();

    for (int j = 0; j < wrapper.data.size(); j++) {
        LayoutInflater inflater1 = this.getLayoutInflater();
        View itemMain = inflater1.inflate(R.layout.item_scedule, null);
        HorizontalScrollView horizontalScrollView = (HorizontalScrollView) itemMain
                .findViewById(R.id.horizontalScrollView1);
        scrArrayList.add(horizontalScrollView);
    }

    mainScrollView.getViewTreeObserver().addOnScrollChangedListener(
            new OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                        for (int i = 0; i < scrArrayList.size(); i++) {
                            int scrollX = scrollView.getScrollX();
                            int scrollY = scrollView.getScrollY();
                            scrArrayList.get(i).scrollTo(scrollX, scrollY);
                        }

                }
            });