Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set dynamic layout height in data binding

I want to set dynamic layout height in data binding structure.

I tried something, but I get this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
                  at databinding.ItemGroupCommentsBinding.executeBindings(ItemGroupCommentsBinding.java:147)
                  at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)
                  at view.base.adapter.DataBindingAdapter.onBindViewHolder(DataBindingAdapter.java:32)
                  at view.base.adapter.DataBindingAdapter.onBindViewHolder(DataBindingAdapter.java:15)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
                  at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:556)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
                  at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3254)
                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3767)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:636)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                  at android.view.View.layout(View.java:15604)
                  at android.view.ViewGroup.layout(ViewGroup.java:4966)
                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                  at android.widget.FrameLayout.onLayout(FrameLayout.java:508)

The code is:

Fragment.java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    db = DataBindingUtil.inflate(inflater, R.layout.fragment_group_comments, container, false);

    getSubComponent().inject(this);

    db.setVariable(BR.height, 0);

    // Inflate the layout for this fragment
    return db.getRoot();
}

@BindingAdapter("android:layout_height")
public static void setLayoutHeight(View view, float commentNumber) {
    Timber.e("setLayoutHeight " + commentNumber);

    try {
        GridLayoutManager.LayoutParams layoutParams =
                (GridLayoutManager.LayoutParams) view.getLayoutParams();

        if (10 <= commentNumber && commentNumber < 10) {
            layoutParams.height = 400;
        } else if (11 <= commentNumber && commentNumber < 20) {
            layoutParams.height = 500;
        }

        view.setLayoutParams(layoutParams);
    } catch (Exception ignore) {}
}

item_.xml

<layout>

<data>

    <variable
        name="height"
        type="Integer" />
</data>

<ScalingLayout
    android:id="@+id/view_item"
    style="@style/RelativeLayout_WrapContent"
    android:layout_height="@{height, default=wrap_content}">

    <ImageView
        style="@style/ImageView_WrapContent"
        android:src="@mipmap/red_circle" />

</ScalingLayout>
</layout>
like image 711
Huy Tower Avatar asked Oct 18 '22 17:10

Huy Tower


1 Answers

You need to change commentNumber type from float to Integer or to the int in your binding adapter:

@BindingAdapter("android:layout_height")
public static void setLayoutHeight(View view, Integer commentNumber) {
    // your code staff        
}

Check the example, where @BindingAdapter is used for setting of view's padding.

like image 102
Sergey Bubenshchikov Avatar answered Oct 21 '22 01:10

Sergey Bubenshchikov