あなたのコードは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
オブジェクトの
希望に役立ちます!
あなたの質問を編集し、あなたの 'isSelectedTab()'メソッドをどこで呼び出すかを表示することをお勧めします。特に、 'item'値をどこで/どのように取得しているかを表示します。 – CommonsWare
@CommonsWare – MmtBkn