【问题标题】:Add vertical scrolling to a section of android viewPager将垂直滚动添加到android viewPager的一部分
【发布时间】:2013-09-11 15:41:56
【问题描述】:

我有一个使用 FragmentStatePagerAdapter 填充的 viewPager。它工作正常,可以水平浏览填充的页面。

然而viewPager上每个页面的中间部分是由一个listView组成的。这个列表视图有时不适合指定区域,我需要能够垂直滑动。但我目前无法在 viewPager 的这一部分垂直滚动。

看起来 viewPager 正在消耗所有滚动事件(水平和垂直)。

我不想要垂直 viewPager;我在 StackOverflow 上看到了一些答案。

我只希望viewPager中间的section垂直滚动,而这个section是listView

这是带有 viewPager 的选项卡的主布局:

           <LinearLayout 
            android:id="@+id/history"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:background="@color/fdi_gray">
            <android.support.v4.view.ViewPager
                 android:id="@+id/historyFragment_container"
                 android:layout_weight="0.85"
                 android:layout_width="fill_parent"
                 android:layout_height="0dip"/> 
             <include layout="@layout/page_indicator_history"
                android:layout_height="0dip"
                android:layout_width="fill_parent"
                android:layout_weight="0.05"
                android:background="@color/darker_gray"                 
                android:layout_marginTop="2dp"/>
       </LinearLayout>

ViewPager 适配器用来填充上面 viewPager 的布局是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spaceHolderLocations"> 
<TextView
    android:id="@+id/locationName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"
    android:textStyle="bold"
    android:textSize="17sp"/>
 <TextView
    android:id="@+id/latlon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_below="@+id/locationName"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="3dp"
    android:layout_centerHorizontal="true"/>

 <View
     android:id="@+id/listViewStart"
     android:layout_width="match_parent"
     android:layout_height="0.5dip"
     android:layout_marginLeft="3dp"
     android:layout_marginRight="3dp"
     android:layout_below="@+id/latlon"          
     android:background="@color/white" />

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/fdi_gray"
    android:layout_below="@+id/listViewStart"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:divider="@color/white"
    android:dividerHeight="0.5dip"
    android:padding="1dp">       
</ListView>  
<View
     android:id="@+id/listViewEnd"
     android:layout_width="match_parent"
     android:layout_height="0.5dip"
     android:layout_marginLeft="3dp"
     android:layout_marginRight="3dp"
     android:layout_below="@android:id/list"         
     android:background="@color/white" /> 
<TextView
    android:id="@+id/curingLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/listViewEnd"
    android:gravity="center"
    android:text="@string/curingLabel"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="30dp"        
    android:textSize="17sp"/>
 <TextView
    android:id="@+id/curingValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/curingLabel"
    android:layout_marginLeft="30dp"
    android:layout_alignBottom="@+id/curingLabel"
    android:textStyle="bold"        
    android:textSize="17sp"/>       

请注意上述布局中的列表视图。

这是我希望能够滚动的 ViewPager 部分;但不能,如果 listView 内容不适合页面,我只能水平滚动页面而不是垂直滚动。

这个问题很接近,但不是我要找的:link

谢谢。

【问题讨论】:

  • 它应该可以开箱即用,请在此处输入一些代码...
  • 我已经添加了我正在使用的布局。谢谢。
  • 尝试从单个页面视图中删除所有文本视图,只保留android:layout_height="match_parent" 的列表视图,看看是否可行
  • 我已经删除了包含listView的xml文件中的所有textView,但是listView项目上的垂直滚动仍然不起作用。您是否认为 ViewPager 中的包含布局导致了我的问题……但我需要它,它包含页码指示器形状。只有线性布局的 0.85 部分应该垂直滚动。

标签: android android-listview viewpage


【解决方案1】:

实现以下功能。希望对你有帮助。

    mList = (ListView) findViewById(R.id.list);
    mList.setOnTouchListener(new View.OnTouchListener () {

    @Override
            public boolean onTouch(View v, MotionEvent event) {
                mList.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
}
    });

当触摸事件发生时,父母总是会拦截该事件。这里的父级是 View Pager。您在这里尝试做的是,当列表视图出现触摸时,请求父级不要拦截触摸事件。这样,触摸事件就会被Child接收到,这里是ListView,并相应地滚动。

【讨论】:

  • 好伙伴。这是我正在寻找的确切答案。
  • 我有图像视图,在横向模式下我需要同样的方式。我只想滚动孩子(imageview)来查看内容,但在横向模式下它实际上滚动了整个页面,知道吗?
【解决方案2】:

基本上,您不能将列表视图的高度设置为“包装内容”。给出一个固定的高度,或者选择一个不同的布局,比如线性布局并将其权重设置为 1 将是另一种解决方案。如果您将其设置为包装内容,它只会占用所有空间并超出屏幕。

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 2016-03-10
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 2019-10-20
    相关资源
    最近更新 更多