java - android studio 中的 Tabitem 绑定(bind)

标签 java android android-fragments build.gradle android-databinding

大家好! :)

我需要在选项卡式 Activity (或 fragment )tabitem 中自定义布局。

首先-> 我为 tabitem 定义了这个布局: tab_item_layout.xml

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

<LinearLayout
    android:id="@+id/tab_item_root_linear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tab_item_name_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Wink from"/>
    <TextView
        android:id="@+id/tab_item_number_text_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        tools:text="1"/>
</LinearLayout>

</layout>

Databinding 所需的布局标签.

Second-> 定义了一个tablayout(在 fragment 中但我没有附上这个): fragment_surf_base.xml(这是带有tablayout的 fragment )

<android.support.design.widget.TabLayout
        android:id="@+id/surf_tab_layout"
        android:layout_width="368dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TabItem                
            android:id="@+id/tabItem1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/tab_item_layout" />

        <android.support.design.widget.TabItem                
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/tab_item_layout" />

        <android.support.design.widget.TabItem                
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/tab_item_layout" />

</android.support.design.widget.TabLayout>

我需要使用这些代码在 java 文件中的选项卡项(带绑定(bind)):

  1. 初始化我的绑定(bind)类实例:

        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_surf_base, container, false);
    
  2. 现在,这个绑定(bind)实例正在使用(返回 null !! 但是为什么?):

    绑定(bind).tabItem1;这是空的,但我不知道为什么?

  3. 我需要 tabitem 布局项(tab_item_name_text_view 和 tab_item_number_text_view)和 tab_item_layout 绑定(bind)类实例,由 binding.tabItem1.bindingClassInstanceReturnedItem << - 但我没有:\

对这个问题有什么想法吗? :\ 再见 ! :)

(抱歉我的英语不好)

最佳答案

你需要在fragment_surf_base.xml中进行数据绑定(bind)才能得到如下绑定(bind):

<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="variableName"
            type="packagename.yourViewModel" />
    </data>

<android.support.design.widget.TabLayout
        android:id="@+id/surf_tab_layout"
        android:layout_width="368dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TabItem                
            android:id="@+id/tabItem1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/tab_item_layout" />

        <android.support.design.widget.TabItem                
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/tab_item_layout" />

        <android.support.design.widget.TabItem                
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/tab_item_layout" />

</android.support.design.widget.TabLayout>

</layout>

关于java - android studio 中的 Tabitem 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43339023/

相关文章:

java - 如何通过 TCP 连接发送字节数组(java 编程)

用于附加功能的 Android 应用程序扩展

android - ADB over TCP - 设备已连接并在线但安装显示错误 : Device not found

android - 如何创建带动画的 TextSwitcher

java - 使用 Selenium 3.0 启动 Firefox 46.0.1 时出现 IllegalStateException

java - Vertx : java. sql.SQLSyntaxErrorException:用户缺乏权限或未找到对象:ACHAT

java - Spring 启动 (2.0.4.RELEASE) - IllegalArgumentException : Not a managed type

Android GLSurfaceView透明背景不用setZOrderonTop

android - 拉动刷新recyclerview android

java - 如何在父 fragment Activity 的 viewpager 中的两个列表 fragment 之间进行通信