android - 将垂直滚动添加到android viewPager的一部分

标签 android android-listview viewpage

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

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

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

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

我只想让viewPager中间的部分垂直滚动,而且这个部分是一个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"/>       

请注意上面布局中的 ListView 。

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

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

谢谢。

最佳答案

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

    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;
}
    });

当一个触摸事件发生时,parent 总是去拦截这个事件。这里的父级是 View Pager。您在这里要做的是,当 ListView 上出现触摸时,请求父级不要拦截触摸事件。这样,触摸事件将被 Child(这里是 ListView)接收到,从而相应地滚动。

关于android - 将垂直滚动添加到android viewPager的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18745639/

相关文章:

android - 底部软 NavigationBar 与我的 ListView 重叠

android - 向 ArrayAdapter/ListView 添加不同的元素

android - 在 ViewPager 上强制动画

android: ViewPager 超过 100 个 webview

android - 如何将图像与mp3结合起来看起来像android中的视频

android - 如何在 android 中使用谷歌图像搜索查询启动浏览器

android - 如何在 Android 上创建一个 Circle ListView?

java - 实际示例中的内存泄漏

android - ListView 适配器的页眉和页脚

java - 如何将此自定义 ListView ArrayAdapter 连接在一起?