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

onCreateViewHolder is being called only half of the times the actual count returned by getItemCount - Android

我目前正在使用 RecyclerView and CardView 使用 Android 应用程序。我在 RecyclerView 中显示 UserPosts(包含文本和图像)列表。 UserPost 项目是使用 CardView 创建的。

编辑

我在这里面临的问题是 onCreateViewHolder 方法被调用的次数只有 getItemCount 方法返回的实际计数的一半我滚动到列表末尾。例如,我有一个包含 10 个帖子的列表,然后 onCreateViewHolder 仅调用 5 次。它应该首先调用 10 次(当我滚动到列表末尾时),然后创建的视图将被回收。但实际上并没有发生。我访问过许多 Whosebug 帖子,但没有运气。

这是代码 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>

您看到的是预期行为。 RecylerView 不会创建与适配器的 getItemCount() returns 一样多的视图。它会根据呈现视图所需的数量创建视图。

例如,如果您的适配器包含 100 个元素,但一次只显示 5 个,系统将创建 5 个视图(它可能会再创建 1 或 2 个视图,如 "buffer")并在时重用这些视图用户滚动。

当一个 View 被回收时,它不会 "reset" 到它的原始状态,所以它和它的子 Views 仍然具有上次 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);
    }
}