Android 在Recyclerview列表中闪烁

Android 在Recyclerview列表中闪烁,android,android-asynctask,android-recyclerview,android-runonuithread,Android,Android Asynctask,Android Recyclerview,Android Runonuithread,我正在制作记分板应用程序,它运行良好,但小问题是记分板正在闪烁 举个例子: 什哈尔达旺86(126) 罗希特·夏尔马20(20) 整个列表以毫秒(毫秒)为单位闪烁。 下面是一个短代码: public class handleBetTask extends AsyncTask<JSONObject, Void, Void> { @Override protected Void doInBackground(JSONObject... voids) { t

我正在制作记分板应用程序,它运行良好,但小问题是记分板正在闪烁

举个例子:

什哈尔达旺86(126)

罗希特·夏尔马20(20)

整个列表以毫秒(毫秒)为单位闪烁。

下面是一个短代码

public class handleBetTask extends AsyncTask<JSONObject, Void, Void> {
    @Override
    protected Void doInBackground(JSONObject... voids) {
        try {
            JSONObject response = voids[0];
            JSONArray dataArray = response.getJSONArray("Data");
            if (dataArray.length() > 0) {
                        groupWiseScoreGenerate(dataArray);
                    } else {
                        matchedScoreGroup.clear();//Here matchedScoreGroup is ArrayList
            }
    }
}
在设置分数的匹配分数片段(片段)中。

public void setMatchedScoreGroup(ArrayList<Match> matchedScoreGroup) {
    try {

        if (matchedScoreGroup.size() > 0) {
            if (txtEmptyView.isShown()) {
                txtEmptyView.setVisibility(View.GONE);
            }
            mRecyclerView.setVisibility(View.VISIBLE);
            this.matchedScoreGroup= matchedScoreGroup;
            adapter.notifyDataChanged(this.matchedScoreGroup);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            txtEmptyView.setVisibility(View.VISIBLE);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
public void setMatchedScoreGroup(ArrayList matchedScoreGroup){
试一试{
如果(matchedScoreGroup.size()>0){
if(txtEmptyView.isShown()){
txtEmptyView.setVisibility(View.GONE);
}
mRecyclerView.setVisibility(View.VISIBLE);
this.matchedScoreGroup=matchedScoreGroup;
adapter.notifyDataChanged(this.matchedScoreGroup);
}否则{
mRecyclerView.setVisibility(View.GONE);
txtEmptyView.setVisibility(View.VISIBLE);
}
}捕获(例外e){
e、 printStackTrace();
}
}
它工作正常,但每200ms调用一次,因此在显示分数时会闪烁,即txtEmptyView.setVisibility(View.VISIBLE);打电话和记分牌消失了几毫秒。我刚刚展示了有限的代码,但您将很容易得到所做的事情

问题可能是由于RunUnuithRead。
感谢您的帮助谢谢。

问题是doInBackground()调用太快,在UiThread处理Runnable之前清除了“matchedScoreGroup”数组。因此,解决方案是:(1)声明“ArrayList doInBackground()”并在此处返回转换/填充的ArrayList(2)创建“AsyncTask.onPostExecute(ArrayList)”方法并从那里运行“MatchedScoreFragment.getInstance().setMatchedScoreGroup()”

您可以避免在那里使用runUnuithRead(),因为这是最后一个操作,因此,您可以在“onPostExecute()”中移动整个“groupWiseScoreGenerate()”内容,并删除runOnUiThread()。但是我不确定这是否取决于它。似乎您的“setMatchedScoreGroup(ArrayList)”仅在“ArrayList.size()>0”的情况下调用,因此“mRecyclerView.setVisibility(View.GONE);”永远不会执行。我想问题出在别的地方。您是否仅从那里执行“MatchedScoreFragment.getInstance().setMatchedScoreGroup()”?是的,您是ryt“ArrayList.size()>0”,但问题是我已使用runonuithread登录检查过,它完全正常,但当它转到setMatchedScoreGroup(ArrayList)时有时arraylist会显示为0,这就是它闪烁的原因。还有其他方法吗?我给你写过:你的“setMatchedScoreGroup()”是从其他地方调用的吗?因为从您放在这里的代码来看,当参数为零时,无法执行该方法。所以应该有另一个地方用空的ArrayList调用该方法…是的,它是从3个方面调用的,我测试了与setMatchedScoreGroup(ArrayList matchedScoreGroup,int I)相同的方法,这里我是它的来源…我主要是从runonuithread方面得到的。对不起,我很愚蠢:你从来没有在你的应用程序中使用“array”参数“groupWiseScoreGenerate()”方法。您必须调用“MatchedScoreFragment.getInstance().setMatchedScoreGroup(数组);!!或者用“数组”数据填充“matchedScoreGroup”变量。当代码粘贴到onPostExecute时,该变量无法顺利工作。解决方案已完成,但无法顺利工作。因为图形界面中的更新已完成200毫秒(您说已设置的计时器)+IDE运行命令“onPostExecute()所需的时间“方法。用你以前的方法,你只有200米。唯一的解决方案是在“活动”(或具有图形界面的位置)中创建一个处理程序,并在前面的“onPostExecute()”中向该处理程序发送一个“Handler.sendMessage()”。通过这种方式,您的“onPostExecute()”不做任何工作,但在稍后的过程中,它将全部用于UiThread。我在这个网站的移动版本上,无法格式化代码,但是:(1)在MainActivity(或您有图形界面的地方)创建一个处理程序:“Handler mHandler=新处理程序(Looper.getMainLooper()){@Override public void handleMessage(Message Message){if(Message.what==123)MatchedScoreFragment.getInstance().setMatchedScoreGroup((ArrayList)Message.obj);};“(2)然后在前面的“onPostExecute()”中,只需使用“…mHandler.sendMessage(Message.get(…mHandler,123,0,0,yourArrayList))”向它发送一个“信号”我认为您应该遵循一些关于线程通信的指南,但是您可以遵循我的代码(不同的示例但相同的解决方案):是的,我必须查看线程通信,但现在请告诉我在哪里编写MatchedScoreFragment.getInstance().setMatchedScoreGroup(matchedScoreGroup);在groupwiseScoreGenerate中还是在其他地方?
public void setMatchedScoreGroup(ArrayList<Match> matchedScoreGroup) {
    try {

        if (matchedScoreGroup.size() > 0) {
            if (txtEmptyView.isShown()) {
                txtEmptyView.setVisibility(View.GONE);
            }
            mRecyclerView.setVisibility(View.VISIBLE);
            this.matchedScoreGroup= matchedScoreGroup;
            adapter.notifyDataChanged(this.matchedScoreGroup);
        } else {
            mRecyclerView.setVisibility(View.GONE);
            txtEmptyView.setVisibility(View.VISIBLE);
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}