我的回收站视图有一个父母和一个孩子。我的父母只显示一项,然后是该一项中的所有主题。我可以在下面发布我的代码我不确定我需要做什么来纠正这个问题,因为这是我第一次使用可扩展视图。我已经在下面发布了整个班级。不确定您需要查看什么。
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);
}
}
我只是错过了一些非常简单的东西吗?因为这感觉真的很复杂哈哈
提前致谢!