【问题标题】:How to add scroll in Fragment如何在片段中添加滚动
【发布时间】:2016-11-12 11:22:56
【问题描述】:

我在列表片段中实现了Twitter SDK。它像推特一样具有无限滚动。但是当我尝试在scrollview layout 中添加这个片段时,它会禁用scrollviewtwitter fragment

public class TwitterFragment extends ListFragment {
    public static TwitterFragment newInstance() {
        return new TwitterFragment();
    }

布局

    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal|center_vertical"
        android:text="No Tweets" />

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="50dp"
        android:layout_weight="1"
        android:divider="#e1e8ed"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="false" />
</LinearLayout>

现在我想在另一个活动中添加这个片段。 MainActivity 已经有一个滚动视图。当我在那里添加这个片段时,推特片段的滚动消失了。我尝试了各种方法,但未能达到目标。

我也需要在小片段中进行同样的无限滚动。

这是我的MainActivity 代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_users_feedback"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.myapp.app.usersreviews.reviews"
    android:background="@drawable/users_bg"
    tools:showIn="@layout/activity_user_reviews">



    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="false"
        android:id="@+id/scroll_view"
        android:layout_below="@+id/sv_users"
        >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            >


            <!-- USER DATA-->
            <include layout="@layout/users_data_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/layout_data"

                />



            <!-- TWITTER-->

            <TextView
                android:id="@+id/textView8"
                android:layout_width="match_parent"
                android:layout_height="33.62dp"
                android:background="#054F92"
                android:gravity="center_vertical"
                android:text="\@tweets"
                android:textColor="@android:color/white"
                android:textSize="16sp"
                android:paddingLeft="15dp"
                android:textStyle="normal|bold" />


                <fragment
                    android:id="@+id/cart_twitter_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="-50dp"
                    android:nestedScrollingEnabled="true"
                    class="com.myapp.app.TwitterFragment"
                    tools:layout="@layout/fragment_twitter" />


            <!-- TWITTER -->


        </LinearLayout> 

    </ScrollView>

</RelativeLayout>

【问题讨论】:

  • 确认一下,用户数据列表是否包含太多需要滚动的数据?
  • @clownba0t ,是的,它有很多数据,我没有在问题中添加所有视图
  • 我的理解是嵌套ScrollViews 通常不被建议,至少部分是因为很难确定它们中的哪一个应该处理任何给定的滑动手势。您似乎遇到了这个问题。我建议更改数据的呈现方式。考虑在您的活动布局中移动ScrollView,以便它只包装用户数据列表。这样你就有两个滚动布局,但它们不是嵌套的。然后,您可以设置片段和滚动视图的宽度、高度、重量等,以实现所需的 UI,同时仍然允许两者滚动。

标签: android android-layout listview android-fragments twitter


【解决方案1】:

您可以在包裹整个布局的fragment_layout.xml 中添加ScrollViewScrollView 通常有一个孩子,在这种情况下,整个布局在fragment_layout.xml 中定义)。 View 然后由布局充气器正常生成

fragment.java:

View view = inflater.inflate(R.layout.fragment_layout, container, false);
return view;

在下面的带有 ScrollViewfragment_layout.xml 示例中,ScrollView 包装了一个包含两个 TextView 的 ConstraintLayout

fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ralf.gpstelephony.TelephonyManagerFragment">


    <TextView
        android:id="@+id/TextView_1"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:text="@string/string1"
        app:layout_constraintTop_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"  />

 <TextView
        android:id="@+id/TextView_2"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:text="@string/string2"
        app:layout_constraintTop_toBottomOf="@id/TextView1"
        app:layout_constraintLeft_toLeftOf="@id/TextView1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="0dp"  />

</android.support.constraint.ConstraintLayout>

</ScrollView>

当使用此方法时,LayoutInflater 完成所有工作,无需在fragment.java(基于 xml 的方法)中创建 ScrollView 的实例

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2017-04-14
    相关资源
    最近更新 更多