gpt4 book ai didi

android - Android 的双向 ScrollView

转载 作者:太空狗 更新时间:2023-10-29 16:16:10 25 4
gpt4 key购买 nike

我需要一个双向 ScrollView ,其中添加了捏合和缩放手势。是否建议使用带有水平 ScrollView 的 ScrollView 作为其 subview ,或者自定义实现是唯一的解决方案?

最佳答案

严。 Yurkin 回答了类似的问题。您不需要自定义 View ,但必须处理触摸事件。 Yan 的这个解决方案适用于水平 ScrollView 内的垂直 ScrollView :

There is much more simpler solution than creating a custom view:

布局:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/scrollHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ScrollView
android:id="@+id/scrollVertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<WateverViewYouWant/>

</ScrollView>
</HorizontalScrollView>

代码(onCreate/onCreateView):

final HorizontalScrollView hScroll = (HorizontalScrollView) value.findViewById(R.id.scrollHorizontal);
final ScrollView vScroll = (ScrollView) value.findViewById(R.id.scrollVertical);
vScroll.setOnTouchListener(new View.OnTouchListener() { //inner scroll listener
@Override
public boolean onTouch(View v, MotionEvent event) {
return false;
}
});
hScroll.setOnTouchListener(new View.OnTouchListener() { //outer scroll listener
private float mx, my, curX, curY;
private boolean started = false;

@Override
public boolean onTouch(View v, MotionEvent event) {
curX = event.getX();
curY = event.getY();
int dx = (int) (mx - curX);
int dy = (int) (my - curY);
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
if (started) {
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
} else {
started = true;
}
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
vScroll.scrollBy(0, dy);
hScroll.scrollBy(dx, 0);
started = false;
break;
}
return true;
}
});

>

关于android - Android 的双向 ScrollView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26839743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com