scrollview inside a fragment is not scrolling
Asked Answered
G

5

5

i am new in app development.

i am using 3 tabs. in each fragment i want to display a list of cardview(7-8 cards).

my first fragment.xml

<ScrollView
android:layout_height="fill_parent"
android:layout_width="match_parent"
android:fillViewport="true"

xmlns:android="http://schemas.android.com/apk/res/android" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@color/white">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/crt1"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="2dp"
        card_view:cardCornerRadius="1dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp">

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_3musketeers" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Three Musketeers"
                    android:textSize="25sp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Rare | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Three Musketeers"
        android:textSize="25sp" />

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/crt2"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="2dp"
        card_view:cardCornerRadius="1dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp">

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_archers" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Archers"
                    android:textSize="25sp" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Common | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

when i install this ap it is not scrolling at all.

my fragment.java file

public class TroopsFragment extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    final ScrollView scrollview1 = (ScrollView) inflater.inflate(R.layout.troops,
            container, false);


    CardView cvt1;
    cvt1 = (CardView) scrollview1.findViewById(R.id.crt1);


    cvt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "tushar kalsara",
                    Toast.LENGTH_SHORT).show();
        }
    });
    return scrollview1;
}}
Gapes answered 7/3, 2016 at 10:46 Comment(3)
remove android:orientation="vertical" from linearlayout below ScrollView.Catechumen
after that how can i make my list vertical @amit vaghelaGapes
If you are using Constraint Layout, then, follow this linkTacheometer
F
5

Add this property in ScrollView

android:isScrollContainer="false"

and Make LinearLayout to

wrap_content

Francisco answered 11/3, 2016 at 5:34 Comment(1)
Also add android:fitsSystemWindows="true"Sweptback
B
2

Using a nested scrollview for fragment views does the trick for achieving scrollable views:

<androidx.core.widget.NestedScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical">
Boll answered 20/2, 2019 at 11:25 Comment(0)
W
0

Add this property in ScrollView

android:isScrollContainer="false"

and Make LinerLayout to

wrap_content

Weaverbird answered 7/3, 2016 at 10:54 Comment(0)
T
0

change scrollview's height to wrap content

and its children layouts to wrap content

then it will work

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

<LinearLayout
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="10dp" >

    <android.support.v7.widget.CardView
        android:id="@+id/crt1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        card_view:cardCornerRadius="1dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp" >

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_3musketeers" />

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Three Musketeers"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Rare | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Three Musketeers"
        android:textSize="25sp" />

    <android.support.v7.widget.CardView
        android:id="@+id/crt2"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:clickable="true"
        android:foreground="?attr/selectableItemBackground"
        card_view:cardCornerRadius="1dp"
        card_view:cardElevation="2dp"
        card_view:cardUseCompatPadding="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="3dp" >

            <ImageView
                android:layout_width="85dp"
                android:layout_height="85dp"
                android:src="@drawable/c_archers" />

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Archers"
                    android:textSize="25sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Common | Air + Ground"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

this might work. :)

Tepper answered 7/3, 2016 at 11:3 Comment(1)
please add details to your answer with code snip, else this will just like a commentSawn
D
0

You should never put a scrollable view inside another scrollable view. (in your example you have cardviews in scrollview).

However, if you insist on doing that anyway check this answer. You can set fixed heights on cardviews

It can be done also for listviews. See this blog if you need it

Dentation answered 7/3, 2016 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.