使用FirebaseIndexRecyclerAdapter以不同方式显示第一个项目

时间:2017-05-25 11:17:40

标签: android firebase android-recyclerview firebase-authentication recycler-adapter

我有一个应用程序,我在firebase中有一个“游戏”列表,以及属于该游戏的用户。 我正在使用FirebaseIndexRecyclerAdapter在Recyclerview中显示某个用户的游戏详情

  "Games" : {
    "-Kk6_Y05XnmWpxrhjdg2" : {
      "confirmedPlayers" : {
        "cahTG4Fp5XUOOU8TGnyf8S35PVN2" : true
      },
      "missingPlayers" : 9,
      "name" : "GoogleGame",
      "noPlayers" : 5,
      "timestamp" : "201706151200"
    },
    "-Kk7u9kbcLmVfq3i2kiK" : {
      "confirmedPlayers" : {
        "cahTG4Fp5XUOOU8TGnyf8S35PVN2" : true
      },
      "missingPlayers" : 9,
      "name" : "nico game",
      "noPlayers" : 5,
      "timestamp" : "201706151800"
    },
    "-KkGfriMuqnw2DrnpsW-" : {
      "confirmedPlayers" : {
        "cahTG4Fp5XUOOU8TGnyf8S35PVN2" : true
      },
      "missingPlayers" : 9,
      "name" : "santi",
      "noPlayers" : 5,
      "timestamp" : "201706171100"
    }
  },
  "UserGames" : {
    "cahTG4Fp5XUOOU8TGnyf8S35PVN2" : {
      "confirmed" : {
        "-Kk6_Y05XnmWpxrhjdg2" : "201706151200",
        "-Kk7u9kbcLmVfq3i2kiK" : "201706151800",
        "-KkGfriMuqnw2DrnpsW-" : "201706171100"
      }
    }
  }
}

这里是代码

FirebaseAuth mAuth = FirebaseAuth.getInstance();
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
DatabaseReference userGamesRef = root.child("UserGames").child(mAuth.getCurrentUser().getUid());
DatabaseReference confirmedRef = userGamesRef.child("confirmed");
DatabaseReference gamesRef = root.child("Games");

gameListFirebaseAdapter = getFirebaseIndexRecyclerAdapterInstance(confirmedRef, gamesRef);

gameListFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
  @Override
  public void onItemRangeInserted(int positionStart, int itemCount) {
    super.onItemRangeInserted(positionStart, itemCount);
    int gamesViewCount = gameListFirebaseAdapter.getItemCount();
    int lastVisiblePosition =
      gameListLinearLayoutManager.findLastCompletelyVisibleItemPosition();
    if (lastVisiblePosition == -1 ||
      (positionStart >= (gamesViewCount - 1) &&
        lastVisiblePosition == (positionStart - 1))) {
      gameListRecyclerView.scrollToPosition(positionStart);
    }
  }
});

gameListRecyclerView.setAdapter(gameListFirebaseAdapter);

但是我想以不同的格式显示第一个项目(不同的视图)。

您建议采用什么方法?

尝试失败1#:我无法执行排除第一项的查询并将其传递给FirebaseIndexRecyclerAdapter。 (并在不同视图中显示第一个项目) 我相信因为FirebaseIndexRecyclerAdapter收到了DatabaseReference,如果在你松开之前有查询。

DatabaseReference filteredDbRef = confirmedRef.orderByValue().startAt("201506151800").getRef();
gameListFirebaseAdapter = getFirebaseIndexRecyclerAdapterInstance(filteredDbRef, gamesRef);

待定尝试:停止使用FirebaseIndexRecyclerAdapter使用嵌套的addListenerForSingleValueEvent执行addChildEventListener并以某种方式逐项将这些项添加到recyclerview。 (并在不同的视图中显示第一个项目)

0 个答案:

没有答案