gpt4 book ai didi

android - scrollView Inside Horizo​​ntal Scrollview 无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:58:56 26 4
gpt4 key购买 nike

希望我们玩的开心/p>

当我尝试滚动布局时,只允许我向一个方向滚动,而不是同时向两个方向滚动。我认为这是 android 真正的问题。

请提供任何解决方案。问候

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >

<LinearLayout
android:id="@+id/linearLayout21"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:background="@drawable/top_bar_bg" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="1dip"
android:layout_marginTop="1dip"
android:text=" Image card"
android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>
</LinearLayout>

<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Description"
android:textSize="20sp" >
</TextView>

<EditText
android:id="@+id/IMAGEVIEW_TEXTVEIW"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_below="@+id/textView1"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:gravity="top" >
</EditText>
</RelativeLayout>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Image"
android:textSize="20sp" >
</TextView>
</LinearLayout>

<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginTop="5dip"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical|fill_horizontal" >

<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_margin="5dip" >

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >

<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" >

<ImageView
android:id="@+id/IMAGE_VIEW"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/diamond" >
</ImageView>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>

</LinearLayout>

最佳答案

有比创建自定义 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;
}
});

您可以更改 ScrollView 的顺序。只需更改它们在布局和代码中的顺序。很明显,您放置了想要双向滚动的布局/ View ,而不是 WateverViewYouWant。

关于android - scrollView Inside Horizo​​ntal Scrollview 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7819837/

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