In Adapter class, how can I call startListener() and stopListener()?

爷,独闯天下 提交于 2020-06-29 04:39:43

问题


Multiple viewType using FirebaseRecyclerAdapter. I studied about RecyclerView that have multiple viewType. And I'm going to apply it in FirebaseRecyclerApdater. I want to show the data in Firebase Realtime Database. But anything shows up in-app. Here my code.

public class HolderHomeAdapter extends RecyclerView.Adapter<HolderHomeAdapter.HomeSectionHolder> {

private ArrayList<SectionType> sectionList;
private Context context;

FirebaseRecyclerOptions<ContentModel> options;
public static FirebaseRecyclerAdapter recyclerAdapter;

public HolderHomeAdapter(Context context, ArrayList<SectionType> sectionList) {
    this.context = context;
    this.sectionList = sectionList;
}

@NonNull
@Override
public HomeSectionHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.holder_section_home, viewGroup, false);
    return new HomeSectionHolder(view);
}

@Override
public void onBindViewHolder(@NonNull HomeSectionHolder holder, int i) {
    SectionType tempSection = sectionList.get(i);

    int type = tempSection.getSectionType();
    String title = tempSection.getTitle();

    holder.holderText.setText(title);
    holder.holderRecycler.setHasFixedSize(true);
    holder.holderRecycler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

    if (type == TYPE_01) {
        Query type01Query = FirebaseDatabase.getInstance().getReference().child("TYPE_01");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type01Query, ContentModel.class).build();
        recyclerAdapter = new Type01Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    } else if (type == TYPE_02) {
        Query type02Query = FirebaseDatabase.getInstance().getReference().child("TYPE_02");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type02Query, ContentModel.class).build();
        recyclerAdapter = new Type02Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    } else if (type == TYPE_03) {
        Query type03Query = FirebaseDatabase.getInstance().getReference().child("TYPE_03");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type03Query, ContentModel.class).build();
        recyclerAdapter = new Type03Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    } else {
        Query type04Query = FirebaseDatabase.getInstance().getReference().child("TYPE_04");
        options = new FirebaseRecyclerOptions.Builder<ContentModel>().setQuery(type04Query, ContentModel.class).build();
        recyclerAdapter = new Type04Adapter(options);
        holder.holderRecycler.setAdapter(recyclerAdapter);
    }

}

@Override
public int getItemCount() {
    return sectionList.size();
}
}

Above HolderHomeAdapter class called in MainActivity. I want to know when I have to call the method startListening() and stopListening().

来源:https://stackoverflow.com/questions/57702409/in-adapter-class-how-can-i-call-startlistener-and-stoplistener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!