如何在嵌套的viewpager中通过用户ID检索firebase实时数据库数据?

rqmkfv5c  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(242)

我有两个适配器:一个是垂直的,另一个是水平的,我想把一个用户的数据放在列表中,所以当我水平滑动时,我只得到这个用户的数据。
...

When I swipe vertical I would like to get another user
Like this:
Vertical swipe
User1 horizontal swipe -> {datauser1, datauser1}
Vertical swipe
User2 horizontal swipe ->   {datauser2, datauser2}
...

我有一个包含所有用户id的列表,当向左(水平)滑动时,我希望从中检索数据,但问题是,当我垂直滑动时,我得到的列表始终与列表中包含的所有用户id的数据相同。这意味着我得到:

Vertical swipe
User1 horizontal swipe -> {datauser1, datauser1, datauser2, datauser2}
Vertical swipe
User1 horizontal swipe -> {datauser1, datauser1, datauser2, datauser2}

我解决这个问题的方法是首先获取用户的id,然后使用id遍历数组,从数组中的每个userid获取数据。用户id的垂直滑动加载列表水平加载此用户id的数据
但它仍然不能像我想的那样工作。

public class discover_fullscreen_adapter2 extends RecyclerView.Adapter {

private final int stories = 0;
private List<String> userList;
private Context mContext;
private List<Object> objectItems = new ArrayList<>();
private DatabaseReference databaseReference;

public discover_fullscreen_adapter2(Context context, List<String> userIdList) {
    this.mContext = context;
    userList = userIdList;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    int layout = 0;
    RecyclerView.ViewHolder viewHolder;
    // Identify viewType returned by getItemViewType(...)
    // and return ViewHolder Accordingly
    if (viewType == stories) {
        layout = R.layout.view_pager_horizontal;
        View contentView = LayoutInflater
                .from(parent.getContext())
                .inflate(layout, parent, false);
        viewHolder = new testViewHolder2(contentView);
    } else {
        viewHolder = null;
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    int viewType = holder.getItemViewType();
    if (viewType == stories) {
        ((testViewHolder2) holder).setViewPager2(((testViewHolder2) holder).videosViewPager, userList);
    }
}

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

public class testViewHolder2 extends RecyclerView.ViewHolder {

    ViewPager2 videosViewPager;
    int limit = 7, itemPos = 0;
    private int currentPage = 0, intttt;
    private int itemCurrentPositionOnViewPager;
    private boolean loading = true;

    public testViewHolder2(@NonNull View itemView) {
        super(itemView);

        videosViewPager = itemView.findViewById(R.id.viewPagerVideosHorizontal);

    }

    void setViewPager2(ViewPager2 videosViewPager, List<String> listUserId){

        databaseReference = FirebaseDatabase.getInstance().getReference("stories");
        Query queryFeed = databaseReference;
        queryFeed.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                story_modell story = dataSnapshot.getValue(story_modell.class);

                for (String id: listUserId){
                    if (id.equals(story.getUID())){

                        objectItems.add(story);
                    }

                }

                videosViewPager.setAdapter(new view_page_horizontal_adapter(mContext, objectItems));

            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
  }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题