简体   繁体   English

notifyItemInsertedRange()无法处理recyclerview适配器

[英]notifyItemInsertedRange() not working on recyclerview adapter

I used recyclerview on my project. 我在我的项目上使用了recyclerview。 I have a problem updating the data. 我在更新数据时遇到问题。 I want to use notifyItemRangeInserted() or notifyItemInserted() method but none of them are working(the arraylist are updated, but the new item is not showing in the recyclerview). 我想使用notifyItemRangeInserted()或notifyItemInserted()方法,但它们都没有工作(arraylist已更新,但新项目未显示在recyclerview中)。 It's really confusing because notifyDataSetChanged() is works. 它真的很混乱,因为notifyDataSetChanged()是有效的。

I have tried both notifyItemInserted(index) and notifyItemRangeInserted(startIndex, insertedCount) but none of them works. 我已经尝试了notifyItemInserted(index)和notifyItemRangeInserted(startIndex,insertedCount)但它们都不起作用。 I also have googled this problem but I didn't find a similar problem. 我也搜索过这个问题,但我没有发现类似的问题。

RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest postRequest = new StringRequest(Request.Method.POST, UrlHouse.MOMENT_EXTENTION,
                response -> {
                    // response
                    Log.e("moment_extention", response);
                    try {
                        JSONArray momentArray = new JSONArray(response);
                        CustomJsonParser jsonParser = new CustomJsonParser(this);
                        for(int i = 0; i < momentArray.length(); i++){
                            JSONObject c = momentArray.getJSONObject(i);
                            momentArrayList.add(jsonParser.parseMomentData(c));
                        }
                        if (momentArray.length() > 0){
                            runOnUiThread(() -> {
                                Log.e("item_count", String.valueOf(mAdapter.getItemCount()));
                                //mAdapter.notifyDataSetChanged();
                                //mAdapter.notifyItemInserted(10);
                                mAdapter.notifyItemRangeInserted(momentArrayList.size() - momentArray.length(), momentArray.length());
                            });
                        }
                        if (momentArray.length()  < 10){
                            stopLoading = true;
                            extentionLoading.setVisibility(View.GONE);
                        }else{
                            extentionLoading.setVisibility(View.VISIBLE);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                },
                error -> {
                    // error
                }
        ) {
            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("key_owner", userData.getUserId());
                params.put("session_key", userData.getLoginKey());
                params.put("user_id", userData.getUserId());
                params.put("excluded_post", getExcludedPost());
                return params;
            }
        };
        postRequest.setRetryPolicy(new DefaultRetryPolicy(24 * 60 * 60 * 1000, 0, 1f) );
        queue.add(postRequest);

I want the recyclerview item updated the new data. 我希望recyclerview项目更新新数据。 in my case, after updating the adapter it should be 20 item displayed on the screen(10 the original item and 10 is the new item), but instead of displaying 20 item its only displaying 10 item(only the original 10 item) 在我的情况下,更新适配器后,它应该是屏幕上显示的20项(10项原始项目,10项是新项目),但不显示20项目,它只显示10项目(仅原始10项目)

You are using momentArray.length() as endPoint of range and that's why it is not working. 您正在使用momentArray.length()作为范围的endPoint,这就是它无法正常工作的原因。

So just replace below line in runOnUiThread(), 所以只需在runOnUiThread()中替换下面的行,

mAdapter.notifyItemRangeInserted(momentArrayList.size() - momentArray.length(), momentArray.length());

To, 至,

mAdapter.notifyItemRangeInserted(momentArrayList.size() - momentArray.length() + 1, momentArray.length());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM