我试图从ButterKnife迁移到Android数据绑定。
使用ButterKnife时(工作正常):
// Called after a network call
public void updateView(MyAdapter adapter, String title) {
toolbar.setTitle(title);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
使用Android数据绑定:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = MyFragmentBinding.inflate(inflater, container, false);
binding.toolbar.setTitle(R.string.app_name); // this works
return binding.getRoot();
}
// Called after a network call
public void updateView(MyAdapter adapter, String title) {
binding.toolbar.setTitle(title);
binding.recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
binding.recyclerView.setLayoutManager(layoutManager);
binding.recyclerView.setAdapter(adapter);
}
在稍后的代码上调用updateView
后,屏幕变为空白。
我进行了调试,发现视图仍然附加到片段。
此外,如果我正在更新onCreateView
内的内容,那么可以。
我有什么不对的吗?
布局:
<?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">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="@dimen/_6sdp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="@drawable/default_nav_icon_back"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
</android.support.design.widget.CoordinatorLayout>
</layout>