Recyclerview仅显示单个项目

时间:2018-03-05 05:04:40

标签: android android-recyclerview

我没有记录。项目,这给了我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>

3 个答案:

答案 0 :(得分:1)

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

  

在适配器内部添加一个方法,一次添加所有列表数据。

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

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

 blogRecyclerAdapter.addAllItems(blog_list);

并删除blogRecyclerAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

在获取数据的时候对home片段进行一些更改,以便定义适配器对象,如下面的代码......

        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);

答案 2 :(得分:0)

这是一个非常愚蠢的错误,我还在MainActivity中使用了一个抽屉,该抽屉与recyclerview重叠(我的编码不正确),因此我无法滚动它,并且看起来recyclerview仅显示单个项目。 抱歉,谢谢大家。