Put Button at the bottom of the screen - Android

Asked

Viewed 750 times

0

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

inserir a descrição da imagem aqui

Can anyone help me? Thank you.

1 answer

2


Fala Tiago,

You will need to create an interface class:

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

Once done, 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 will need to change 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 that way in Java, and daclara a Istener:

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 tricky, but it’s the solution to use it on a fragment.

Hugs.

  • I already do a extends Fragment, as I do to give another extend?

  • Speak James, I forgot this detail, sorry, I edited my answer, give a look how to do there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.