0

私はデータバインディングを使用しており、タブアイテムのように見えるのはデータバインディングからnullが返されますが(gameTypesではなく)、それは普通ですか?他のビューも正常に動作しているので、データバインディングの実装に問題はありません。ここにレイアウトファイルの一部があります。データバインディングはnullを返します。それは普通ですか?

  <android.support.design.widget.TabLayout 
      android:id="@+id/gameTypes" 
      android:layout_width="0dp" 
      android:layout_height="48dp" 
      android:background="@android:color/white" 
      > 

      <android.support.design.widget.TabItem 
       android:id="@+id/football" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:tag="football" 
       android:text="Football" 
      /> 

      <android.support.design.widget.TabItem 
       android:id="@+id/basketball" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:tag="basketball" 
       android:text="Basketball"/> 


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

ここでは、選択したタブを検索しようとしているコードです。

private Boolean isSelectedTab(TabItem item, TabLayout.Tab tab) { 
    if (tab.getTag().equals(item.getTag())) 
     return true; 
    return false; 
    } 

エラーです。項目はヌルです。databinding.basketballです。私はそれがTabItemであるので、nullであってはならないと思います。

ありがとうございました。

+0

あなたの質問を編集し、あなたの 'isSelectedTab()'メソッドをどこで呼び出すかを表示することをお勧めします。特に、 'item'値をどこで/どのように取得しているかを表示します。 – CommonsWare

+0

@CommonsWare – MmtBkn

答えて

1

あなたのコードはXMLと整列していないので、おそらく1つの問題です。しかし、TabLayoutsとデータバインディングで問題が発生している可能性があります。

根本原因は、TabLayoutがXMLでTabItemを使用しているように見えますが、実行時にTabに変換されます。これは、内部参照を作成するためにデータバインディングを使用する内部マッピングをねじ込みます。この例では、TabItemインスタンスが間違ったオブジェクトからキャストされていました。あなたの場合、それはちょうどヌルオブジェクトかもしれないのような音。

いずれの場合も、TabLayoutをデータバインディングに使用することは確実にできません。最終的にTabLayoutをラップし、そのデータを直接Tabアイテムに直接アクセスしてデータにバインドするカスタムViewを作成しました。例えば

TabLayoutとレイアウト:

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

<data> 
    <variable name="viewModel" type="com.app.ViewModel" /> 
</data> 

<android.support.design.widget.TabLayout 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:background="@color/white" 
    app:onTabSelectedListener="@{viewModel.onTabSelectedListener}"> 

    <android.support.design.widget.TabItem 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout="@layout/custom_tab_layout"> 

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

    <android.support.design.widget.TabItem 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout="@layout/custom_tab_layout"> 

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

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

し、そのラップカスタムビュー:あなたがTabLayoutを必要とするレイアウトに続いて

public class CustomTabLayout extends FrameLayout { 
    private CustomTabLayoutBinding mBinding; 

    public CustomTabLayout(@NonNull Context context) { 
     this(context, null); 
    } 

    public CustomTabLayout(@NonNull Context context, @Nullable AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public CustomTabLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 

     LayoutInflater inflater = LayoutInflater.from(context); 
     View tabLayout = inflater.inflate(R.layout.custom_tab_layout, this, false); 
     addView(tabLayout); 

     if (!isInEditMode()) { 
      mBinding = CustomTabLayoutBinding.bind(tabLayout); 
     } 
    } 

    public void setViewModel(@Nullable ViewModel viewModel) { 
     mBinding.setViewModel(viewModel); 

     if (viewModel != null) { 
      updateTabAtIndex(viewModel.getFirstTabViewModel(), 0, viewModel.getSelectedIndex()); 
      updateTabAtIndex(viewModel.getSecondTabViewModel(), 1, viewModel.getSelectedIndex()); 
     } 
    } 

    private void updateTabAtIndex(TabViewModel tabViewModel, int index, int selectedIndex) { 
     TabLayout.Tab tab = mBinding.tabLayout.getTabAt(index); 
     if (tab == null) { 
      return; 
     } 

     View customView = tab.getCustomView(); 
     if (customView == null) { 
      return; 
     } 

     if (index == selectedIndex) { 
      tab.select(); 
     } 

     TextView textView = (TextView) customView.findViewById(R.id.title); 
     textView.setText(tabViewModel.getTitleText()); 

     TextView subTitleTV = (TextView) customView.findViewById(R.id.subtitle); 
     subTitleTV.setText(tabViewModel.getSubTitleText()); 
    } 
} 

を、代わりにカスタム表示を使用してください:

<com.app.CustomTabLayout 
     android:id="@+id/custom_tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:elevation="@dimen/default_toolbar_elevation" 
     app:viewModel="@{viewModel.getTabsViewModel}" /> 

ここではMVVMを使用していますが、要望があれば、setViewModel(またはsetMyDataなど)のメソッドを公開することで、カスタムタブレイアウトを使用するデータバインディングを利用できますが、 TabLayoutにあるTabオブジェクトの

希望に役立ちます!

+0

変数の名前を変更したので、間違ったリファクタリングについてです。私はあなたがテーブルレイアウトではなくタブレイアウトをラップするビューを書いたと思います。私が一見することができる例はありますか?私はそれをする方法を知らない。 – MmtBkn

+1

ええ、 'TabLayout'を意味しました。 – dominicoder

0

アイテムは、あなたのコードスニペットに応じないdatabinding.itemBasketball

です。あなたのコードスニペットは、isSelectedTab(b.itemFootballTypes, tab)に電話していることを示しています。あなたのレイアウトXMLにitemFootballTypesという名前のものはありません。ただし、itemFootballタブがあります。

+0

がリファクタリングされていて申し訳ありませんが、問題の原因はありません。 – MmtBkn

関連する問題