Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh android view height when sibling position changed?

I have an Android Layout like this :

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_layout"
        android:orientation="vertical">
<android.support.v7.widget.RecyclerView
            android:id="@+id/rv_weighing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
<android.support.v7.widget.CardView
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/cv_price_holder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

<!-- another view elements here -->
</android.support.v7.widget.CardView>

</LinearLayout>

Basically I want the first element fill the remaining space between parent and the second element. It works fine until I change the Y position of the second element at runtime. I tried to call .invalidate() and .requestLayout() on both parent and the first element but it seems like doesn't work as expected.

I also tried to change the parent layout using <RelativeLayout> and set android:layout_above="@+id/cv_price_holder" on the first element but didn't work as well.

EDIT : Here is the method that invoked to toggle the layout :

public void togglePriceHolder(){
        float newY = mPriceHolderToggleState ? -mPriceHolderToggleLimitY : mPriceHolderToggleLimitY;
        int newIcon = mPriceHolderToggleState ? R.drawable.ic_keyboard_arrow_down_white_36dp : R.drawable.ic_keyboard_arrow_up_white_36dp;
        mBtnTogglePriceHolder.setImageResource(newIcon);
        mCvPriceHolder.animate().translationYBy(newY).setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {}

            @Override
            public void onAnimationEnd(Animator animation) {
                // refresh the layout
                rvWeighing.forceLayout();
                mMainLayout.requestLayout();
            }

            @Override
            public void onAnimationCancel(Animator animation) {}

            @Override
            public void onAnimationRepeat(Animator animation) {}
        });    
        mPriceHolderToggleState = !mPriceHolderToggleState;
}

The question is how can I refresh the layout so the first element can adjust its height when Y position of the second element change ?

Thanks in advance

enter image description here

like image 319
qatz Avatar asked Oct 28 '25 17:10

qatz


1 Answers

Try calling forceLayout() on the views whose dimensions might change and then calling requestLayout().

When requestLayout() is called on a view, all the parents of that view are redrawn, but the siblings are not, calling forceLayout() on a particular view, marks that view for the next layout pass(along with the default traversal).

In your particular case, try calling forceLayout() on the siblings you want to redraw, and then call requestLayout() from one of the views.

like image 111
Sarthak Mittal Avatar answered Oct 31 '25 07:10

Sarthak Mittal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!