Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android studio中的Tabitem绑定_Java_Android_Android Fragments_Build.gradle_Android Databinding - Fatal编程技术网

Java android studio中的Tabitem绑定

Java android studio中的Tabitem绑定,java,android,android-fragments,build.gradle,android-databinding,Java,Android,Android Fragments,Build.gradle,Android Databinding,嗨,大家!:) 我需要在选项卡式活动(或片段)选项卡项中自定义布局 首先->我为tabitem定义了这个布局: tab\u item\u 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"&

嗨,大家!:)

我需要在选项卡式活动(或片段)选项卡项中自定义布局

首先->我为tabitem定义了这个布局: tab\u item\u 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>
  • 现在,这个绑定实例正在使用中(返回null!!但是为什么?)

    第1项的约束力;这是空的,但我不知道为什么


  • 我需要binding.tabItem1.bindingClassInstanceReturnedItem绑定类实例中的tabitem布局项(tab_item_name_text_视图和tab_item_number_text_视图)和tab_item_布局绑定类。您需要在fragment_surf_base.xml中执行数据绑定,以获得如下绑定:

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

    这可能会对您有所帮助。我知道,我不应该使用
    findviewbyd()
    ,但我还没有办法解决这个问题。如果我得到任何答案,我会更新我的答案

    View tabView = binding.surfTabLayout.getTabAt(0).getCustomView();
    TextView textView = (TextView) tabView.findViewById(R.id.tab_item_name_text_view);
    textView.setText("Hello")
    
    我就是这样做的:

        val tabView = binding.tabLayout.getTabAt(0)?.customView
    
        if (tabView != null){
            val tabBinding = DataBindingUtil.bind<ViewTabWithBadgeBinding>(tabReceived)
            tabBinding?.count = viewModel.tabBadgeCount
            tabBinding?.name = viewModel.tabName
        }
    
    val tabView=binding.tabLayout.getTabAt(0)?.customView
    if(选项卡视图!=null){
    val tabBinding=DataBindingUtil.bind(tabReceived)
    tabBinding?.count=viewModel.tabBadgeCount
    tabBinding?.name=viewModel.tabName
    }
    

    其中“binding”是片段/活动的根绑定。

    我知道这一点。但是我需要tabitem布局项绑定。示例:binding.tabitem1.nameTextView.setText(“asdad”)。编辑:binding.tabItem1为null,但我使用tablayout.getTabAt(0)这不是null,但这是tablayout.tab,我不知道转换为tabitem或其他。
    View tabView = binding.surfTabLayout.getTabAt(0).getCustomView();
    TextView textView = (TextView) tabView.findViewById(R.id.tab_item_name_text_view);
    textView.setText("Hello")
    
        val tabView = binding.tabLayout.getTabAt(0)?.customView
    
        if (tabView != null){
            val tabBinding = DataBindingUtil.bind<ViewTabWithBadgeBinding>(tabReceived)
            tabBinding?.count = viewModel.tabBadgeCount
            tabBinding?.name = viewModel.tabName
        }