我是android开发的新手,我被以下问题卡住了。我已经努力工作了好几天,但还是没能找到解决办法。在这个问题上,我正在解释我究竟采取了哪些步骤来执行。请看看我的程序,并建议一个解决方案或即兴或替代的方式,通过这可以处理。
我想设计一个屏幕,它分为三个部分。我的屏幕结构如下:
整个屏幕的父布局是一个滚动视图。布局文件的大致结构为:
<ScrollView>
<!-- Other Views (Part: 1) -->
<RelativeLayout>
<!-- Part 2 -->
<LinearLayout android:orientation = "vertical">
<LinearLayout android:orientation = "horizontal">
<Button1 /> <Button1 /> </Button3>
</LinearLayout>
<LinearLayout android:orientation = "horizontal">
<Button4 /> <Button5 /> </Button6>
</LinearLayout>
</LinearLayout>
<ImageView>
</RelativeLayout>
<!--Other Views (Part 3) -->
</ScrollView>
感兴趣的部分是中间部分,即相对布局。我想要的东西实现是:
我想在这个RelativeLayout上检测滑动。每次滑动到左边或者我更新了这些按钮上的图片和文字
我希望能够从屏幕的任何地方滚动屏幕
可以点击按钮
现在让我告诉你我做了什么和我面临的问题。为了检测相对布局上的滑动,我在相对布局上设置了一个onTouchListener。重写的onTouch方法是:
public boolean onTouch(View v, MotionEvent event) {
scrollview.requestDisallowInterceptTouchEvent(true);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
return true;
}
case MotionEvent.ACTION_UP: {
if(v instanceof Button){
v.performClick();
}
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// swipe horizontal?
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
this.onLeftToRightSwipe();
return true;
}
if (deltaX > 0) {
this.onRightToLeftSwipe();
return true;
}
}
}
}
return false;
}
由于按钮占据了相对布局上的所有空间,因此我必须设置此侦听器所有的内部线性布局和按钮。为了检测按钮点击并禁止滚动视图拦截,我必须包含scrollview.requestDisallowInterceptTouchEvent(true)
。但问题是,现在我不能滚动这个相对布局。滑动和按钮点击完全工作,因为他们应该,但是,现在它没有检测到滚动。
如果我移除scrollview.requestDisallowInterceptTouchEvent(true)
,则检测到滚动但未检测到滑动。这给了一个非常糟糕的用户体验,总的来说,从所有我展示过应用程序的人那里得到了负面的反馈。所以我想要的是,用户应该能够从屏幕的任何点滚动加上他应该能够滑动相对布局,也能够点击按钮。
请帮忙!可能的解决方案是:
1)实现simpleOnGesture监听器或检测器?推翻投掷事件?请提供有价值和有用的建议。
我建议您使用ViewPager来完成此操作。您可以从以下链接了解更多有关ViewPagers的信息:http://developer.android.com/training/animation/screen-slide.html
在网站上还有一个示例代码,你可以看到它是如何工作的。它可以让你滑动页面,也可以在每页向下滚动。
所以main。xml文件看起来像这样:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment.xml文件看起来像这样:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView android:id="@android:id/text1"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
<TextView style="?android:textAppearanceMedium"
android:lineSpacingMultiplier="1.2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum" />
</LinearLayout>
</ScrollView>