0

The scrollview doesn't work for some reason.I have one linear layout inside the scrollview and a listview inside the LinearLayout. What am i doing wrong?

Below is my layout file.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/colorWhite"
    android:layout_height="wrap_content">

<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_movie_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:background="@color/colorWhite"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.netbigs.apps.moviesat.MovieDetail"
android:orientation="vertical">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imView"
    android:layout_gravity="center"

    android:layout_marginTop="8dp"
    android:layout_alignParentTop="true"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvmvname"
    android:paddingTop="10dp"
    android:textColor="@color/colorBg"
    android:textSize="16sp"
    android:textStyle="normal|bold"
    android:hint="Movie Name"
    android:layout_gravity="center"
    android:textAlignment="center"
    android:layout_centerVertical="true"

    android:layout_above="@+id/tvmvdate"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="18dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:layout_gravity="center"
    android:id="@+id/tvmvdate"
    android:textColor="@color/colorBg"
    android:hint="Release Date"
    android:layout_marginBottom="14dp"

    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvmvinfo"
    android:layout_gravity="center"
    android:textColor="@color/colorBg"
    android:textAlignment="center"

    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<com.freeapps.apps.mosoet.NonScrollListView
    android:id="@+id/theatrelist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"


    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="5dp">


 </com.freeapps.apps.mosoet.NonScrollListView>


 </LinearLayout>

 </ScrollView>

NonScrollListView Class to prevent the list view from scrolling

public class NonScrollListView extends ListView {

public NonScrollListView(Context context) {
    super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int   defStyle) {
    super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
            Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
    ViewGroup.LayoutParams params = getLayoutParams();
    params.height = getMeasuredHeight();
}
}
  • Your entire idea is wrong. You should never include a listView inside of a scrollView. That makes no sense, listViews inherently scroll. Re-think what you are trying to achieve. Its obvious that you want some sort of scrolling since you have a scrollView, yet you are trying to prevent your listView from scrolling using a custom "NonScrollListView" class. Huh!? Use normal Listview, remove the scrollView and you will not only see better performance, but your code will actually work. – portfoliobuilder Jan 04 '17 at 19:36
  • I am fetching a list of data from a database,the size can differ.So how else can i list them if not by using listview ? The normal list view when implemented is scrolling by default,and i want to view it expanded. – Neeraj Athamkavil Jan 04 '17 at 19:43
  • Have you considered using a `RecyclerView` inside a `NestedScrollView`? http://stackoverflow.com/questions/31000081/how-to-use-recyclerview-inside-nestedscrollview – PPartisan Jan 04 '17 at 20:10

1 Answers1

1

You can use listview header.

Create a xml file for header part (containing elements that you want to show above listview), let, name of xml file is header.xml

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_movie_detail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:background="@color/colorWhite"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.netbigs.apps.moviesat.MovieDetail"
    android:orientation="vertical">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imView"
    android:layout_gravity="center"
    android:layout_marginTop="8dp"
    android:layout_alignParentTop="true"
    android:layout_centerVertical="true"
    android:layout_alignParentStart="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvmvname"
    android:paddingTop="10dp"
    android:textColor="@color/colorBg"
    android:textSize="16sp"
    android:textStyle="normal|bold"
    android:hint="Movie Name"
    android:layout_gravity="center"
    android:textAlignment="center"
    android:layout_centerVertical="true"
    android:layout_above="@+id/tvmvdate"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="18dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:layout_gravity="center"
    android:id="@+id/tvmvdate"
    android:textColor="@color/colorBg"
    android:hint="Release Date"
    android:layout_marginBottom="14dp"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvmvinfo"
    android:layout_gravity="center"
    android:textColor="@color/colorBg"
    android:textAlignment="center"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
</LinearLayout>

Keep only listview in your current layout.

Set header.xml as listview header,

LayoutInflater myinflater = getLayoutInflater();
ViewGroup myHeader = (ViewGroup)myinflater.inflate(R.layout.headerlayout, myListView, false);

// you need to initialize your header xml elements using myHeader
// tvmvname = (TextView)myHeader.findViewById(R.id.tvmvname);
//and so on...
// you can set text and image here or later after initializing those
myListView.addHeaderView(myHeader, null, false);

***Performance much more better using listview header rather than listview inside scrollview

Exigente05
  • 2,161
  • 3
  • 22
  • 42