onBindViewHolder仅首先执行ViewHolder

时间:2019-03-22 22:04:35

标签: android android-recyclerview android-view android-viewholder recyclerview-layout

我有三个ViewHolders CardViewHolderTitleViewHolderGridViewHolder,用于在用户选择特定布局时在其间切换,当我尝试切换到“标题和网格布局”时,一切正常/视图,我只能看到放置在其布局中的“虚拟标题和默认图像”

这是我的 PostAdapter 类:

public class PostAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<Item> items;

    public static final int CARD_LIST = 0;
    public static final int CARD_MAGAZINE = 1;
    public static final int TITLE = 2;
    public static final int GRID = 3;

    private int viewType;

    PostAdapter(Context context, List<Item> items) {
        this.context = context;
        this.items = items;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
        notifyDataSetChanged();
    }

    public int getViewType() {
        return this.viewType;
    }



    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view;

        if (this.viewType == CARD_LIST) {
            view =  inflater.inflate(R.layout.card_layout, parent, false);
            return new CardViewHolder(view);
        } else if (this.viewType == CARD_MAGAZINE) {
            view =  inflater.inflate(R.layout.card_magazine_layout, parent, false);
            return new CardViewHolder(view);
        } else if(this.viewType == TITLE){
            view = inflater.inflate(R.layout.title_layout, parent, false);
            return new TitleViewHolder(view);
        }else {
            view = inflater.inflate(R.layout.grid_layout,parent,false);
            return new GridViewHolder(view);
        }

    }


    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemType = getItemViewType(position);
        final Item item = items.get(position);
        final Document document = Jsoup.parse(item.getContent());
        final Elements elements = document.select("img");
        if (itemType == CARD_LIST || itemType == CARD_MAGAZINE) {
            if (holder instanceof CardViewHolder) {
                CardViewHolder cardViewHolder = (CardViewHolder) holder;
                cardViewHolder.postTitle.setText(item.getTitle());
                cardViewHolder.postDescription.setText(document.text());
                Log.d("IMAGE", elements.get(0).attr("src"));
                Glide.with(context).load(elements.get(0).attr("src"))
                        .into(cardViewHolder.postImage);

        } else if (itemType == TITLE) {
            if (holder instanceof TitleViewHolder) {
                TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
                titleViewHolder.postTitle.setText(item.getTitle());

                Log.d("IMAGE", elements.get(0).attr("src"));
                Glide.with(context).load(elements.get(0).attr("src"))
                        .into(titleViewHolder.postImage);
            }
        }else {
            GridViewHolder gridViewHolder = (GridViewHolder) holder;
            gridViewHolder.postTitle.setText(item.getTitle());
            Log.d("IMAGE", elements.get(0).attr("src"));
            Glide.with(context).load(elements.get(0).attr("src"))
                    .into(gridViewHolder.postImage);


        }
    }



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

    public class CardViewHolder extends RecyclerView.ViewHolder {

        ImageView postImage;
        TextView postTitle;
        TextView postDescription;

        public CardViewHolder(View itemView) {
            super(itemView);
            postImage = itemView.findViewById(R.id.postImage);
            postTitle = itemView.findViewById(R.id.postTitle);
            postDescription = itemView.findViewById(R.id.postDescription);

        }
    }

    public class TitleViewHolder extends RecyclerView.ViewHolder {
        TextView postTitle;
        MyImageview postImage;


        public TitleViewHolder(@NonNull View itemView) {
            super(itemView);
            postTitle = itemView.findViewById(R.id.postTitle);
            postImage = itemView.findViewById(R.id.postImage);
        }
    }


    public class GridViewHolder extends RecyclerView.ViewHolder {
        TextView postTitle;
        MyImageview postImage;


        public GridViewHolder(@NonNull View itemView) {
            super(itemView);
            postTitle = itemView.findViewById(R.id.postTitle);
            postImage = itemView.findViewById(R.id.postImage);
        }
    }
}

第一个ViewHolder似乎仅在第一个if block中实现,我试图找出原因是由日志引起的,但我不知道为什么。

2 个答案:

答案 0 :(得分:0)

这是因为您没有通过覆盖getItemViewType(int position)方法来提供视图类型。您需要在适配器中添加以下方法:

@Override
public int getItemViewType(int position) {
     // this is just an example for returning the type.
     // Assuming each item containing a type
     return items.get(position).type;

     // you can also use an if condition here
     // for example
     // if(position % 2 == 0) {
     //   return CARD_LIST;
     // } else {
     //   return CARD_MAGAZINE;
     // }
}

答案 1 :(得分:0)

[已解决]

问题 发生问题是因为我正在通过方法getItemViewType检查错误的变量,应该是这样

int itemType = getViewType();