Im Batman
Im Batman

Reputation: 1876

Scrollable view with LinearLayout and ListView

I need to display listview and rest of the views after the listview in my Fragment.

First i tried using ScrollView instead of LinearLayout but when im using it ListView only show single row only.

Problem with LinearLayout is it does not scroll entire view. it only scroll through ListView Only. so other views are always hidden from the main view.

i want to make entire view scrollable with expanded listview. so there wont be two scrolling views. how can i do that?

My Fragment Layout is like below.

<FrameLayout>
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ListView>
            /** List View **/
        </ListView>

        <LinearLayout>
            /** Layouts that need to be display after list view **/
        </LinearLayout>

    </LinearLayout>
</FrameLayout>

Upvotes: 0

Views: 668

Answers (4)

Sneha Sarkar
Sneha Sarkar

Reputation: 731

In Xml:

<!--Required to scroll the list view with Linear Layout-->
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <!--Direct child container of scroll view-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <com.example.custom.ScrollableListView
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:choiceMode="singleChoice"
                    android:divider="@color/colorDivider"
                    android:dividerHeight="0.09dp"
                    android:fadingEdge="none"
                    android:listSelector="@color/colorSelectedGreen"
                    android:orientation="vertical"
                    android:scrollbars="none" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <!--Direct child container of scroll view-->

        </ScrollView>
        <!--Required to scroll the list view with Linear Layout-->

Create a ScrollableListView custom class in java:

public class ScrollableListView extends ListView {

    public ScrollableListView(Context context) {
        super(context);
    }

    public ScrollableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollableListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 4, MeasureSpec.AT_MOST));
    }
}

Upvotes: 1

Haimal Khetan
Haimal Khetan

Reputation: 91

You should try with Recycler Views as google now recommends using recycler views. You can put the recycler view inside a nested scroll view something like this :

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="vertical">

    <ImageView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/loader2" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:text="Title"
        android:textColor="@android:color/black"
        android:textSize="26sp"
         />

    <app.so.city.appconstants.CustomTextViews.SourceRegularTextView

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginBottom="-40dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:lineSpacingMultiplier="1.3"
        android:textColor="#CC000000"
        android:textSize="16sp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/article_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:background="#ffffff" />

</LinearLayout>

You can put any number of views inside the nested scroll view.

For the Scrolling to work perfectly and smoothly, you will also need to add the following line of code to java code for recycler views:

recycler_view.setNestedScrollingEnabled(false);

Give it a try and let me know if this works for you as well.

Upvotes: 1

Rishabh Mahatha
Rishabh Mahatha

Reputation: 1271

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


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

        <ListView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/colorAccent">


        </LinearLayout>

    </LinearLayout>

</ScrollView>

try this.

Upvotes: 2

Manohar
Manohar

Reputation: 23384

You can use this list view with scroll view

import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;

public class ExpandableHeightListView extends ListView
{

boolean expanded = false;

public ExpandableHeightListView(Context context)
    {
        super(context);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public ExpandableHeightListView(Context context, AttributeSet attrs,
            int defStyle)
    {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded()
    {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded())
        {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(
                    Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        }
        else
        {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setExpanded(boolean expanded)
    {
        this.expanded = expanded;
    }
}

and in class

ExpandableHeightListView listView = new ExpandableHeightListView(this);
listView.setAdapter(adapter);
listView.setExpanded(true);

in xml

<yourpackagename.ExpandableHeightListView 
      android:layout_width="match_parent"
        android:layout_height="match_parent"
          />

the list view will be expanded fully

Upvotes: 1

Related Questions