可扩展的Recycler视图仅显示父项中的一个项目

时间:2016-04-20 15:45:26

标签: android android-recyclerview expandablerecyclerview

我的回收者视图有父母和孩子。我的父母只显示一个项目,然后显示该项目中的所有主题。我可以在下面发布我的代码我不知道我需要做什么来纠正这个问题,因为这是我第一次使用可扩展视图。我在下面发了全班。不确定你需要什么。

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);
    }

}

我只是错过了一些非常简单的事情吗?因为这感觉很复杂lol

提前致谢!

2 个答案:

答案 0 :(得分:0)

我不确定你的代码是否足够好。
请参阅https://github.com/anandbose/ExpandableListViewDemo非常清楚

答案 1 :(得分:0)

您可以使用库SectionedRecyclerViewAdapter轻松实现它。有一个完整的工作示例here

基本上你创建了一个分类:

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();
            }
        });
    }
}

然后使用您的部分设置RecyclerView:

// 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);