我有一个ScrollView环绕我的整个布局,以便整个屏幕可滚动.我在这个ScrollView中的第一个元素是一个HorizontalScrollView块,它具有可以水平滚动的功能.我在horizontalscrollview中添加了一个ontouchlistener来处理触摸事件并强制视图"捕捉"到ACTION_UP事件上最近的图像.
所以我想要的效果就像股票android主屏幕,你可以从一个滚动到另一个,当你举起手指时,它会捕捉到一个屏幕.
这一切都很有效,除了一个问题:我需要从左到右几乎完全水平滑动,以便ACTION_UP进行注册.如果我至少垂直滑动(我认为许多人往往会在他们的手机上左右滑动),我会收到ACTION_CANCEL而不是ACTION_UP.我的理论是,这是因为横向视图滚动视图位于滚动视图内,并且滚动视图正在劫持垂直触摸以允许垂直滚动.
如何在水平滚动视图中禁用滚动视图的触摸事件,但仍允许在滚动视图中的其他位置正常垂直滚动?
这是我的代码示例:
public class HomeFeatureLayout extends HorizontalScrollView {
private ArrayList<ListItem> items = null;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
private static final int SWIPE_MIN_DISTANCE = 5;
private static final int SWIPE_THRESHOLD_VELOCITY = 300;
private int activeFeature = 0;
public HomeFeatureLayout(Context context, ArrayList<ListItem> items){
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setFadingEdgeLength(0);
this.setHorizontalScrollBarEnabled(false);
this.setVerticalScrollBarEnabled(false);
LinearLayout internalWrapper = new LinearLayout(context);
internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(internalWrapper);
this.items = items;
for(int i = 0; i< items.size();i++){
LinearLayout featureLayout = (LinearLayout) View.inflate(this.getContext(),R.layout.homefeature,null);
TextView header = (TextView) …Run Code Online (Sandbox Code Playgroud) android horizontalscrollview ontouchlistener android-scrollview
所以,我有ScrollView一个孩子 - 一个LinearLayout有两个孩子:a TextView和a ViewPager.ViewPager包含许多元素的布局,这就是为什么我需要垂直滚动的能力.只有页面ViewPager可以水平滚动(即:我只想在其中水平滑动ViewPager).那一个TextView不能水平滚动,但应该与我一起滚动ViewPager.
简单?没有.
我在StackOverflow中看到了非常类似的问题(这里,这里,这里和这里).所有建议的解决方案都不适合我:(
我看到的是这个 < - 我的甜蜜UI :),但我无法垂直滚动:(
在ViewPager中嵌入ScrollViews不是一个选项 - UI的设计禁止这样做.
也许这是我用编程方式填充视图寻呼机中的每个页面的东西?嗯...
任何建议,将不胜感激.
我的代码:
activity_main.xml中:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/linearLayoutGeneral"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tvText"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:text="Test text" />
<android.support.v4.view.ViewPager
android:id="@+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</android.support.v4.view.ViewPager>
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
ViewPager中的每个页面都有这样的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" …Run Code Online (Sandbox Code Playgroud) android android-fragments android-scrollview android-viewpager
android ×2