首页 > 解决方案 > 可完成的未来是否被中断或 buttonView 是否有某种超时

问题描述

我正在使用 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);

insidesetColorsAndCounterAfterReaction方法是发生变化的地方:

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
            }

标签: javaandroidandroid-recyclerviewcompletable-futureui-thread

解决方案


activityInstance.runOnUiThread(()->{setColorsAndCounterAfterReaction(mainActivityReference.userReactionsListLocal, position, holder);});

解决了这个问题,希望有一天它会对某人有所帮助


推荐阅读