如何在FirebaseRecyclerAdapter

时间:2018-05-18 23:48:35

标签: android firebase firebase-realtime-database firebaseui

我有一个主要的回收站视图,我想要添加标题。如果我创建自己的适配器,这将是一项简单的任务,但我使用Firebase Ui库中的FirebaseRecyclerAdapter,因为它具有优势。我在firebase中的json数据包含两种类型的"对象"在同一主列表中,一种类型是电影版本,另一种类型是标题对象。所以这意味着我可以查看我们正在谈论的数据,如果它是一个标题对象,则电影ID将为空(不存在)。我想要粘性标题,这意味着我希望月标题始终位于顶部。 我想要' May'标题贴在顶部,直到用户向下滚动到“6月”。电影发行,在这种情况下,下一个坚持到顶部的项目将是6月'。我该怎么做?

我的代码知道:

        FirebaseRecyclerOptions<_Release> options =
            new FirebaseRecyclerOptions.Builder<_Release>()
                    .setQuery(mReleasesRef, _Release.class)
                    .build();

    adapter = new FirebaseRecyclerAdapter<_Release, RecyclerView.ViewHolder>(options) {
        public final static int TYPE_HEADER = 0;
        public final static int TYPE_ITEM = 1;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.message for each item
            if (viewType == TYPE_ITEM) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_release_item, parent, false);
                return new ReleaseHolder(view);
            } else {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
                return new HeaderHolder(view);
            }
        }

        @Override
        public int getItemViewType(int position) {
            if (getItem(position).getGame() == null) {
                return TYPE_HEADER;
            }
            return TYPE_ITEM;
        }

        @Override
        protected void onBindViewHolder(RecyclerView.ViewHolder holder, int position, _Release release) {
            // Bind the Release object to the ReleaseHolder
            if (holder instanceof  HeaderHolder) {
                HeaderHolder headerHolder = (HeaderHolder) holder;
                headerHolder.setHeaderText(release.getName());
            } else if (holder instanceof ReleaseHolder) {
                ReleaseHolder releaseHolder = (ReleaseHolder) holder;
                releaseHolder.setName("");

            }
        }
    };

我的数据库结构:

"data" : [ {
    "m" : 5,
    "name" : "May 2018",
    "y" : 2018
  }, {
    "category" : 0,
    "created_at" : 1525589123745,
    "date" : 1525564800000,
    "movie" : 121,
    "human" : "2018-May-06",
    "id" : 152209,
    "m" : 5,
    "platform" : [ 3, 14, 6 ],
    "region" : 8,
    "updated_at" : 1525594750205,
    "y" : 2018
  }]

1 个答案:

答案 0 :(得分:0)

您可以通过覆盖FirebaseRecyclerAdaptergetItemCount方法将任何自定义对象添加到getItem (无需从Firebase数据库加载)

示例(java) - 位置0上的自定义项目。从db加载的列表的其余部分:

@Override
public int getItemCount() {
    if (super.getItemCount() == 0)
      return 0;    // use this perk to prevent showing heder element before 
                   // database query is fully-loaded

      return super.getItemCount() + 1;
}

@Override
public _Release getItem(int position) {
    if (position == (getItemCount() - 1)) {
        _Release asdf = new _Release();
        // init data of object
        return asdf;
    } else {
        return super.getItem(position);
    }
}