【问题标题】:Recyclerview is displaying only single itemRecyclerview 仅显示单个项目
【发布时间】:2018-03-05 05:04:40
【问题描述】:

我没有记录。项目数,这给了我 17 个,但 Recyclerview 只显示 1 个。
与其他解决方案一样,我将根 layout_height 设置为 wrap_content。 我在其他应用程序中也使用过类似的代码,但在这里它不起作用。
请帮忙。我错过了什么吗?

这是我的 single_post_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:paddingStart="8dp"
    android:paddingEnd="8dp"
    android:layout_height="wrap_content">

<android.support.v7.widget.CardView
    android:id="@+id/main_blog_post"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:backgroundTint="#fff"
    android:padding="20dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="20dp">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/blog_user_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/ellipse"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.025"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.024" />

        <TextView
            android:id="@+id/blog_user_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:fontFamily="@font/oxygen_bold"
            android:text="Username"
            android:textSize="14sp"
            app:layout_constraintStart_toEndOf="@+id/blog_user_image"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/blog_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:text="Blog Date"
            app:layout_constraintStart_toEndOf="@+id/blog_user_image"
            app:layout_constraintTop_toBottomOf="@+id/blog_user_name" />

        <ImageView
            android:id="@+id/blog_image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/blog_user_image"
            app:layout_constraintVertical_bias="0.0"
            app:srcCompat="@drawable/rectangle" />

        <TextView
            android:id="@+id/blog_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"
            android:layout_marginTop="24dp"
            android:fontFamily="@font/robotobold"
            android:gravity="start"
            android:text="Lorem Ipsum Dolor Sit Amet"
            android:textSize="20sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/blog_image" />

        <TextView
            android:id="@+id/blog_desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:ellipsize="marquee"
            android:fontFamily="@font/robotoregular"
            android:gravity="start"
            android:lines="2"
            android:maxLines="2"
            android:text="@string/lorem_ipsum_paragraph"
            android:textAlignment="textStart"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/blog_title" />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>


</android.support.constraint.ConstraintLayout>

这是我的适配器:

package com.thenetwork.app.android.thenetwork.Adapters;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.google.firebase.firestore.FirebaseFirestore;
import com.thenetwork.app.android.thenetwork.HelperUtils.BlogPost;
import com.thenetwork.app.android.thenetwork.R;

import java.sql.Timestamp;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class BlogRecyclerAdapter extends 
RecyclerView.Adapter<BlogRecyclerAdapter.BlogViewHolder> {

    public List<BlogPost> blog_list;



public BlogRecyclerAdapter(List<BlogPost> blog_list){
    this.blog_list = blog_list;
}

@NonNull
@Override
public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.single_blog_list_item,parent,false);
    return new BlogViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull BlogViewHolder holder, int position) {

    String user_id = blog_list.get(position).getUser_id();
    //String timestamp = blog_list.get(position).getTimestamp().toString();
    //Log.i("Timestamp",timestamp);
    final BlogPost blogPost = blog_list.get(position);

    String blogImageUrl = blogPost.getImage_url();
    String blogThumbUrl = blogPost.getThumb_url();
    String blogTitle = blogPost.getTitle();
    String blogDesc = blogPost.getDesc();

    Log.i("blog","title : "+blogTitle);
    Log.i("blog","desc : "+blogDesc);
    Log.i("blog","image : "+blogImageUrl);
    Log.i("blog","thumb : "+blogThumbUrl);


    holder.title.setText(blogTitle);
    holder.desc.setText(blogDesc);




}

@Override
public int getItemCount() {
    Log.i("BLOG",String.valueOf(blog_list.size()));
    return blog_list.size();
}

public class BlogViewHolder extends RecyclerView.ViewHolder{

    private View mView;
    private TextView desc,title,username;
    //private TextView blogDate;
    private ImageView blogImage;
    private CircleImageView userImage;

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

        mView = itemView;
        desc = itemView.findViewById(R.id.blog_desc);
        title = itemView.findViewById(R.id.blog_title);
        username = itemView.findViewById(R.id.blog_user_name);
        //blogDate = itemView.findViewById(R.id.blog_date);
        blogImage = itemView.findViewById(R.id.blog_image);
        userImage = itemView.findViewById(R.id.blog_user_image);

    }
}

}

这是我的片段布局

package com.thenetwork.app.android.thenetwork.Fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;
import com.thenetwork.app.android.thenetwork.Adapters.BlogRecyclerAdapter;
import com.thenetwork.app.android.thenetwork.HelperUtils.BlogPost;
import com.thenetwork.app.android.thenetwork.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {

private RecyclerView blogListView;
private List<BlogPost> blog_list;
private BlogRecyclerAdapter blogRecyclerAdapter;

//firebase
private FirebaseFirestore firebaseFirestore;

public HomeFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    blog_list = new ArrayList<>();
    blogListView = view.findViewById(R.id.blog_list_view);
    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);

    blogListView.setLayoutManager(new LinearLayoutManager(container.getContext()));

    blogListView.setAdapter(blogRecyclerAdapter);

    firebaseFirestore = FirebaseFirestore.getInstance();
    firebaseFirestore.collection("Posts").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            int count = 0;
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()){

                if (doc.getType() == DocumentChange.Type.ADDED){

                    BlogPost blogPost = doc.getDocument().toObject(BlogPost.class);
                    blog_list.add(blogPost);
                    blogRecyclerAdapter.notifyDataSetChanged();

                }

            }

        }
    });

    return view;
}

}

我在其中显示 recyclerview 的片段:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.thenetwork.app.android.thenetwork.Fragments.HomeFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/blog_list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

</FrameLayout>

【问题讨论】:

  • 请发布活动布局...可能是可见性问题...
  • 您是否检查了notifyDataSetChanged 之后的列表大小进入适配器...?
  • 当我在适配器中记录大小时,它给了我正确的编号。所以我认为这应该不是问题。
  • blogListView.setLayoutManager(new LinearLayoutManager(getActivity()));使用活动上下文。
  • 发布您的fragment_home.xml

标签: android android-recyclerview


【解决方案1】:

您只是通知数据而不是将其添加到适配器。 尝试这样做

在 Adapter 内部添加一个方法来一次添加所有列表数据。

 public void addAllItems(List<BlogPost> items) {
    blog_list.clear();
    blog_list.addAll(items);
    notifyDataSetChanged();
}

在 onEvent 的最后一行添加如下代码

 blogRecyclerAdapter.addAllItems(blog_list);

并删除blogRecyclerAdapter.notifyDataSetChanged();

【讨论】:

  • 谢谢,但它对我不起作用。 Firestore 可能是什么问题?我在其他各种应用程序中使用了相同的代码。它在那里工作。
【解决方案2】:

当您获取数据时,对主片段进行一些更改,然后定义适配器对象,如下面的代码...

        if (doc.getType() == DocumentChange.Type.ADDED){

        BlogPost blogPost = doc.getDocument().toObject(BlogPost.class);
        blog_list.add(blogPost);

        blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);

        blogListView.setAdapter(blogRecyclerAdapter);
        blogRecyclerAdapter.notifyDataSetChanged();

    }

并删除 onCreateView 方法中的两行,如下所示

        blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
    blogListView.setAdapter(blogRecyclerAdapter);

【讨论】:

  • 为什么每次收到数据都要新建一个适配器?您应该将数据添加到适配器中的列表中。
  • 如果不是每次都创建 adpater 对象,则使 if 条件类似
  • 谢谢,但它仍然只显示一项。另外,我不认为这是适配器问题,因为我没有登录。适配器类中的项目,它给了我一个确切的数字。
  • 如果不是每次都创建 adpater 对象,然后创建一个方法并像 private void setAdapter(){ if (blogRecyclerAdapter==null) { if (!blog_list.isEmpty()) { blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list); blogListView.setAdapter(blogRecyclerAdapter); blogRecyclerAdapter.notifyDataSetChanged(); } } 其他 { blogRecyclerAdapter.notifyDataSetChanged(); } } 并调用此方法方法获取数据。
  • 检查博客列表数组项目大小。
【解决方案3】:

从我这边看,这是一个非常愚蠢的错误,我还在 MainActivity 中使用了一个抽屉,它与 recyclerview 重叠(我的编码不好),因此我无法滚动,看起来 recyclerview 只显示了单个项目。 对不起,谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 2019-05-25
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多