Android: scroll down after animating LinearLayout.addview

I want to create a list of items that are in a Scrollview. So I am putting the LinearLayout inside the Scrollview. There is already one element inside linearLayout: a view for adding elements. I want this item to always stay at the bottom of my list. (what is done)

But when I add an item by executing the code below, my item to add views comes out of the screen.

linearLayout.addView(theview, linearLayout.getChildCount() - 1);

      

The views are added animated:

screen.android:animateLayoutChanges="true"

      

So my question is, how do I scroll my scrollView during animation, so my element stays on screen.

Here are some screenshots.
my screenshots

Thanx

+3


source to share


2 answers


After addView you have to call scrollTo or fullScroll method inside the post method.

scrollView.post(new Runnable() {
    @Override
    public void run() {
        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
    }
});

      



ref: How to programmatically scroll a ScrollView to the bottom of the page

+1


source


Implementation for smooth scrolling for ScrollView

after the layout animation ends.

First add the type LayoutTransition

to the code, not the xml

final LayoutTransition layoutTransition = mainLinearLayoutContainer.getLayoutTransition();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
    layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
}
layoutTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, 0);

      

Then create the item LayoutTransition.TransitionListener

to be able to add / remove multiples. Also you need to remove the transitionListener at the end of the translation because the endTransition () callback is called multiple times and it calls smoothScroll.



final LayoutTransition.TransitionListener transitionListener = new LayoutTransition.TransitionListener() 
{
    @Override
    public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {}

    @Override
    public void endTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) 
    {
        // Remove TransitionListener because endTransition is called multiple times and it brokes smoothScroll
        layoutTransition.removeTransitionListener(this);
        scrollContainer.post(new Runnable() 
        {
            @Override
            public void run() {
                scrollContainer.smoothScrollTo(0, scrollContainer.getBottom());
            }
        });
    }
};

      

Finnaly adds a starting point for the animation.

button.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v) 
    {
        if (licenseLabel.getVisibility() == View.GONE) 
        {
            layoutTransition.addTransitionListener(transitionListener);
            button.setVisibility(View.VISIBLE);
        } 
        else {
            licenseLabel.setVisibility(View.GONE);
        }
    }
});

      

0


source







All Articles