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

标签 android header android-recyclerview

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

我想复制 facebook 邀请列表格式,其中 recyclerview 中的第一个 View 是标题(在 facebook 案例中为好友请求),第一组数据将滚动到此标题下方,然后作为第二个数据格式进入 View (在 Facebook 案例中您可能认识的人)一个新的标题将滚动并替换这个初始标题,然后第二组数据将再次滚动到第二个标题下。

我有包含数据集和切换 View 的 recyclerview 设置,但是有什么想法可以像 facebook 一样实现“动态标题效果”吗?

谢谢。

最佳答案

你可以用这个 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);
    }
}

然后您使用您的 Sections 设置 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);

关于android - 带有滚动标题的 Recyclerview(如 facebook 邀请列表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37319278/

相关文章:

html - 如何在 HTML5 中放置标题横幅图像

html - 防止在 header 之后换行,但不能在 header 之前换行

java - Android RecyclerView 添加和删除项

java - 如何启用 RecyclerView 中的下一项?

android - Recyclerview View 未填充

java - 服务测试中的 NullPointerException

android - 如何连接到我的 http ://localhost web server from MEmu Android emulator

android - 需要改变浮点精度

java - 在 Android 应用程序中显示较大的格式化文本文章

html - 为什么当我播放 HTML5 mp3 文件时,Firefox 不在 HTTP header 中发送 Referer?