问题描述
我正在使用 CF 来获取练习列表。当用户单击按钮时,文本变为灰色并且按钮不再可单击:
所以按钮变成灰色但 setEnabled(false);
调用导致问题,当我设置断点
当要执行 setEnabled(false);
行时,调试器只是停止并且程序继续运行,就好像线程被中断并且控制权交还给主线程(或 UI 线程),当我点击并回来然后 setEnabled(false);
也被执行并且按钮不再可点击,按钮在执行点击后也不可点击但视图根本没有更新所以它可以可见,那么为什么“setTextColor (Color.GRAY); " 不会有任何问题,但 setEnabled 会。
这是 CF 调用
client.userReacted(exercises.get(position),reaction,mainActivityReference.userReactionsListLocal,mainActivityReference.reactionsThread)
.thenAcceptAsync((reactionMap)->{
if (reactionMap!=null){
mainActivityReference.userReactionsListLocal=reactionMap;
setColorsAndCounterafterReaction(mainActivityReference.userReactionsListLocal,position,holder);
CompletableFuture.runAsync(()->{LoggedInUserHolder.updateOrSaveReactionListLocally(mainActivityReference.userReactionsListLocal);});
}
else
Helpers.msgTodisplayAsSnackbar(mainActivityReference.findViewById(android.R.id.content),"Es tut uns leid,aber aufgrund Verbindungsproblemen ist die Abgabe einer Reaktion nicht möglich",3000,7,Gravity.TOP);
},mainActivityReference.reactionsThread);
在 setColorsAndCounterafterReaction
方法内部是发生变化的地方:
if(reaction.contains(REACT_FAVORITE_KEY)){
holder.to_favorite.setTextColor(Color.GRAY);
holder.to_favorite.setEnabled(false);-> the buttonView is not updated,have to exit the view and come back to see the change
}
解决方法
activityInstance.runOnUiThread(()->{setColorsAndCounterAfterReaction(mainActivityReference.userReactionsListLocal,position,holder);});
解决了问题,希望有一天能帮到人