Home » Android » android – Passing view id on binding adapter?-Exceptionshub

android – Passing view id on binding adapter?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I have been trying to pass the viewpager id to the binding adapter method. I get and error on this line

setUpViewPager="@{(main_viewpager)}"

I get this error under this ‘}’ symbol on above line

'->' or <expr> expected

This is my xml file

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
    <variable
        name="viewModel"
        type="com.example.project_budget_planner.main.MainViewModel" />

    <variable
        name="adapter"
        type="com.example.project_budget_planner.main.TabBarAdapter" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.MainActivity">

    <com.example.project_budget_planner.shared.NonSwipableViewPager
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        setAdapter="@{adapter}"
        app:layout_constraintBottom_toTopOf="@+id/main_tab_layout"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/main_tab_layout"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        setUpViewPager="@{(main_viewpager)}"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

This is my binding adapter method

 @BindingAdapter("setUpViewPager", "setAdapterForIcons")
    @JvmStatic fun bindViewPagerAndIcons(view: TabLayout, viewPager: ViewPager, tabBarAdapter: TabBarAdapter) {
        view.setupWithViewPager(viewPager)
        for (i in 1 until tabBarAdapter.count) {
            view.getTabAt(i)?.let { it.setIcon(tabBarAdapter.getItem(i).icon) }
        }
    }

How do I solve this?

How to&Answers:

You have to remove your parenthesis and add @id/ to main_viewpager

It will look like this
setUpViewPager="@{@id/main_viewpager}"