Android:在片段中以编程方式添加多个RecyclerView

9
我需要在一个Fragment中以编程方式添加多个RecyclerView。我已经成功地包含了一个使用xml布局的RecyclerView(如下所示),它工作得很好,但是当我尝试以编程方式添加任何一个RecyclerView时,即使返回的RecyclerView不为null,也没有一个出现在片段视图中。因为我的数据源是Web API驱动的,所以我无法在xml布局中添加特定数量的RecyclerView,因为所需数量会随时改变,因此必须在程序中完成。我尝试了许多不同的方法,但所有结果都是相同的,例如:没有一个RecyclerView。我还需要在每个RecyclerView上方添加TextView作为标题,我已经完成了这些并且它们完美地工作,但是它们从下面的代码中删除,以便更容易理解。我只需要能够添加多个RecyclerView就可以完成我的项目。希望有人能帮忙?
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
    svv = new ScrollView(getActivity());
    svv.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.WRAP_CONTENT));
    linLayout = new LinearLayout(getActivity());
    linLayout.setOrientation(LinearLayout.VERTICAL);
    linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

    View rootView = inflater.inflate(R.layout.fragment_root, container, false);
    HorizontalScrollView svh;
    RecyclerView itemsListing;

    int top = 100;
    for(int i = 0; i < itemCount; i++) {
    String strSubURL = myListItem.myListUrls.get(i).toString();
    sharedData.setCurrMyURL(String.valueOf(strSubURL));

    // Below is where the problems start

    // This works fine but only provides one recyclerview
    //itemsListing = (RecyclerView) rootView.findViewById(R.id.items_listing);

    // This does not work at all, showing zero recyclerviews even though the views are not null and are therefore actually created
    itemsListing = new RecyclerView(inflater.getContext());

    itemsListing.setPadding(0, top, 0, 0);
    mLayoutManager = new LinearLayoutManager(itemsListing.getContext(), LinearLayoutManager.HORIZONTAL, false);
    itemsListing.setLayoutManager(mLayoutManager);
    mAdapter = new ItemsListingAdapter(mItems, this);
    itemsListing.setAdapter(mAdapter);
    svh = new HorizontalScrollView(getActivity());
    svh.setPadding(0, top, 0, 0);
    top=top+400;
    }
    svv.addView(linLayout);
    RelativeLayout mainLayout = (RelativeLayout) rootView.findViewById(R.id.item_layout);
    mainLayout.addView(svv);
    return rootView;
    }

XML布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#000000"
    tools:context="com.myco.myapp.items.listing.ItemsListingListingFragment">
    <android.support.v7.widget.RecyclerView
    android:id="@+id/items_listing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
    </RelativeLayout>

我不明白为什么你要这样做:这里有一个解决方案,实现有限数量的RecyclerView,并且每个RecyclerView行包含另一个RecyclerView。现在你可以展示任意多的RecyclerView项。 - suku
嗨@suku,感谢您的回复,尽管我完全不理解!我有一个Web API,可以对集合进行分类,每个集合都有一个标题。这些集合是动态的,它们的数量、其中的项目和标题将定期更改。在iOS中,我使用通过编程创建的带有标题标签的集合视图,这很完美。在Android中,我还编写了静态XML回收站视图,其中集合不会更改,并且它们也可以正常工作。那么,您是否在说我可以在一个xml声明的回收站视图中嵌套通过编程创建的回收站视图? - partynose
现在我明白你想做什么了。请看我的答案。 - suku
2个回答

8

您不需要多个RecyclerView,只需使用一个即可实现。通过使用库SectionedRecyclerViewAdapter,您可以将项目分组到各个部分,并为每个部分添加标题:

class MySection extends StatelessSection {

    String title;
    List<String> list;

    public MySection(String title, List<String> 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 list.size(); // number of items of this section
    }

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

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

然后你需要使用你的Sections设置RecyclerView:

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

// Create your sections with the list of data you got from your API
MySection data1Section = new MySection("Data 1", data1List);
MySection data2Section = new MySection("Data 2", data2List);

// Add your Sections to the adapter
sectionAdapter.addSection(data1Section);
sectionAdapter.addSection(data2Section);

// Set up your RecyclerView with the SectionedRecyclerViewAdapter
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);

1
谢谢@Gustavo...这听起来就是我想要的。我会试一试...谢谢,谢谢.... :) - partynose
如何在同一行上显示每个部分的2个项目,并能够左右滚动以获取更多项目? - Chhorn Soro

2

如果你有多个集合,每个集合都有一些你想要显示的项目列表。这可以通过使用可扩展列表视图轻松完成。您可以显示一个标题列表,当单击时,它会展开以显示标题下的项目列表。这里提供了一个实现该功能的教程。


谢谢@suku。尽管我不想让任何东西“可扩展”,但我会研究一下,因为它需要从一开始就扩展并设置在滚动视图中。这是一个媒体播放器,有大量内容应该从一开始就可见,并且需要与其他静态声明的回收站视图匹配,而不是动态的。我认为Gustavo给了我我所需要的东西,但是你的帮助同样受到赞赏。我也会接受你的答案。再次感谢... :) - partynose

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接