gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:19:36 27 4
gpt4 key购买 nike

我已经设置了一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com