androidandroid-asynctaskandroid-recyclerviewandroid-runonuithread

Blinking in Recyclerview list


I am making Scoreboard app and It is working well but small issue is the Scoreboard is blinking

Just for example:

Shikhar Dhawan 86 (126)

Rohit Sharma 20(20)

the whole list is blinking in ms(millisecond)

here is a short code:

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
            }
    }
}

Now 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
    }

In MatchedScoreFragment (fragment) where score is set.

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();
    }
}

it is working well but it is called for every 200 ms so blinking is happening when showing score i.e txtEmptyView.setVisibility(View.VISIBLE); called and scoreboard gone for few ms.I had just shown limited code but you will get easily what is done

problem may be because of runOnUiThread. Appreciate for help thank you.


Solution

  • The problem is that the doInBackground() is called too quick and clears the "matchedScoreGroup" array before the UiThread could process the Runnable. So the solution is: (1) declare "ArrayList<> doInBackground()" and return here your converted/filled ArrayList (2) create the "AsyncTask.onPostExecute(ArrayList<>)" method and run your "MatchedScoreFragment.getInstance().setMatchedScoreGroup()" from there