加载RecyclerView非常慢

时间:2018-03-11 01:27:18

标签: android performance android-recyclerview android-volley recycler-adapter

我的应用程序性能存在问题,我可以在视图中加载数据,但是,生成列表需要两分多钟。这是我的代码优化不佳吗?

以下是我在调用(\d.*)时的活动。

RecyclerView

Json正在以这种格式加载

public class ResultsActivity extends AppCompatActivity {

    private RecyclerView rlist;

    private LinearLayoutManager linearLayoutManager;
    private DividerItemDecoration dividerItemDecoration;
    private List<Riders> ridersList;
    private RecyclerView.Adapter adapter;

    public static Intent makeIntent(Context context) {
        return new Intent(context, ResultsActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);

        rlist = findViewById(R.id.recycler_view);
        ridersList = new ArrayList<>();
        adapter = new RidersAdapter(getApplicationContext(),ridersList);
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        dividerItemDecoration = new DividerItemDecoration(rlist.getContext(),linearLayoutManager.getOrientation());

        rlist.setHasFixedSize(true);
        rlist.setLayoutManager(linearLayoutManager);
        rlist.addItemDecoration(dividerItemDecoration);
        rlist.setAdapter(adapter);

        getData();
    }

    private void getData(){
        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading results...");
        progressDialog.show();

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                for (int i = 0; i < response.length(); i++){
                    try {
                        JSONObject jsonObject = response.getJSONObject(i);

                        Riders riders = new Riders();
                        riders.setName(jsonObject.getString("Full_Name"));
                        riders.setClub(jsonObject.getString("Club"));
                        riders.setFinishPosition(jsonObject.getString("Finish_Position"));

                        ridersList.add(riders);
                    } catch (JSONException e){
                        e.printStackTrace();
                        progressDialog.dismiss();
                    }
                }
            }
        }, new Response.ErrorListener(){

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("Volley", error.toString());
                progressDialog.dismiss();
            }
        });
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonArrayRequest);
    }
}

这只是一个大字符串没有断线或什么?任何帮助都会受到很大的关注。

适配器是 -

"[{"Full_Name":"Sam sam",
"Club":"Club",
"Finish_Position":"1"
},
{  
"Full_Name":"Chris Chris",
"Club":"Club",
"Finish_Position":"2"
}"

1 个答案:

答案 0 :(得分:0)

这不是您问题的正确答案。但是,我想知道你的data_to_insert = [1,2,3,4.5] # store as string arr[52, 7, 12, 12, 44, 54] = ','.join(map(str, data_to_insert)) # retrieve arr[52, 7, 12, 12, 44, 54].decode('utf-8').split(',') 如何更新服务器的响应?我没有看到任何RecyclerView

您需要更改notifyDataSetChanged()功能,如下所示。

onResponse