Put Button at the bottom of the screen - Android

0

I have an ExpandableListView and a button that should only appear when someone gives the Scroll on it until the end. I already tried to put it as footer but if the list ends in the middle of the screen the button is in the middle, I need it to be at the end and that only appears when the scroll is given to the end. The image below helps to explain.

Can anyone help me? Thank you.

    
asked by anonymous 05.07.2016 / 16:03

1 answer

2

Speak James,

You will need to create an interface class:

public interface ScrollViewListener {
    void onScrollChanged(ScrollViewExt scrollView, 
                         int x, int y, int oldx, int oldy);
}

Once you've done this, you'll need to create a ScrollView class:

public class ScrollViewExt extends ScrollView {
    private ScrollViewListener scrollViewListener = null;
    public ScrollViewExt(Context context) {
        super(context);
    }

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

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

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
        }
    }
}

There in your layout, you need to change the scrollView:

<br.com.seupackage.ScrollViewExt
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</br.com.seupackage.ScrollViewExt>

You declare it this way in Java, and assign a listener:

ScrollViewExt scroll = (ScrollViewExt) findViewById(R.id.id_scroll);
scroll.setScrollViewListener(this);

And finally, you can use the onScrollChange method:

@Override
    public void onScrollChanged(ScrollViewExt scrollView, int x, int y, int oldx, int oldy) {

        View view = (View) scrollView.getChildAt(scrollView.getChildCount() - 1);
        int diff = (view.getBottom() - (scrollView.getHeight() + scrollView.getScrollY()));

        if (diff <= 10) {
            // Se o diff for menor que 10, exibe o botão

        }
    }

It's a little painful, but it's the solution to using it in a Fragment.

Hugs.

    
05.07.2016 / 17:28