在选项卡式活动中,RecyclerView是空白的

时间:2019-02-26 15:14:05

标签: android android-fragments android-recyclerview android-tabbed-activity

我正在制作一个类似Instagram的应用,其中每个用户都有一个选项卡式活动。第一个标签显示有关用户的常规信息。 seccond选项卡应在回收者视图中显示所有用户的帖子。问题是,事实并非如此。该选项卡保持空白,屏幕上没有任何显示。我有一个主页,其中与每个用户帖子相同的提要工作正常,并且仅针对用户帖子具有几乎相同的代码。我认为主要的问题是,因为这是一个选项卡式的行为,所以回收者视图位于一个片段中。片段的Java类有所不同,我真的不知道如何在另一个活动中使用相同的代码。    这是我在特定片段中的代码。由于代码很长,因此我花了一些时间,但我希望您能基本了解我要达到的目标以及为什么它在正常活动中无法正常工作。先感谢您。 :)

private RecyclerView spotsList;

private FirebaseAuth firebaseAuth;
private DatabaseReference postsRef, userReference, likesRef, seenRef;

public static String thisPagesUsersId = null;
private String currentUserId;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.fragment_users_spots, container, false);

    firebaseAuth = FirebaseAuth.getInstance();
    currentUserId = firebaseAuth.getCurrentUser().getUid();
    postsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
    userReference = FirebaseDatabase.getInstance().getReference().child("Users");
    likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
    seenRef = FirebaseDatabase.getInstance().getReference().child("Seen");

    spotsList = (RecyclerView)view.findViewById(R.id.usersSpotsRV);
    spotsList.setHasFixedSize(true);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setReverseLayout(true);
    linearLayoutManager.setStackFromEnd(true);
    spotsList.setLayoutManager(linearLayoutManager);

    displaySpots();

    // Inflate the layout for this fragment
    return view;
}


//DISPLAY USERS ALL SPOTS
public void displaySpots()
{
    Query currentUserQuery = postsRef.orderByChild("userId").startAt(thisPagesUsersId).endAt(thisPagesUsersId + "\uf8ff");

    FirebaseRecyclerAdapter<Posts, postsViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, postsViewHolder>
            (
                    Posts.class, R.layout.spot_layout, postsViewHolder.class, currentUserQuery
            )
    {
        @Override
        protected void populateViewHolder(final postsViewHolder viewHolder, Posts model, int position)
        {
            final String postKey = getRef(position).getKey();

            final DatabaseReference currentPostRef = FirebaseDatabase.getInstance().getReference().child("Posts").child(postKey);

            viewHolder.setDetails(model.getDetails());
            viewHolder.setDate(model.getDate());
            viewHolder.setImage(model.getImage());
            viewHolder.setManufacturer(model.getManufacturer());
            viewHolder.setModel(model.getModel());
            viewHolder.setProfileImage(model.getProfileImage());
            viewHolder.setTime(model.getTime());
            viewHolder.setUsername(model.getUsername());

            viewHolder.setLikeButtonStatus(postKey);
            viewHolder.setSeenButtonStatus(postKey);
        }
    };

    spotsList.setAdapter(firebaseRecyclerAdapter);
}


public static class postsViewHolder extends RecyclerView.ViewHolder
{
    View mView;

    TextView detailsTextView;

    ImageView infoIW;
    ImageView menuDotsIW;
    ImageView locationIW;
    ImageView likeIW;
    ImageView seenIW;
    TextView likesAmountTV;
    TextView seenAmountTV;

    int likesCount;
    int seenCount;

    String currentUserId;

    DatabaseReference likesRef;
    DatabaseReference seenRef;

    public postsViewHolder(View itemView)
    {
        super(itemView);

        mView = itemView;

        detailsTextView = (TextView)mView.findViewById(R.id.postDetailTV);
        detailsTextView.setVisibility(View.GONE);
        infoIW = (ImageView)mView.findViewById(R.id.postInfoIW);
        menuDotsIW = (ImageView)mView.findViewById(R.id.menuDotsIW);
        locationIW = (ImageView)mView.findViewById(R.id.postLocateIW);
        likeIW = (ImageView)mView.findViewById(R.id.likeIW);
        seenIW = (ImageView)mView.findViewById(R.id.seenIW);
        likesAmountTV = (TextView)mView.findViewById(R.id.likesAmountTV);
        seenAmountTV = (TextView)mView.findViewById(R.id.seenTV);

        likesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
        seenRef = FirebaseDatabase.getInstance().getReference().child("Seen");

        currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    }


    //SET ALL THE TEXTS AND IMAGES
    public void setDate(String date)
    {
        TextView postDate = (TextView) mView.findViewById(R.id.postDateTV);
        postDate.setText(date);
    }


    public void setDetails(String details)
    {
        TextView details_text = (TextView) mView.findViewById(R.id.postDetailTV);
        details_text.setText(details);
    }

    public void setImage(String image)
    {
        ImageView profileImage = (ImageView) mView.findViewById(R.id.postTheImage);
        Picasso.get().load(image).into(profileImage);
    }

    public void setManufacturer(String manufacturer)
    {
        TextView manufact_name = (TextView) mView.findViewById(R.id.postManufactTV);
        manufact_name.setText(manufacturer);
    }

    public void setModel(String model)
    {
        TextView model_name = (TextView) mView.findViewById(R.id.postModelTV);
        model_name.setText(model);
    }

    public void setProfileImage(String profileImage)
    {
        CircleImageView profImage = (CircleImageView) mView.findViewById(R.id.postProfpicIW);
        Picasso.get().load(profileImage).into(profImage);
    }

    public void setTime(String time)
    {
        TextView postTime = (TextView) mView.findViewById(R.id.postTimeTV);
        postTime.setText(time);
    }

    public void setUsername(String username)
    {
        TextView user_name = (TextView) mView.findViewById(R.id.postUsernameTV);
        user_name.setText(username);
    }
}

0 个答案:

没有答案