我正在制作记分板应用程序,并且运行良好,但小问题是记分板闪烁
例如:
Shikhar Dhawan 86(126)
Rohit Sharma 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
}
}
}
现在 groupWiseScoreGenerate(dataArray):
private void groupWiseScoreGenerate(JSONArray array) {
matchedScoreGroup.clear();
//Here is a data insertion in matchedScoreGroup
runOnUiThread(new Runnable() {
@Override
public void run() {
MatchedScoreFragment.getInstance().setMatchedScoreGroup(matchedScoreGroup);//where it is bind to recyclerview
}
在设置分数的 MatchedScoreFragment(片段)中。
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();
}
}
它运行良好,但是每200毫秒调用一次,因此在显示分数时即发生闪烁,例如txtEmptyView.setVisibility(View.VISIBLE);叫了,记分牌消失了几毫秒。。我刚刚显示了有限的代码,但是您会很容易地完成操作
问题可能是由于runOnUiThread引起的。 感谢您的帮助。
答案 0 :(得分:1)
问题在于doiBackground()调用得太快,并且在UiThread可以处理Runnable之前清除了“ matchedScoreGroup”数组。因此,解决方案是:(1)声明“ ArrayList <> doInBackground()”并返回此处转换/填充的ArrayList(2)创建“ AsyncTask.onPostExecute(ArrayList <>)”方法并运行“ MatchedScoreFragment.getInstance()” .setMatchedScoreGroup()”从那里