【问题标题】:Android - scrolling ScrollView in Fragment is impossibleAndroid - 在 Fragment 中滚动 ScrollView 是不可能的
【发布时间】:2015-11-07 18:14:02
【问题描述】:

我在 ViewPager 中有一个可以左右滚动的片段。但每个人都有 ScollView 和 TextView。这个TextView可以有很多文本(代码生成),所以我希望能够垂直滚动它,但是如果我触摸设备,片段更重要,所以我只能水平滑动它。有什么办法可以解决吗? 我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/colectionday_relative_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#123456"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <RelativeLayout
        android:id="@+id/colectionday_relative_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_layout_top" >

        <TextView
            android:id="@+id/colectionday_text_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="5dp"
            android:textSize="@dimen/day_date_text" />

        <ImageView
            android:id="@+id/colectionday_image_favourite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:contentDescription="@string/caution"
            android:src="@drawable/star_gray"
            android:visibility="visible" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/colecionday_relative_content"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/colectionday_button_share"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:fastScrollAlwaysVisible="true"
            android:fastScrollEnabled="true" >

            <TextView
                android:id="@+id/colectionday_text_holidays"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"    
                android:fastScrollAlwaysVisible="true"
                android:fastScrollEnabled="true"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFFFFF"
                android:textSize="@dimen/day_holiday_text" />
        </ScrollView>

        <Button
            android:id="@+id/colectionday_button_share"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/colectionday_adview_bottom_dark"
            android:layout_centerHorizontal="true"
            android:text="@string/share"
            android:textColor="#FFFFFF"
            android:textSize="35sp" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/colectionday_adview_bottom_dark"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-1234567890/1234567890" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/colectionday_adview_bottom_light"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-1234567890/1234567890"
            android:visibility="invisible" />
    </RelativeLayout>

</LinearLayout>

【问题讨论】:

  • “片段可以左右滚动”是什么意思? FragmentViewPager 内吗?
  • @PPartisan 是的,它在 ViewPager 中
  • 在这种情况下,ViewPager 可能会拦截所有滑动事件。您可以通过创建自己的 ViewPager 类来测试这一点,覆盖 onInterceptTouchEvent()/onTouchEvent() 并查看上下滑动会发生什么。有机会我会自己测试一下。
  • @PPartisan 不是这样,在另一个地方我有包含片段的 viewpager,每个都包含 gridview,这可以简单地垂直滚动,或者 viewpager 可以水平滚动。问题没有发生
  • 所有设备snd SDK都会发生这种情况?什么包裹片段本身?

标签: android android-fragments scrollview vertical-scrolling


【解决方案1】:

如果您自己运行重写一些参数,请尝试此代码。

<ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView2"
            android:scrollIndicators="right">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:paddingRight="8dp">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingTop="20dp"
                    android:scrollIndicators="right">


                <FrameLayout>

                </FrameLayout>

            </FrameLayout>
        </ScrollView>

【讨论】:

    【解决方案2】:

    编辑:

    您必须修改滚动视图高度。 wrap_content -> match_parent 你可以添加这个属性

    android:focusable="true"
    android:focusableInTouchMode="true"
    android:descendantFocusability="beforeDescendants"
    

    edit2:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/colectionday_relative_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" // edit
    android:focusable="true" // add
    android:focusableInTouchMode="true" // add
    android:descendantFocusability="beforeDescendants" // add
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
    
    // ... 
    
    </LinearLayout>
    

    【讨论】:

    • fragmentScrollView 表示扩展Fragment的类的对象,仅仅是ViewPager对象的FragmentAdapter? childScrollView 是我要滚动的滚动视图吗?
    • 你的布局的根元素是LinearLayout
    • 再次 - 没有任何改变。
    • 什么是 "android:layout_above="@+id/colectionday_button_share" ? 显示所有布局代码
    • 我加了,查一下。
    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多