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

时间:2015-03-13 06:49:15

标签: android scroll horizontalscrollview

我在顶部标题视图中有HorizontalScrollView,我以编程方式添加了一些与顶部Horizo​​ntalScrollView并行的Horizo​​ntalScrollViews。当我滚动顶部ScrollView然后所有其他应该滚动这个,反之亦然。怎么实现这个? Here is image for better understanding

3 个答案:

答案 0 :(得分:1)

您可以将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

对我而言,它运作良好......

答案 1 :(得分:0)

<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>

将子水平滚动视图动态添加到此表格布局中,如

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

答案 2 :(得分:0)

我通过滚动更改事件解决了。也顺利滚动。

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

                }
            });