RecyclerView部分和来自Firebase的数据 [英] RecyclerView Sections and data from Firebase

查看:156
本文介绍了RecyclerView部分和来自Firebase的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一个应用程序,在这个应用程序中,我必须使用Firebase填充数据回收站。
现在我想要做的是用数据firebase填写一个recyclerview,但是有一些部分将是主数据firebase的父亲,我有点迷路了,而且我认为这是在适配器Recyclerview哪里没有同样,我希望你能帮助我。
对不起,我的英文不好。





谢谢!我很抱歉我的英文。

使用这个库 SectionedRecyclerViewAdapter 将数据分组到各个部分。



首先创建一个Section类:

  class MySection extends StatelessSection {

String title;
列表< String>列表;
$ b $ public MySection(String title,List< String> list){
//调用具有此页眉,页脚和项目布局资源的构造函数
super(R.layout。 section_header,R.layout.section_item);

this.title = title;
this.list = list;


@Override
public int getContentItemsTotal(){
return list.size(); //本节的项目数
}

@Override
public RecyclerView.ViewHolder getItemViewHolder(View view){
//返回一个ViewHolder自定义实例这部分的项目
返回新的MyItemViewHolder(view);

$ b @Override
public void onBindItemViewHolder(RecyclerView.ViewHolder持有者,int位置){
MyItemViewHolder itemHolder =(MyItemViewHolder)持有者;

//在这里绑定你的视图
itemHolder.tvItem.setText(list.get(position));
}

@Override
public RecyclerView.ViewHolder getHeaderViewHolder(View view){
return new SimpleHeaderViewHolder(view);

$ b @Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder持有者){
MyHeaderViewHolder headerHolder =(MyHeaderViewHolder)持有者;

//在这里绑定你的标题视图
headerHolder.tvItem.setText(title);






$ b然后你用你的章节设置RecyclerView: / p>

  //创建SectionedRecyclerViewAdapter的实例
SectionedRecyclerViewAdapter sectionAdapter = new SectionedRecyclerViewAdapter();

//使用每年的数据列表创建您的部分
MySection section1 = new MySection(Section 1,section1DataList);
MySection section2 = new MySection(Section 2,section2DataList);

//将节添加到适配器
sectionAdapter.addSection(section1);
sectionAdapter.addSection(section2);

//使用SectionedRecyclerViewAdapter设置您的RecyclerView
RecyclerView recyclerView =(RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(新的LinearLayoutManager(getContext()));
recyclerView.setAdapter(sectionAdapter);


Hi I'm working on an app, in it I have to populate a data recyclerview with Firebase, ready. Now I want to do is fill out a recyclerview with data firebase but has sections which would be the father that host data firebase, I'm a little lost with this, and I think it is in the adapter Recyclerview where not as do, I hope you can help me. Sorry for my English, it's not good.

Thanks! and i am sorry for my english.

解决方案

Use this library SectionedRecyclerViewAdapter to group your data into sections.

First create a Section class:

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 for each year
MySection section1 = new MySection("Section 1", section1DataList);
MySection section2 = new MySection("Section 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);

这篇关于RecyclerView部分和来自Firebase的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆