【问题标题】:Android How to scroll HorizontalScrollView with scroll of another parellel HorizontalScrollViewAndroid如何滚动HorizontalScrollView与另一个平行HorizontalScrollView的滚动
【发布时间】:2015-03-13 06:49:14
【问题描述】:
【问题讨论】:
标签:
android
scroll
horizontalscrollview
【解决方案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
对我来说效果很好......
【解决方案2】:
<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);
【解决方案3】:
我通过获取滚动更改事件来解决。滚动也很流畅。
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);
}
}
});