0

My recycler view has a parent and a child. My parent is only displaying one item with then all the topics in that one item. I can post my code below I'm not sure what it is I need to do to correct this as this is my first time using expandable views. I've posted the whole class below. Not sure what was needed for you to see.

public class TopicFragment extends Fragment {

    private View mRootView;
    private CategoryResponse mData;
    private CategoryFeedDataFactory mDataFactory;
    private List<String> mCategories;

    //Expandable Recycler View
    private List<TopicItem> mTopics;
    private TopicResponse mTopic;
    private TopicFeedDataFactory mTopicDataFactory;
    private CategoryItem mCategoryItem;


    //RecyclerView
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLayoutManager;
    private TopicExpandableAdapter mExpandableAdapter;


    public TopicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

        mRootView = inflater.inflate(R.layout.fragment_topic, container, false);

        initUI();

        return mRootView;

    }

    private void initUI() {

        mRecyclerView = (RecyclerView)   mRootView.findViewById(R.id.categoryView);
        mLayoutManager = new LinearLayoutManager(getActivity().getApplication().getApplicationContext());

        loadData();


    }

    private void loadData() {
        mDataFactory = new CategoryFeedDataFactory(getActivity().getApplicationContext());
        mTopicDataFactory = new TopicFeedDataFactory(getActivity().getApplicationContext());

        mDataFactory.getCategoryFeed(new CategoryFeedDataFactory.CategoryFeedDataFactoryCallback() {
            @Override
            public void onCategoryDataReceived(CategoryResponse response) {

                mData = response;

            }

            @Override
            public void onCategoryDataFailed(Exception exception) {

            }
        });

        mTopicDataFactory.getAllTopics(new TopicFeedDataFactory.TopicFeedDataFactoryCallback() {
            @Override
            public void onTopicDataReceived(TopicResponse response) {

                mTopic = response;
                populateUIWithData();

            }

            @Override
            public void onTopicDataFailed(Exception exception) {

            }
        });
    }

    private void populateUIWithData() {
        mCategories = new ArrayList<>();
        mTopics = new ArrayList<>();

        for (int i = 0; i <= mData.getItems().size(); i++) {
            if (mData != null) {
                if (mData.getItem(i) != null) {
                    if (mData.getItem(i).getCategoryItem() != null &&
                            mData.getItem(i).getCategoryItem().getName() != null) {
                        mCategories.add(mData.getItem(i).getCategoryItem().getName());
                    }
                }

            }

            for (int j = 0; j <= mTopic.getItems().size(); j++) {
                if (mTopic != null)
                    if (mTopic.getItem(j) != null)
                        if (mTopic.getItem(j).getTopicItem() != null)
                            if (mTopic.getItem(j).getTopicItem().getCategoryID() != null) {
                                if (mData.getItem(i) != null && mData.getItem(i).getCategoryItem() != null)
                                    if (mData.getItem(i).getCategoryItem().getId() != null)
                                    if (mTopic.getItem(j).getTopicItem().getCategoryID().equals
                                            (mData.getItem(i).getCategoryItem().getId())) {

                                        mTopics.add(mTopic.getItem(j).getTopicItem());
                                        mCategoryItem =
                                                new CategoryItem(mData.getItem(i).getCategoryItem().getName(),
                                                        mTopics);

                                    }

                        }
        }
    }


    final List<CategoryItem> categoryItems = Collections.singletonList(mCategoryItem);

        mExpandableAdapter = new TopicExpandableAdapter(getActivity(), categoryItems);
        mExpandableAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
            @Override
            public void onListItemExpanded(int position) {
                CategoryItem expandedCategoryItem = categoryItems.get(position);

                String toastMsg = getResources().getString(R.string.expanded, expandedCategoryItem);
                Toast.makeText(getActivity().getApplicationContext(),
                        toastMsg,
                        Toast.LENGTH_SHORT)
                        .show();

            }

            @Override
            public void onListItemCollapsed(int position) {
                CategoryItem collapsedCategoryItem = categoryItems.get(position);

                String toastMsg = getResources().getString(R.string.collapsed, collapsedCategoryItem.getName());
                Toast.makeText(getActivity().getApplicationContext(),
                        toastMsg,
                        Toast.LENGTH_SHORT)
                        .show();

            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mExpandableAdapter);
    }

}

Am I just missing something really simple? Because this feels really complicated lol

Thanks in advance!

BilalMH
  • 175
  • 1
  • 21

2 Answers2

0

I'm not sure that your code is good enough.
See https://github.com/anandbose/ExpandableListViewDemo is pretty clear

Artur
  • 390
  • 1
  • 3
  • 12
  • See, this is what confuses me. I have two api calls and anything using hardcoded data like this just baffles me lol. But will give this a try – BilalMH Apr 20 '16 at 16:59
0

You can easily implement it with the library SectionedRecyclerViewAdapter. There is a full working example here.

Basically you create a section class:

class MySection extends StatelessSection {

    String title;
    List<TopicItem> list;
    boolean expanded = true; // true if you want it to be displayed expanded initially

    public MySection(String title, List<TopicItem> list) {
        // call constructor with layout resources for this Section header, footer and items 
        super(R.layout.section_header, R.layout.section_item);

        this.title = title;
        this.list = list;
    }

    @Override
    public int getContentItemsTotal() {
        return expanded? list.size() : 0;
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;

        // bind your view here
        itemHolder.tvItem.setText(list.get(position).getTopicName());
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new SimpleHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;

        // bind your header view here
        headerHolder.tvItem.setText(title);

        // handles the click on the header to toggle the expanded variable
        headerHolder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                expanded = !expanded;
                headerHolder.imgArrow.setImageResource(
                        expanded ? R.drawable.ic_keyboard_arrow_up_black_18dp : R.drawable.ic_keyboard_arrow_down_black_18dp
                );
                sectionAdapter.notifyDataSetChanged();
            }
        });
    }
}

Then you set up the RecyclerView with your sections:

// Create an instance of SectionedRecyclerViewAdapter 
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

// Create your sections with the list of data for each category
MySection cat1Section = new MySection(mCategories.get(0), cat1TopicItemList);
MySection cat2Section = new MySection(mCategories.get(1), cat2TopicItemList);

// Add your Sections to the adapter
sectionAdapter.addSection(cat1Section);
sectionAdapter.addSection(cat2Section);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);
Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71