【发布时间】:2018-12-31 15:22:22
【问题描述】:
我正在制作记分牌应用程序,它运行良好,但小问题是记分牌在闪烁
举例:
Shikhar Dhawan 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
}
}
}
现在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。
感谢您的帮助。
【问题讨论】:
-
您可以避免在此处使用 runOnUiThread(),因为它是最后一个操作,因此您可以在“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) 一样的方法,这里 i 是它的来源……主要是我从 runonuithread 端获得的。 -
对不起,我很愚蠢:你从来没有在你的“groupWiseScoreGenerate()”方法中使用“array”参数。你必须调用“MatchedScoreFragment.getInstance().setMatchedScoreGroup(array);”!!或者用那个“数组”数据填充你的“matchedScoreGroup”变量。
标签: android android-asynctask android-recyclerview android-runonuithread