在RecyclerView中加载更多不能正常工作

时间:2017-12-11 20:00:53

标签: android android-recyclerview recyclerview-layout recyclerview-multiselect

我有自定义的recyclerView.In项目长按我会隐藏一些视图并显示另一个视图(隐藏图像视图和显示复选框) 这是我的代码片段

      @Override
        public void onLongClickListener(String type, final int pos) {
            if (serverStatus == 4) {
                for (int i = 0; i < recyclerView.getChildCount(); i++) {
                    View view = recyclerView.getChildAt(i);
                    if (view == null) {
                        return;
                    }
                    final CircleImageView circleImageView = view.findViewById(R.id.circle_image);
                    final LetterImageView letterImageView = view.findViewById(R.id.iv_avatar);
                    final AppCompatCheckBox appCompatCheckBox = view.findViewById(R.id.checkbox);

                    letterImageView.setVisibility(View.GONE);
                    circleImageView.setVisibility(View.GONE);
                    final int finalI = i;

                    if (i == pos) {
                        appCompatCheckBox.setChecked(true);
                        currenctAmount = currenctAmount + Double.parseDouble(recyclerAdapter.getItem(finalI).getPrice());
                        ((DashboardActivity) getActivity()).showFourTabHeader(currenctAmount + "");

                        ((DashboardActivity) getActivity()).hideBottomBar();

                    }

                    appCompatCheckBox.setVisibility(View.VISIBLE);
                }
            }

        }

一切都很完美但我有另一个问题。我会尝试解释我的问题 在我的recyclerView中,我使用了更多规范加载。当加载更多动作时,我的所有复选框都隐藏了,并且recyclerView具有以前的样式。(我的所有复选框都被隐藏)。这是我的加载更多功能

 if (recyclerAdapter != null) {
        recyclerAdapter.setmOnLoadMoreListener(new RecyclerAdapter.OnLoadMoreListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onLoadMore() {
                packagesLists.add(null);
                recyclerAdapter.notifyItemInserted(packagesLists.size() - 1);

                getpackageRequest(false, serverStatus,
                        recyclerAdapter.getItemCount(),
                        null,
                        payStatus, true);
            }
        });
    }



public void getpackageRequest(boolean allowShowDialog, final int serverStatus, int start, String searchParameters, final int payStatus, final boolean loadMore) {



    DataRepository dataRepository = new DataRepository(getActivity());

    dataRepository.getVolleyRequest(allowShowDialog, Request.Method.GET, url, new HashMap<String, String>(), new DataRepository.DaTaRepositoryCallback() {
        @Override
        public void onSuccess(String response) throws JSONException {
            JSONObject jsonObject = new JSONObject(response);



            Log.e("response packages", jsonObject.toString() + "mm");
            int errorcode = jsonObject.getInt("errorcode");
            String message = jsonObject.getString("message");

            if (errorcode == 0) {

                JSONObject dataObject = jsonObject.getJSONObject("data");
                packageSize = dataObject.getInt("count");
                ((DashboardActivity) getActivity()).updateHeaderValue(packageSize);

                JSONArray listArray = dataObject.getJSONArray("list");


                if (listArray.length() > 0) {
                    emptyLayout.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);

                    if (loadMore) {
                        packagesLists.remove(packagesLists.size() - 1);
                        recyclerAdapter.notifyItemRemoved(packagesLists.size());
                        recyclerAdapter.setLoaded();

                    }


                    Gson gson = new Gson();
                    PackagesList[] packages = gson.fromJson(listArray.toString(), PackagesList[].class);
                    packagesLists.addAll(Arrays.asList(packages));
                    recyclerAdapter.notifyDataSetChanged();


                } else {

                    if (!loadMore) {
                        recyclerView.setVisibility(View.GONE);
                        emptyLayout.setVisibility(View.VISIBLE);
                    }


                }
            } else {
                new MaterialDialog.Builder(getActivity())
                        .content(message)
                        .positiveText(getString(R.string.u_yes))
                        .show();
            }

        }

我认为问题是notifyDataSetChanged。如何在recyclerView中添加新项目可以保存以前的项目样式? 感谢

1 个答案:

答案 0 :(得分:0)

您应该在数据中保留一些标记,具体取决于您可以切换视图的可见性。