recyclelerview子计数小于活动类中的arrayList大小

时间:2017-06-19 11:59:56

标签: android android-recyclerview

在我的Activity课程中,我得到了Recyclerview的孩子,如:

final int itemsCount = recly_list_lab.getChildCount();

但它给了我7个项目,但我有10个项目然后如何在主要活动中获得10个项目

这是我为recyclelerView获取子项目的代码:

  final int itemsCount = recly_list_lab.getChildCount();
    Log.e("itemsCount::",itemsCount+"");
    if (itemsCount != 0) {
        try {

            for (int i = 0; i <itemsCount; i++) {

                final View viewtestcategoryname = recly_list_lab.getChildAt(i);
                AutoCompleteTextView autoctv_testcategory = (AutoCompleteTextView) viewtestcategoryname.findViewById(R.id.autoctv_testcategory);
                String TestCategoryName = autoctv_testcategory.getText().toString();
                Log.e("TestCategoryname:",TestCategoryName);

                AutofitRecyclerView grid_labreferal_checkboxes_test = (AutofitRecyclerView) viewtestcategoryname.findViewById(R.id.grid_labreferal_checkboxes_test);
                final int itemcheckTestCount = grid_labreferal_checkboxes_test.getChildCount();
                Log.e("itemcheckTestCount:",itemcheckTestCount+"");

                RecyclerView recly_testsubcategory = (RecyclerView) viewtestcategoryname.findViewById(R.id.recly_testsubcategory);
                final int itemSubCategoryName = recly_testsubcategory.getChildCount();
                Log.e("itemSubCategoryName:",itemSubCategoryName+"");


                for (int test_check = 0; test_check < itemcheckTestCount; test_check++) {

                    final View viewTestCheck = grid_labreferal_checkboxes_test.getChildAt(test_check);
                    CheckBox grid_labreferal_checkbox = (CheckBox) viewTestCheck.findViewById(R.id.grid_labreferal_checkbox);
                    TextView txt_item_id = (TextView) viewTestCheck.findViewById(R.id.txt_item_id);
                    if (grid_labreferal_checkbox.isChecked()) {
                         Log.e("%sgrid_lcheckbox", grid_labreferal_checkbox.getText().toString());


                        if (txt_item_id != null && txt_item_id.getText().toString().trim().length() != 0) {


                        }

                    }
                }

                for (int subcatname = 0; subcatname< itemSubCategoryName; subcatname++){
                    final View SubCategory = recly_testsubcategory.getChildAt(subcatname);
                    AutoCompleteTextView autoctv_testsubcategory = (AutoCompleteTextView) SubCategory.findViewById(R.id.autoctv_testsubcategory);
                    Log.e("subcategoryname::",autoctv_testsubcategory.getText().toString());
                    AutofitRecyclerView grid_labreferal_checkboxes_subtest= (AutofitRecyclerView) SubCategory.findViewById(R.id.grid_labreferal_checkboxes_subtest);
                    final int itemcheckSubtestCount = grid_labreferal_checkboxes_subtest.getChildCount();
                    Log.e("itemcheckTestCount:",itemcheckSubtestCount+"");

                    for (int test_check = 0; test_check < itemcheckSubtestCount; test_check++) {
                        // jsonObjectMulti = new JSONObject();
                        final View viewTestCheck = grid_labreferal_checkboxes_subtest.getChildAt(test_check);
                        CheckBox grid_labreferal_checkbox = (CheckBox) viewTestCheck.findViewById(R.id.grid_labreferal_checkbox);
                        TextView txt_item_id = (TextView) viewTestCheck.findViewById(R.id.txt_item_id);
                        if (grid_labreferal_checkbox.isChecked()) {
                            Log.e("%sgrid_subtestcheckbox", grid_labreferal_checkbox.getText().toString());


                            if (txt_item_id != null && txt_item_id.getText().toString().trim().length() != 0) {
                                //testUpload.put(txt_item_id.getText().toString().trim(), grid_labreferal_checkbox.getText().toString().trim());
                            }

                        }
                    }


                }



            }

            } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

3 个答案:

答案 0 :(得分:0)

recyler视图中的

视图可能小于视图中的项目总数,因为视图将被回收并重复使用以显示多个项目。

您可以使用adapter.getItemCount()方法来了解在recyler视图中将显示多少个视图项。

答案 1 :(得分:0)

并非所有适配器项都附加到父项。 getChildCount()方法返回显示的视图数。如果要获取适配器的项目数,则必须查询适配器。

final int itemsCount = recly_list_lab.getAdapter().getItemCount();

但考虑到您的代码,您只是错过了使用RecyclerView。您不应该直接访问回收站的子视图。您需要做的一切都应该通过适配器和视图持有者!

答案 2 :(得分:0)

RecyclerView 不保存内存中所有列表项的视图,它只有例如 - 在您的情况下为 7。当您滚动时,它会重复使用这 7 个视图来显示不同的项目。这样做的目的是提高渲染性能。