Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View margin not working after using databinding

Below is the code of the item layout of gridview. In this, the layout_margin is there in the design panel but when I run the there is no margin. I tried to check other tags like the background and found that it was working. A similar problem is with the layout_weights of linear_layout, the are also not working. These were working fine without data binding. Can someone please help me out in resolving this? '''

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="localEntry"
            type="com.example.lo_cal.data.models.LoCalEntry" />
    </data>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/small"
        android:background="@color/colorAccent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/item_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:itemId="@{localEntry}"
            tools:text="ID" />

        <TextView
            android:id="@+id/item_first_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{localEntry.firstName}"
            tools:text="shashank" />

        <TextView
            android:id="@+id/item_second_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{localEntry.secondName}"
            tools:text="mohabia" />

        <TextView
            android:id="@+id/item_result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{localEntry.result}"
            tools:text="100" />
    </androidx.appcompat.widget.LinearLayoutCompat>
</layout>

'''

like image 454
shashank mohabia Avatar asked Mar 21 '20 19:03

shashank mohabia


People also ask

Can I use both databinding and ViewBinding?

Data binding includes everything that ViewBinding has, so it wasn't designed to work side by side with View binding. The biggest issue is the naming conflict between the generated classes. Both ViewBinding and DataBonding would want to generate the class MainLayoutBinding for the layout main_layout. xml .

Is view binding faster than findViewById?

Databinding is really faster compared to findViewById and setText . Not only performance, It is also much faster and maintainable for mid, full-scale projects. As a side note, benefits include: performance.

How do you pass context in databinding?

You just have to import <import type="android. content. Context" /> in your xml data imports. Then, you can access context for your databinding simply with context .


1 Answers

I faced this problem when inflate the view. Previously,

mBinding = FragmentxxBinding.inflate(inflater);

After I change to

mBinding = FragmentxxBinding.inflate(inflater, container, false);

the problems solved, hope this can help.

like image 106
user2301281 Avatar answered Sep 21 '22 02:09

user2301281