0

So, i got a fragment with a listview inside the layout file. Above this listview is an imageview with a logo.

Layout

The problem i'm facing is that i can't set the scroll to the entire layout. I have red on other posts that you shouldn't do this, but i have to redo so much code if i must do it in another way.

I'm also getting data from an API, and the amount of elements can change. That's why i'm using a listview.

I don't have a pref regarding layouttype. At the moment I am using the ScrollView layout.

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

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:layout_gravity="center"
    android:adjustViewBounds="true"
    android:src="@drawable/logo"/>

    <ListView
        android:id="@+id/listFemArrangementer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:listSelector="@color/white"
        android:scrollbars="none">

    </ListView>

</LinearLayout>

Does anyone have a good solution for this problem?

Toby
  • 53
  • 7
  • Please add your code as text not as image – AskNilesh Jul 12 '18 at 07:55
  • use android.support.v4.widget.NestedScrollView instead of ScrollView –  Jul 12 '18 at 07:59
  • @Subzero changed the scrollview to nestedscrollview, but it was no change. The list is scrolling by it self, but the image is not scrolling with the list. I would like kind of a static layout that scrolls both the image and the listview at the same time – Toby Jul 12 '18 at 08:05
  • if you don't need scroll why don't use LinearLayout instead of ListView? –  Jul 12 '18 at 08:10
  • Because the amount of elements is not determined, like it could be 2 or it could be 5. I don't know any better way to do this. @Subzero – Toby Jul 12 '18 at 08:19

3 Answers3

0

Try changing the layout xml to:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.HomeFragment"
    android:fillViewport="true">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:src="@drawable/logo"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ListView
            android:id="@+id/listFemArrangementer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:listSelector="@color/white"
            android:scrollbars="none"/>
    </ScrollView>

</LinearLayout>
Susmit Agrawal
  • 3,649
  • 2
  • 13
  • 29
  • ListView inside ScrollView it's "nice" –  Jul 12 '18 at 08:11
  • The problem is not the scrolling on the listview. I want to include the imageview when i'm scrolling so that the whole layout-content is moving. – Toby Jul 12 '18 at 08:15
  • In that case, check this link: https://stackoverflow.com/questions/7611085/disable-scrolling-in-listview – Susmit Agrawal Jul 12 '18 at 08:21
  • Yeah, i saw that post when searching yesterday. I managed to stop the scrolling in the listview, but the lower element wouldn't show. i guess i just have to find a way to display the whole list in one pice. @SusmitAgrawal – Toby Jul 12 '18 at 08:26
0

It is possible to it using NestedScrollView, but first it's always better to use RecyclerView instead of ListView.

Below is a simple xml demonstrating this layout

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.HomeFragment"
    android:fillViewport="true">

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

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:src="@drawable/logo"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/listFemArrangementer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none"/>

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>
seyed Jafari
  • 1,235
  • 10
  • 20
0

Found a solution for my problem.

I made a listviewclass and disabled scrolling etc..

public class ScrollDisabledListView extends ListView {

private int mPosition;

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

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

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

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;

    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Record the position the list the touch landed on
        mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
        return super.dispatchTouchEvent(ev);
    }

    if (actionMasked == MotionEvent.ACTION_MOVE) {
        // Ignore move events
        return true;
    }

    if (actionMasked == MotionEvent.ACTION_UP) {
        // Check if we are still within the same view
        if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
            super.dispatchTouchEvent(ev);
        } else {
            // Clear pressed state, cancel the action
            setPressed(false);
            invalidate();
            return true;
        }
    }

    return super.dispatchTouchEvent(ev);
}

And then i used this class in the layout(scrollview)

<no.packagename.ScrollDisabledListView
            android:id="@+id/listFemArrangementer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:listSelector="@color/white"
            android:scrollbars="none"
            android:padding="0px"
            >
        </no.packagename.ScrollDisabledListView>

And after that i found and used the height of the list from the adapter

listView.setAdapter(adapter);


    if(adapter != null){
        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
Toby
  • 53
  • 7