У меня есть одно приложение активности с нижними вкладками навигации, использующее компонент навигации Android Architecture.
Вот код для одного из фрагментов:
public class ProfileFragment extends BaseFragment {
private final int layout = R.layout.fragment_profile;
//private final int layout = R.layout.fragment_profile_;
private ProfileViewModel mViewModel;
// FragmentProfileBinding mBinding;
public static ProfileFragment newInstance() {
return new ProfileFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(layout, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mViewModel = ViewModelProviders.of(this, viewModelFactory).get(ProfileViewModel.class);
// mBinding = DataBindingUtil.setContentView(getActivity(), layout);
// mBinding.setLifecycleOwner(this);
// mBinding.setVm(mViewModel);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((mcApplication) getActivity().getApplication())
.getApplicationComponent()
.inject(this);
}
}
Имеется 2 возможных версии (одна с привязкой к данным комментируется).
Текущая версия без привязки данных работает правильно, но версия с привязкой данных отображает фрагмент в полноэкранном режиме, а не отображает его в области androidx.navigation.fragment.NavHostFragment моей функции MainActivity.
Привязка данных работает, моя проблема заключается в том, что фрагмент отображает весь экран, а попытка программной навигации по этому фрагменту не работает. Так что привязка данных в этом случае ломает вещи.
Что я делаю не так?
Вот содержимое файла frag_profile_.xml:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<data>
<variable
name="vm"
type="com.synergy.megacampus3.viewmodel.ProfileViewModel" />
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="@={vm.fullUserName}"
android:textColor="@color/mc_text_color_1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/avatar"
tools:textSize="16sp" />
</LinearLayout>
</layout>