ViewPager + RecyclerView issue in android

前端 未结 3 427
逝去的感伤
逝去的感伤 2020-11-28 16:58

Hi I have Tablayout with Viewpager and i am using Fragment for tablayout. Now in every Tablayout fragments I have Recyclerview

相关标签:
3条回答
  • 2020-11-28 17:16

    Problem:

    There's no way to pass a Serializable ArrayList in a Bundle. Look at the docs page here Bundle docs

    Solution:

    Change your SubCategoryModel to implement Parcelable and then use bundle.putParcelableArrayList(key, list) and bundle.getParcelableArrayList(key) to pass the ArrayList to the FragmentArgs and get them from the Fragment

    0 讨论(0)
  • 2020-11-28 17:28

    I see a lot of problems with your code, but let's get your UI displaying the subcategories since that's your main concern.

    Change the getItem in your adapter to this:

            @Override
            public Fragment getItem(int position) {
    
                ArrayList<SubcategoryModel> subcategories = filelist.get(position).getItems();
                Log.v("adapter", "getitem" + String.valueOf(position)+subcategories.size());
                return FirstFragment.create(position,subcategories);
            }
    

    What caused the problem:

    Let's focus on ArrayList<SubcategoryModel> subct in your activity:

    First your code did this:

        for(int i = 0; i < filelist.size(); i++){
            ArrayList<SubcategoryModel> subct=filelist.get(i).getItems();
            // for(int j=0;j<subct.size();j++) ...
        }
    

    So at the end of this loop subct is set the subcategories of the last category in filelist.

    After that, you did another loop to load the tabs, but that used a different subct variable that was declared inside the loop, and that had no effect on the subct field of your activity.

    Then you created your view pager and adapter.

    In your pager adapter you had this:

        @Override
        public Fragment getItem(int position) {
    
            Log.v("adapter", "getitem" + String.valueOf(position)+subct.size());
            return FirstFragment.create(position,subct);
        }
    

    Since subct was set to the last category's subcategories from the loop before, every single fragment created was receiving those subcategories, no matter what position (category) the fragment was for. All I did was change the code to go back to filelist and get the correct category (and subcategories) for the position of the fragment being created.

    When you're writing code, you think about what you want the code to do. However, at the point where you run the code and discover you have a problem, you have to forget what you wanted the code to do, then pretend you're the computer and run the code in your head. You want to understand what effect every line of code is having. When you do it that way it's easier to find the problem.

    0 讨论(0)
  • 2020-11-28 17:40

    this is the main logic of your code I guess... Try it and let me know if you need more help or you find it helpful...

    private void parseJsonData() {
        try {
            listDogs.clear();
            JSONArray jsonArray = new JSONArray(loadJSONFromAsset());
            JSONObject firstJsonobject = jsonArray.optJSONObject(0);
            JSONArray itemListJsonArray = firstJsonobject.optJSONArray("itemList");
            JSONObject secondJsonobject = itemListJsonArray.optJSONObject(0);
            JSONArray typeMasterArray = secondJsonobject.optJSONArray("typeMaster");
            JSONObject thirdJsonobject = typeMasterArray.optJSONObject(0);
            JSONArray catMasterArray = thirdJsonobject.optJSONArray("catMaster");
            for(int i=0; i<catMasterArray.length(); i++) {
                JSONObject jsonObject = catMasterArray.optJSONObject(i);
                ModelClass modelClass = new ModelClass();
                modelClass.setTypeId(jsonObject.optString("catID"));
                modelClass.setTypeName(jsonObject.optString("catName"));
                listDogs.add(modelClass);
            }
            RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(getActivity(), listDogs);
            recyclerView.setAdapter(recyclerViewAdapter);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    

    Note: To parse data for dogs category I've passed 0 position in variable thirdJsonobject. Pass 1 for cats and 2 for horse and you will find your desired output

    Screenshots: Dogs category

    Cats category

    Horse category

    0 讨论(0)
提交回复
热议问题