Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add scroll in Fragment

I have implemented Twitter SDK in a List Fragment. It has unlimited scroll just like twitter. But when i try to add this Fragment in a scrollview layout it disables scrollview of twitter fragment.

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

Layout

    <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>

Now i want to add this Fragment in another activity. MainActivity has already a scroll view. when i add this fragment there, the scroll of twitter fragment is gone. I tried various ways, but failed to achieve the target.

I need the same unlimited scroll in the small fragment as well.

This is my MainActivity Code.

<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>
like image 383
dev90 Avatar asked Sep 03 '25 09:09

dev90


1 Answers

you can add a ScrollView in the fragment_layout.xml that wraps the entire layout (a ScrollView normally has one child, in this case the entire layout defined in fragment_layout.xml). The View is then generated normally by the layout inflater

fragment.java :

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

in the following example of a fragment_layout.xml with a ScrollView the ScrollView wraps a ConstraintLayout that contains two 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>

when using this method the LayoutInflater does all the work, one does not have to create an instance of ScrollView in fragment.java (xml based method)

like image 83
ralf htp Avatar answered Sep 04 '25 22:09

ralf htp