Skip to content

DataBoundListAdapter

kaloglu edited this page Jul 12, 2020 · 1 revision

Sample for Implementation DataBoundListAdapter

class TimelineAdapter : DataBoundListAdapter<TweetWithUser>() {

    override fun setBindVariable(viewDataBinding: ViewDataBinding, recylerItem: TweetWithUser) {
        viewDataBinding.setVariable(BR.tweetWithUser, recylerItem)
    }
}

Simple Databinding xml for RecyclerView usage

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

    <data>

        <variable
            name="adapter"
            type="com.zsk.androtweet.adapters.TimelineAdapter" />

        <variable
            name="viewModel"
            type="com.zsk.androtweet.viewmodels.TweetListViewModel" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.fragments.TweetListFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/tweetsRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            binding:adapter="@{adapter}"
            binding:items="@{viewModel.list}"
            binding:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:itemCount="10"
            tools:listitem="@layout/tweets_layout" />

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

Sample layot for recycler view item

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

    <data>

        <variable
            name="tweetWithUser"
            type="com.zsk.androtweet.models.TweetWithUser" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/time_on_tweets"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="end"
                android:layout_marginEnd="5.0dip"
                android:layout_weight="1.0"
                android:gravity="end"
                android:text="@{tweetWithUser.tweet.createdAt}"
                android:textAppearance="?android:textAppearanceSmall"
                android:textSize="12.0sp"
                android:textStyle="bold|italic" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="68.0dip"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tweet_on_tweets"
                    android:layout_width="240.0dip"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2.0dip"
                    android:layout_marginEnd="5.0dip"
                    android:clickable="false"
                    android:linksClickable="false"
                    android:longClickable="false"
                    android:text="@{tweetWithUser.tweet.text}"
                    android:textSize="12sp" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center|top"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/rt_img"
                        android:layout_width="40.0dip"
                        android:layout_height="40.0dip"
                        android:padding="7.0dip"
                        android:src="@drawable/no_retweet" />

                    <TextView
                        android:id="@+id/rtCount_on_tweets"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="start"
                        android:text="@{Integer.toString(tweetWithUser.tweet.favoriteCount)}"
                        android:textSize="9.0sp"
                        android:textStyle="bold" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:gravity="center|top"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/fav_img"
                        android:layout_width="40.0dip"
                        android:layout_height="40.0dip"
                        android:padding="7.0dip"
                        android:src="@drawable/no_favorites" />

                    <TextView
                        android:id="@+id/favCount_on_tweets"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="start"
                        android:text="@{Integer.toString(tweetWithUser.tweet.retweetCount)}"
                        android:textSize="9.0sp"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</layout>
Clone this wiki locally