onCreateViewHolder只被调用getItemCount返回的实际计数的一半 - Android

时间:2015-11-21 06:59:06

标签: android android-recyclerview android-cardview

我目前正在使用RecyclerViewCardView处理Android应用。我在RecyclerView中显示了UserPosts列表(包含文本和图像)。 UserPost项目已使用CardView创建。

修改

我面临的问题是 onCreateViewHolder 方法只被调用 getItemCount 方法返回的实际计数的一半,即使我滚动到列表的末尾。例如,我有10个帖子的列表,然后 onCreateViewHolder 只调用5次。它应该首先调用10次(当我滚动到列表的末尾),然后创建视图将被回收。但事实并非如此。我访问了许多Stackoverflow帖子,但没有运气。

以下是代码 UserPostAdapter.java

    package com.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.model.UserPostsListItem;
import com.musomeet.R;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by SI_Android on 10/21/2015 .
 *
 */

public class UserPostAdapter extends RecyclerView.Adapter<UserPostAdapter.UserPostViewHolder> {

    private List<UserPostsListItem> userPostList;
    Context conetxt ;
    LayoutInflater inflater ;
    public UserPostAdapter(){

    }
    public UserPostAdapter(Context conetxt, List<UserPostsListItem> postlist) {
        this.userPostList = postlist;
        this.conetxt = conetxt ;
        inflater = LayoutInflater.from(this.conetxt);
    }

    @Override
    public int getItemCount() {
        //int size = 0 ;
        //if( userPostList != null )
            //size = userPostList.size();
        return userPostList.size();
    }

    @Override
    public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {

        UserPostsListItem item = userPostList.get(position);
        videoViewHolder.userName.setText(item.getName());
        videoViewHolder.userLocation.setText(item.getCity()+", "+item.getCountry());
        videoViewHolder.timeAgo.setText(item.getAgo());
        videoViewHolder.likes.setText(item.getNo_of_likes());
        videoViewHolder.comments.setText(item.getNo_of_comments());
        videoViewHolder.userMessage.setText(item.getContent());

      if(!TextUtils.isEmpty(item.getPhoto())) {
            Picasso.with(conetxt).load(item.getPhoto()).into(videoViewHolder.circleImageView);
        }
        String url = item.getAttachment_url() ;
        if (!TextUtils.isEmpty(url)) {
            if ((url.endsWith(".png") || url.endsWith(".jpg"))) {
                //videoViewHolder.attachment.setVisibility(View.VISIBLE);
                Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
            }
        }
    }
    @Override
    public UserPostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View itemView = inflater.inflate(R.layout.userposts_listitem_textmessage, viewGroup, false);
        return new UserPostViewHolder(itemView);
    }
    public static class UserPostViewHolder extends RecyclerView.ViewHolder {
        TextView userName, userLocation, timeAgo, likes, comments, userMessage;
        ImageView attachment;
        CircleImageView circleImageView;
        public UserPostViewHolder(View v){
            super(v);
            attachment = (ImageView) v.findViewById(R.id.attachment);
            circleImageView = (CircleImageView) v.findViewById(R.id.circleView);
            userMessage = (TextView) v.findViewById(R.id.massage);
            userName = (TextView) v.findViewById(R.id.name);
            userLocation = (TextView) v.findViewById(R.id.location);
            timeAgo = (TextView) v.findViewById(R.id.time);
            likes = (TextView) v.findViewById(R.id.likespoints);
            comments = (TextView) v.findViewById(R.id.commentspoints);
        }
    }
}

以下是 userposts_listitem_textmessage.xml

    <?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardView="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/userpopstcardview"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    cardView:cardElevation="5dp"
    cardView:cardCornerRadius="10dp"
    cardView:cardBackgroundColor="#fff"
    >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/contact"
        android:layout_margin="10dp"
        android:id="@+id/circleView"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp">

     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:text="Jasmine"
        android:textSize="20sp"
        android:textStyle="bold"
        />
     </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="5dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    <TextView
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#929292"
        android:text="Los Angles, California"
        android:textSize="14sp"
        android:textStyle="normal"
        />
    </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#929292"
                android:text="-"
                android:textSize="14sp"
                android:textStyle="normal"
                />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#929292"
                android:text="2 hours ago"
                android:textSize="14sp"
                android:textStyle="normal"
                />
        </LinearLayout>
    </LinearLayout>
    </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/userListContentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/massage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/recet_text"
            android:textColor="#000"
            android:textSize="15sp"
            android:padding="10dp"
            android:scrollbars="vertical"/>

        <ImageView
            android:id="@+id/attachment"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="10dp"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:tint="#00000000"
            android:src="@drawable/contact"
            android:visibility="visible"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/likespoints"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:textColor="#929292"
                android:textSize="14sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Likes"
                android:textColor="#929292"
                android:textSize="14sp"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginLeft="30dp">
            <TextView
                android:id="@+id/commentspoints"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"
                android:textColor="#929292"
                android:textSize="14sp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Comments"
                android:textColor="#929292"
                android:textSize="14sp"
                android:layout_marginLeft="5dp"/>

        </LinearLayout>
    </LinearLayout>

        <LinearLayout
            android:layout_height="1dp"
            android:layout_width="match_parent">
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#eeeeee"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:orientation="horizontal"
            android:weightSum="3">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/like"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Like"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:textSize="14sp"
                    android:textColor="#929292"
                    android:padding="10dp"
                    android:clickable="true"
                    android:background="@drawable/textview_effect"/>
            </LinearLayout>
            <LinearLayout
                android:layout_height="match_parent"
                android:layout_width="1dp">
                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="#eeeeee"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/comment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Comment"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:textSize="14sp"
                    android:textColor="#929292"
                    android:padding="10dp"
                    android:clickable="true"
                    android:background="@drawable/textview_effect"/>
            </LinearLayout>
            <LinearLayout
                android:layout_height="match_parent"
                android:layout_width="1dp">
                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:background="#eeeeee"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/share"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Share"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:textSize="14sp"
                    android:textColor="#929292"
                    android:padding="10dp"
                    android:clickable="true"
                    android:background="@drawable/textview_effect"/>
            </LinearLayout>

        </LinearLayout>
</LinearLayout>
</LinearLayout>

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

2 个答案:

答案 0 :(得分:3)

您看到的是预期的行为。 RecylerView不会创建尽可能多的视图,因为适配器的getItemCount()返回。它创建视图,尽可能多地渲染视图。

例如,如果你的适配器包含100个元素,但是当时只显示5个元素,系统将创建5个视图(它可以创建1或2个视图作为&#34;缓冲区&#34;)并重用这些视图当用户滚动时。

答案 1 :(得分:1)

当视图被回收时,它不会被重置&#34;到它的原始状态,所以它和它的子视图仍然具有ViewHolder最后一次传递到onBindViewHolder()时在它们上设置的属性。由于您目前仅在if条件为true时设置图像,因此如果其中任何一个条件为false,则不会更新ImageView,并且仍会在其上设置图像先前。当这些条件不是true时,您只需将ImageView的位图设置为null。此外,您可以合并两个if语句:

@Override
public void onBindViewHolder(UserPostViewHolder videoViewHolder, int position) {

    UserPostsListItem item = userPostList.get(position);
    ...

    String url = item.getAttachment_url() ;
    if (!TextUtils.isEmpty(url) && (url.endsWith(".png") || url.endsWith(".jpg"))) {
        Picasso.with(conetxt).load(url).into(videoViewHolder.attachment);
    }
    else {
        videoViewHolder.attachment.setImageBitmap(null);
    }
}