如何在片段中添加滚动

dev*_*v90 3 twitter android listview android-layout android-fragments

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

public class TwitterFragment extends ListFragment {
    public static TwitterFragment newInstance() {
        return new TwitterFragment();
    }
Run Code Online (Sandbox Code Playgroud)

布局

    <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>
Run Code Online (Sandbox Code Playgroud)

现在我想在另一个活动中添加这个片段。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>
Run Code Online (Sandbox Code Playgroud)

ral*_*htp 9

您可以ScrollViewfragment_layout.xml包装整个布局的 中添加一个(一个ScrollView通常有一个子项,在这种情况下是在 中定义的整个布局fragment_layout.xml)。在View随后由布局充气正常产生

片段.java:

View view = inflater.inflate(R.layout.fragment_layout, container, false);
return view;
Run Code Online (Sandbox Code Playgroud)

在以下fragment_layout.xml带有ScrollView的示例中,ScrollView包装了一个ConstraintLayout包含两个 TextViews

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>
Run Code Online (Sandbox Code Playgroud)

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