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

时间:2016-03-24 16:59:25

标签: android android-fragments android-studio android-recyclerview

我需要以编程方式将多个RecyclerViews添加到片段中。我已经设法使用xml布局(下面)包含一个RecyclerView并且它工作正常,但是,当我尝试以编程方式添加任何内容时,即使返回的RecyclerViews不为null,也不会出现在片段视图中。因为我的数据源是由Web API驱动的,所以我不能在xml布局中添加特定数量的RecyclerViews,因为所需的数量会不时变化,因此必须以编程方式完成。我尝试了许多不同的方法,但所有结果都是相同的,例如:不是一个RecyclerView。我还需要在每个RecyclerView上面添加TextViews作为标题,我已经完成了它们并且它们完美地工作,但是从下面的代码中删除它以使其更容易消化。我需要做的就是完成我的项目是添加多个RecyclerViews。我希望有人可以提供帮助吗?

片段:

    @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>

2 个答案:

答案 0 :(得分:5)

You don't need multiple RecyclerViews, you can achieve it with one. With the library SectionedRecyclerViewAdapter you can group your items in sections and add a header to each section:

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

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 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 :(得分:2)

由于您有多个集合,并且每个集合都有您要显示的项目列表。这可以使用expandable listview轻松完成。您显示标题列表,单击展开以显示标题下的项目列表。 Here是有关其实施的教程