Pass motion event to parent Scrollview when Listview at top/bottom
Asked Answered
J

3

9

I have a ListView in a ScrollView to show comments and I would like to do the following:

When the user swipes down, first the ScrollView should fully scroll down as the list is at the bottom. Once it's fully down, the Listiew should start scrolling.

Similarly, when the user is scrolling up, first the ListView (order reversed here!) should scroll up, before the ScrollView starts scrolling.

So far I have done the following:

listView.setOnTouchListener(new View.OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // If going up but the list is already at up, return false indicating we did not consume it.
            if(event.getAction() == MotionEvent.ACTION_UP) {
                if (listView.getChildCount() == 0 && listView.getChildAt(0).getTop() == 0) {
                    Log.e("Listview", "At top!");
                    return false;
                }
            }

            // Similar behaviour but when going down check if we are at the bottom.
            if( event.getAction() == MotionEvent.ACTION_DOWN) {
                if (listView.getLastVisiblePosition() == listView.getAdapter().getCount() - 1 &&
                        listView.getChildAt(listView.getChildCount() - 1).getBottom() <= listView.getHeight()) {
                    Log.e("Listview","At bottom!");
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    return false;
                }
            }
            v.getParent().requestDisallowInterceptTouchEvent(true);
            return false;
        }
    });

The logs trigger at the right moment, however the ScrollView won't move even though I return false.

I also tried to add v.getParent().requestDisallowInterceptTouchEvent(false); to the statements, but that did not work either.

How can I make it work?

Jibe answered 17/9, 2015 at 13:28 Comment(3)
I've tried doing this (nesting a listview in my scrollview) and I couldn't get it to work. I ended up using two NestedScrollViewAryan
I looked at the NestedScrollView class too, but as pointed at on other threads listviews do have some additional functions over for example a LinearLayout inside a scrollviewJibe
Generally I would recommend using a header view in a ListView, instead of nesting a ListView inside a ScrollView - Nested scroll containers pre-RecyclerView is somewhat painful.Kiakiah
I
8

You can create a custom ScrollView and ListView and override

onInterceptTouchEvent(MotionEvent event)

like this:

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    View view = (View) getChildAt(getChildCount()-1);
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    if( event.getAction() == MotionEvent.ACTION_DOWN) {
        if (diff ==0) {
            return false;
        }
    }

    return super.onInterceptTouchEvent(event);
}

This way every Down Touch on the ListView while you are at the bottom of ScrollView will go to the ListView.

This is just a sketch implementation but I think you could get started from this by doing the same thing to detect when you are at the top of the ListView

As a note I would try an easier implementation by using just a ListView with the current content of you ScrollView added as the header of ListView by using listView.addHeaderView(scrollViewContent). I don't know if this fits your needs.

EDIT:

Detecting when should start scrolling the ScrollView. Keeping a reference of ListView in the ScrollView. When the user is scrolling up and the the ListView is at the top let the event be consumed by the ScrollView.

private void init(){
    ViewConfiguration vc = ViewConfiguration.get(getContext());
    mTouchSlop = vc.getScaledTouchSlop();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    final int action = MotionEventCompat.getActionMasked(event);

    switch (action) {
        case MotionEvent.ACTION_DOWN:{
            yPrec = event.getY();
        }
        case MotionEvent.ACTION_MOVE: {
            final float dy = event.getY() - yPrec;

            if (dy > mTouchSlop) {
                // Start scrolling!
                mIsScrolling = true;
            }
            break;
        }
    }

    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mIsScrolling = false;
    }

    // Calculate the scrolldiff
    View view = (View) getChildAt(getChildCount()-1);
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

        if ((!mIsScrolling || !listViewReference.listIsAtTop()) && diff == 0) {
            if (event.getAction() == MotionEvent.ACTION_MOVE) {
                return false;
            }
    }

    return super.onInterceptTouchEvent(event);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mIsScrolling = false;
    }
    return super.onTouchEvent(ev);
}


public void setListViewReference(MyListView listViewReference) {
    this.listViewReference = listViewReference;
}

The method listIsAtTop in ListView looks like this :

public boolean listIsAtTop()   {
    if(getChildCount() == 0) return true;
    return (getChildAt(0).getTop() == 0 && getFirstVisiblePosition() ==0);
}
Intoxicate answered 28/9, 2015 at 20:46 Comment(2)
The problem with this approach is that there is a circular dependency between the listview and the scrollview. I tried implementing something with the touch intercept method, but as the order is reversed when scrolling up, the scrollview has to "check" if the listview is done first.Jibe
Check out my edit to the answer. I made a test and it works but i am not sure this is what you are trying to achieve. You just have to set a reference to the listview in the scrollview. The code may require some extra tweaking and clean up.Intoxicate
T
4

You should not put ListView inside ScrollView, but you can achieve your requirement by using ExpandableHeightListView. This will make full height Listview inside ScrollView. No need add TouchListener.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
             <!-- your content -->
        </RelativeLayout>

        <my.widget.ExpandableHeightListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>

And another way to achieve your requirement is RecyclerView with header.

Trixie answered 28/9, 2015 at 5:47 Comment(1)
While it does work, it is not really what I asked. This just shows all the items below the content, might as well throw views inside a LinearLayout then.Jibe
E
0

Try This:

  1. First find if you reached at scrollview Bottom. Register the onScrollChanged to your Scroll view

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) 
    {
        // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
        View view = (View) getChildAt(getChildCount()-1);
    
        // Calculate the scrolldiff
        int diff = (view.getBottom()-(getHeight()+getScrollY()));
    
        // if diff is zero, then the bottom has been reached
        if( diff == 0 )
        {
            // notify that we have reached the bottom
            // Add code here to start scrolling the ListView
            Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
        }
    
        super.onScrollChanged(l, t, oldl, oldt);
    }
    
  2. Check if you reach at the Top of ListView. Check if firstVisibleItem is 0:-

    tableListView.setOnScrollListener(new OnScrollListener() {
    
            public void onScrollStateChanged(AbsListView view, int scrollState) {
    
            }
    
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if (visibleItemCount == 0)
                // you have reached at top of lustView
                {
                    java.lang.System.out.println("you have reached at top of lustView!");
                }
                else {
                    if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
                        // put your stuff here
                        java.lang.System.out.println("end of the line reached!");
                    }
                }
    
            }
        });
    

I hope this works for you. If face any issues, please let me know.

Eileneeilis answered 22/9, 2015 at 15:59 Comment(1)
Detecting if I reached the top or bottom is what my code already does, I want however to pass motion events to the parent scrollview depending on this state.Jibe

© 2022 - 2024 — McMap. All rights reserved.