微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – ReyclerView不能正常工作

我正在尝试在recyclerview上显示我的模型列表.但由于某种原因,它没有显示任何东西.我从FirebaseDatabase获取列表项并将所有数据保存在“videoList”上.然后我想呈现我收到的数据,但它没有显示任何内容.

请告诉我我做错了什么.

  public class MyPlayListFragment extends Fragment {
    private FirebaseDatabase refToVideos;
    private FirebaseUser currentUser;
    private ArrayList<Video> videosList;
    private VideoViewAdapter adapter;
    private RecyclerView rvVideos;


public MyPlayListFragment() {
    // required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_my_play_list, container, false);
    rvVideos = (RecyclerView)v.findViewById(R.id.rvVideos);

    return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    videosList = new ArrayList<>();
    refToVideos = FirebaseDatabase.getInstance();
    currentUser = FirebaseAuth.getInstance().getCurrentUser();

    refToVideos.getReference(Params.VIDEOS).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                videosList.add(snapshot.getValue(Video.class));
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    adapter = new VideoViewAdapter(getContext(),videosList);
    rvVideos.setLayoutManager(new linearlayoutmanager(getContext()));
    rvVideos.setAdapter(adapter);

}




public class VideoViewAdapter extends RecyclerView.Adapter<VideoViewHolder>{
  private LayoutInflater inflater;
  private Context context;
  private ArrayList<Video> data;

  public VideoViewAdapter( Context context, ArrayList<Video> data) {
      this.context = context;
      this.inflater = LayoutInflater.from(context);
      this.data = data;
  }

  @Override
  public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = inflater.inflate(R.layout.video_item,parent,false);
      return new VideoViewHolder(v);
  }

  @Override
  public void onBindViewHolder(VideoViewHolder holder, int position) {
      Video video = data.get(position);
      holder.tvTitleVideo.setText(video.getVideoName());
      holder.tvLikes.setText(String.valueOf(video.getLikes()));
      holder.tvViews.setText(String.valueOf(video.getViews()));
      Uri uri = Uri.parse(video.getVideoURL());
      holder.videoViewUser.setVideoURI(uri);
      holder.videoViewUser.start();

  }

  @Override
  public int getItemCount() {
      return 0;
  }
}

public  class VideoViewHolder extends RecyclerView.ViewHolder{
    private TextView tvTitleVideo,tvLikes,tvViews;
    private VideoView videoViewUser;

    public VideoViewHolder(View itemView) {
        super(itemView);
        tvTitleVideo = (TextView) itemView.findViewById(R.id.videoTitle);
        tvLikes = (TextView)itemView.findViewById(R.id.tvLikes);
        tvViews = (TextView)itemView.findViewById(R.id.tvViews);
        videoViewUser = (VideoView)itemView.findViewById(R.id.videoViewUser);

      }
    }

 }

解决方法:

您应该在getItemCount()中返回数据ArrayList的大小

用这个:

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

而不是这个:

@Override
public int getItemCount() {
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐