带滚动标题的Recyclerview(如facebook邀请列表)

时间:2016-05-19 09:34:46

标签: android header android-recyclerview

我已经设置了一个包含两种不同数据格式的列表的recyclerview。

我想要复制facebook邀请列表格式,其中recyclerview中的第一个视图是标题(在Facebook情况下的朋友请求),第一组数据将在此标题下方滚动,然后作为第二个数据格式进入视图(在facebook的情况下你可能知道的人)一个新的标题将滚动并替换这个初始标题,然后第二组数据将在第二个标题下滚动。

我有使用数据集和切换视图的recyclerview设置,但任何想法如何实现动态标题效果'喜欢facebook?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用此library来实现它。见下图:

enter image description here

首先创建一个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);
    }
}

然后使用您的章节设置RecyclerView:

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

// Create your sections with the list of data for each year
MySection section1 = new MySection("Header 1", section1DataList);
MySection section2 = new MySection("Header 2", section2DataList);

// Add your Sections to the adapter
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);

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