简体   繁体   中英

Passing view id on binding adapter?

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?

Use mainViewpager like

setUpViewPager="@{mainViewpager}"

It will work

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

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

I tried some things and figured it out. To pass the id of a view which is present in the same layout using data binding, this is how: @{main_viewpager}

    <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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM