Recycler视图在android studio中未显示一张卡片视图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Recycler视图在android studio中未显示一张卡片视图相关的知识,希望对你有一定的参考价值。

我正在尝试通过回收站视图在片段的卡视图中显示阵列列表。阵列列表包含要在片段的五个卡视图上显示的五个值。但是它只显示四个。fragment的代码如下所示:

public class AddMemberFragment extends Fragment 

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private View view ;
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
String userId;
String n_team ;
private ArrayList<Friend> friends;


public AddMemberFragment() 


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


@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) 
    super.onActivityCreated(savedInstanceState);

    userId = firebaseUser.getUid();
    ManageTeamTabbedActivity activity = (ManageTeamTabbedActivity) getActivity();
    n_team = activity.getMyData();
    mRecyclerView = (RecyclerView) getView().findViewById(R.id.addNewFriend);
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);


    mRootRef.child("users").addValueEventListener(new ValueEventListener() 
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) 
            ArrayList<Friend> friendArrayList = new ArrayList<>();
            for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) 
                Log.d("demo", "USERID " + FirebaseAuth.getInstance().getCurrentUser().getUid());
                Friend friend = new Friend();
                friend.setId(userSnapshot.getKey());
                friend.setFriendName((String) userSnapshot.child("profiles").child("fname").getValue());  //Implement it
                friendArrayList.add(friend);
            
            friends = friendArrayList;
            mAdapter = new AddFriendAdapter(getActivity(), friends, n_team);
            mRecyclerView.setAdapter(mAdapter);


        

        @Override
        public void onCancelled(DatabaseError databaseError) 

        
    );

适配器代码写在下面。

public class AddFriendAdapter extends RecyclerView.Adapter<AddFriendAdapter.RecyclerViewHolder>

private ArrayList<Friend> mData;
private Context mContext;
private FirebaseAuth auth;
private String u_team ;

public AddFriendAdapter(Context mContext, ArrayList<Friend> friendList , String u_team) 
    this.mData = friendList;
    this.mContext = mContext;
    this.u_team= u_team;


@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.addnewfriend_layout, parent, false);
    AddFriendAdapter.RecyclerViewHolder viewHolder = new AddFriendAdapter.RecyclerViewHolder(view);
    return viewHolder;


@Override
public void onBindViewHolder(RecyclerViewHolder holder, final int position) 
    final Friend frnd =(Friend) mData.get(position);

    holder.friendName.setText(frnd.getFriendName());
    holder.addFriend.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            frnd.setStatus("sent");
            final DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
            final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
            final String UID = firebaseUser.getUid();
            mRootRef.child("users").addValueEventListener(new ValueEventListener() 
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) 
                    mRootRef.child("users").child(UID).child("Your Teams").child("requests").child(frnd.getId()).setValue(frnd);
                    Friend temp = new Friend();
                    temp.setId(firebaseUser.getUid());
                    temp.setFriendName(firebaseUser.getDisplayName());
                    temp.setStatus("received");
                    mRootRef.child("users").child(frnd.getId()).child("Your Teams ").child("requests").child(firebaseUser.getUid()).setValue(temp);
                    mData.remove(frnd);
                    notifyDataSetChanged();
                

                @Override

                public void onCancelled(DatabaseError databaseError) 

                
            );
        
    );


@Override
public int getItemCount() 
    return mData.size();


public class RecyclerViewHolder extends RecyclerView.ViewHolder 
    TextView friendName;
    ImageButton addFriend;

    public RecyclerViewHolder(View itemView) 
        super(itemView);
        friendName = (TextView)itemView.findViewById(R.id.textViewFriendName);
        addFriend = (ImageButton) itemView.findViewById(R.id.imagebuttonFriendAdd);
    

还附有模拟器的屏幕截图,其中在添加新成员片段中有四个卡视图。

enter image description here

我很累自己调试,发现数组列表中的条目与预期的一样是五个。 enter image description here

还附有数据库图片供参考。如图所示,它也有五个ID要显示,但回收者视图显示四个ID。enter image description here

答案

由于以下限制,我在评论栏中输入了我的评论:“您必须拥有50个声誉才能发表评论”。

[将Log.d()放入onBindViewHolder以监视值如何?使用“断点”分析onActivityCreated()处的数据并不是一个好主意,因为UI可能尚未完全填充。另外,您还可以添加第6条记录,它显示4条还是5条记录?这样,您可以找出是最后一个记录问题还是其他问题。

以上是关于Recycler视图在android studio中未显示一张卡片视图的主要内容,如果未能解决你的问题,请参考以下文章

使用 android studio java Recycler 视图从 Firebase 实时数据库中删除一个节点

Android - 为啥 Recycler 视图未显示在设备上? .我认为我的代码是正确的,但视图没有显示

我的应用程序的Recycler View问题中的Android Studio SearchView过滤器

在Recycler视图上添加图像视图时,Android导航抽屉动画滞后/减速

在 Android 的 Recycler View 中生成和设置文本视图背景的随机颜色

无法从firebase数据库android,recycler视图中检索数据