由于底部工作表片段没有显示,我最初认为问题出在我的 Java 代码 \xe2\x80\x94 中,可能是适配器中的设置不正确,使用了错误的片段管理器等 \xe2\x80\x94 而不是 XML 中布局。经过几天的沮丧,我终于找到了罪魁祸首。
\n<androidx.coordinatorlayout.widget.CoordinatorLayout\n android:layout_width="match_parent"\n android:layout_height="match_parent">\n\n <androidx.fragment.app.FragmentContainerView\n android:id="@+id/nav_host_fragment"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n android:layout_marginBottom="?android:attr/actionBarSize"\n android:name="androidx.navigation.fragment.NavHostFragment"\n app:defaultNavHost="true"\n app:navGraph="@navigation/analytics"\n app:layout_behavior="@string/appbar_scrolling_view_behavior"/>\n\n <androidx.constraintlayout.widget.ConstraintLayout\n android:id="@+id/bottom_sheet"\n android:layout_width="match_parent"\n android:layout_height="match_parent"\n android:background="@color/color_surface"\n android:elevation="@dimen/size_2"\n app:behavior_peekHeight="?android:attr/actionBarSize"\n app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">\n\n <com.google.android.material.tabs.TabLayout\n android:id="@+id/tabs"\n android:layout_width="match_parent"\n android:layout_height="?android:attr/actionBarSize"/>\n\n <androidx.viewpager.widget.ViewPager\n android:id="@+id/pager"\n android:layout_width="match_parent"\n android:layout_height="0dp"\n app:layout_constraintBottom_toBottomOf="parent"\n app:layout_constraintStart_toStartOf="parent"\n app:layout_constraintTop_toBottomOf="@id/tabs"/>\n </androidx.constraintlayout.widget.ConstraintLayout>\n</androidx.coordinatorlayout.widget.CoordinatorLayout>\nRun Code Online (Sandbox Code Playgroud)\n当底部工作表折叠时,其选项卡及其标签可见(在 处peekHeight),我可以点击它们。但是,当底部工作表展开时,选项卡仍然可见,但其片段中没有显示任何内容。我已经确定,如果我注释掉\xe2\x80\x94 ,如果我使用旧视图而不是 \xe2\x80\x94 ,底部工作表将按预期工作,所有选项卡中的所有内容都FragmentContainerView无关紧要fragment正在显示。
发生什么了?
\n我正在使用RxJava2和Room SQLite.我的DAO:
@Dao
public interface HeroDao {
@Insert
long create(Hero hero);
}
Run Code Online (Sandbox Code Playgroud)
这就是我在RxJava2中使用它的方式:
Observable.just((int) heroDao.create(hero))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(id -> /** do stuff **/);
Run Code Online (Sandbox Code Playgroud)
但是当我运行应用程序时,我在Logcat中得到的错误是Cannot access database on the main thread since it may potentially lock the UI for a long period of time.我已经尝试附加.allowMainThreadQueries()到数据库构建器并且插入经过以确认我的可观察的正确性.看来LiveData和AsyncTasks是我可以尝试的其他方法,但是当我已经对RxJava2进行了大量投资时,我宁愿不去那里.
那么为什么我会收到这个错误?是否subscribeOn(Schedulers.io())足以卸下主线程的工作?否则我该怎么做?似乎有一些细微之处在于将房间查询转换为我遗漏的可观察对象?
想象一个 StackOverflow 问题的列表,这些问题可以单独被赞成和反对。他们每个人都有一个计数,显示其投票数。单击 upvote 将该计数加一,单击 downvote 将该计数减一。这就是我要实现的目标的总体思路。rv_item.xmlRecyclerView 中单个项目的布局(为简洁起见):
data>
<variable
name="item"
type="com.mycodez.Item"/>
<variable
name="vm"
type="com.mycodez.ListViewModel" />
</data>
<Button
android:id="@+id/decrease_quantity"
android:onClick="@{() -> vm.decrementItemQuantity(item)}"/>
<TextView
android:id="@+id/quantity"
android:text="@{String.valueOf(item.quantity)}"
tools:text="1" />
<Button
android:id="@+id/increase_quantity"
android:onClick="@{() -> vm.incrementItemQuantity(item)}"/>
Run Code Online (Sandbox Code Playgroud)
Where@+id/decrease_quantity类似于downvote,@+id/quantity类似于投票计数,@+id/increase_quantity类似于upvote。适配器:
class ListRvAdapter extends RecyclerView.Adapter<ListRvAdapter.ViewHolder> {
private List<Item> items;
private ListViewModel viewModel;
private LifecycleOwner lifecycleOwner;
ListRvAdapter(List<Item> items, ListViewModel viewModel, LifecycleOwner lifecycleOwner) {
this.items = items;
this.viewModel = viewModel;
this.lifecycleOwner = lifecycleOwner;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int …Run Code Online (Sandbox Code Playgroud) android android-databinding android-livedata android-viewmodel