在RecyclerView-AndroidX中的项目之间添加不同的布局

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在RecyclerView-AndroidX中的项目之间添加不同的布局相关的知识,希望对你有一定的参考价值。

我想在我的recyclerview中的项目之间添加不同的布局,我在第一个布局中有3个项目,然后在项目编号2中添加了第二个布局,所以它现在应该有4个项目,(1-FirstLayout)-(2 -SecondLayout)-(3FirstLayout)-(4FirstLayout),但是我意识到我从第二个布局中选择的位置替换为第一个布局,它变成了这样的位置(1-FirstLayout)-(2-SecondLayout)-(3FirstLayout)我不确定如何解决]

It's GONE

这是我的适配器

public class AdapterGameReview extends RecyclerView.Adapter{

public static final int TYPE_ITEM = 1;
public static final int TYPE_TOPREVIEWER = 2;
public static final int TYPE_ADSBANNER = 3;

private boolean mWithTopReviewer = true;
private boolean mWithAdsBanner = false;

private Context mContext;
private ArrayList<ModelGameReview> modelGameReviews;

private RecyclerViewClickListener mListener;

public AdapterGameReview(Context mContext, ArrayList<ModelGameReview> modelGameReviews, RecyclerViewClickListener mListener) {
    this.mContext = mContext;
    this.modelGameReviews = modelGameReviews;
    this.mListener = mListener;
}

//Container
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    View v = null;

    if (viewType == TYPE_TOPREVIEWER) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_gamereviewtopreviewer, null);
        return new GameReviewTopReviewerViewHolder(v);
    } else if (viewType == TYPE_ADSBANNER) {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_adsbannerdummy, null);
        return new GameReviewAdsBannerViewHolder(v);
    } else {
        v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_gamereview, null);
        return new GameReviewViewHolder(v, mListener);
    }

}

//Fill Container with Model Setter Getter
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    if (holder instanceof AdapterGameReview.GameReviewTopReviewerViewHolder) {
        AdapterGameReview.GameReviewTopReviewerViewHolder gameReviewTopReviewerViewHolder = (AdapterGameReview.GameReviewTopReviewerViewHolder) holder;
        /*gameReviewTopReviewerViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });*/

    }else if (holder instanceof AdapterGameReview.GameReviewAdsBannerViewHolder) {
        AdapterFollowSavedGameReviewList.ShowmoreViewHolder showmoreViewHolder = (AdapterFollowSavedGameReviewList.ShowmoreViewHolder) holder;


    } else {

        final GameReviewViewHolder GameReviewViewHolder = (GameReviewViewHolder) holder;
        final ModelGameReview modelGameReviewX = modelGameReviews.get(position);

        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.mipmap.ic_launcher);
        requestOptions.error(R.drawable.bug);
        requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
        requestOptions.centerCrop();

        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = inputFormat.parse(modelGameReviewX.getGamedate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        CharSequence niceDateStr = DateUtils.getRelativeTimeSpanString(date.getTime(), Calendar.getInstance().getTimeInMillis(), DateUtils.MINUTE_IN_MILLIS);
        //Set
        GameReviewViewHolder.TVGameDate.setText(niceDateStr);
        GameReviewViewHolder.TVGameTitle.setText(modelGameReviewX.getGametitle());
        GameReviewViewHolder.TVGameDescription.setText(modelGameReviewX.getGamedescription());
        Glide.with(mContext).load(modelGameReviewX.getGameimage())
                .apply(requestOptions)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        GameReviewViewHolder.ProgressLoadPhoto.setVisibility(View.GONE);
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        GameReviewViewHolder.ProgressLoadPhoto.setVisibility(View.GONE);
                        return false;
                    }
                })
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(GameReviewViewHolder.IMGGameImage);

        GameReviewViewHolder.TVSeenCounter.setText(String.valueOf(modelGameReviewX.getSeencounter()));
        GameReviewViewHolder.TVCommentCounter.setText(String.valueOf(modelGameReviewX.getCommentcounter()));
        GameReviewViewHolder.TVLikeCounter.setText(String.valueOf(modelGameReviewX.getLikecounter()));

        if (modelGameReviewX.getIscomment() == 0) {
            GameReviewViewHolder.IMGCommentView.setImageResource(R.drawable.comment_off);
        } else if (modelGameReviewX.getIscomment() == 1) {
            GameReviewViewHolder.IMGCommentView.setImageResource(R.drawable.comment_on);
        }

        if (modelGameReviewX.getIslike() == 0) {
            GameReviewViewHolder.IMGLikeView.setImageResource(R.drawable.heart_off);
        } else if (modelGameReviewX.getIslike() == 1) {
            GameReviewViewHolder.IMGLikeView.setImageResource(R.drawable.heart_on);
        }

        if (modelGameReviewX.getIsbookmark() == 0) {
            GameReviewViewHolder.IMGBookmarkView.setImageResource(R.drawable.saved_off);
            GameReviewViewHolder.IMGBookmarkView.setVisibility(View.GONE);
        } else if (modelGameReviewX.getIsbookmark() == 1) {
            GameReviewViewHolder.IMGBookmarkView.setImageResource(R.drawable.saved_on);
        }

        GameReviewViewHolder.TVReviewer.setText(modelGameReviewX.getReviewer());
    }

}

@Override
public int getItemCount() {
    int itemCount = 0;
    if(mWithTopReviewer == true){
        itemCount++;
    }
        itemCount = modelGameReviews.size();

    return itemCount;
}

//TYPE_ITEM
public class GameReviewViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView TVGameDate;
    TextView TVGameTitle;
    TextView TVGameDescription;
    ImageView IMGGameImage;

    TextView TVSeenCounter;
    TextView TVCommentCounter;
    TextView TVLikeCounter;

    ImageView IMGSeenView;
    ImageView IMGCommentView;
    ImageView IMGLikeView;

    TextView TVReviewer;

    ProgressBar ProgressLoadPhoto;

    ImageView IMGBookmarkView;
    private RelativeLayout ROWGameReviewContainer;

    private RecyclerViewClickListener mListener;

    public GameReviewViewHolder(View itemView, RecyclerViewClickListener listener) {
        super(itemView);

        TVGameDate = itemView.findViewById(R.id.TV_GameDate);
        TVGameTitle = itemView.findViewById(R.id.TV_GameTitle);
        TVGameDescription = itemView.findViewById(R.id.TV_GameDescription);
        IMGGameImage = itemView.findViewById(R.id.IMG_GameImage);

        TVSeenCounter = itemView.findViewById(R.id.TV_SeenCounter);
        TVCommentCounter = itemView.findViewById(R.id.TV_CommentCounter);
        TVLikeCounter = itemView.findViewById(R.id.TV_LikeCounter);

        IMGSeenView = itemView.findViewById(R.id.IMG_SeenView);
        IMGCommentView = itemView.findViewById(R.id.IMG_CommentView);
        IMGLikeView = itemView.findViewById(R.id.IMG_LikeView);

        TVReviewer = itemView.findViewById(R.id.TV_Reviewer);

        ProgressLoadPhoto = itemView.findViewById(R.id.Progress_LoadPhoto);

        IMGBookmarkView = itemView.findViewById(R.id.IMG_BookmarkView);
        ROWGameReviewContainer = itemView.findViewById(R.id.ROW_GameReviewContainer);

        mListener = listener;
        ROWGameReviewContainer.setOnClickListener(this);
        IMGCommentView.setOnClickListener(this);
        IMGLikeView.setOnClickListener(this);
        IMGBookmarkView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ROW_GameReviewContainer:
                mListener.onRowGameReviewContainerClick(ROWGameReviewContainer, getAdapterPosition());
                break;
            case R.id.IMG_CommentView:
                mListener.onRowCommentViewClick(IMGCommentView, getAdapterPosition());
                break;
            case R.id.IMG_LikeView:
                mListener.onRowLikeViewClick(IMGLikeView, getAdapterPosition());
                break;
            case R.id.IMG_BookmarkView:
                mListener.onRowBookmarkViewClick(IMGBookmarkView, getAdapterPosition());
                break;
            default:
                break;
        }
    }

}

public interface RecyclerViewClickListener {
    void onRowGameReviewContainerClick(View view, int position);
    void onRowCommentViewClick(View view, int position);
    void onRowLikeViewClick(View view, int position);
    void onRowBookmarkViewClick(View view, int position);
}

//TYPE_TOPREVIEWER
public class GameReviewTopReviewerViewHolder extends RecyclerView.ViewHolder{
    Button BTNToBeReviewer;


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

        BTNToBeReviewer = itemView.findViewById(R.id.BTN_ToBeReviewer);

    }


}

//TYPE_ADSBANNER
public class GameReviewAdsBannerViewHolder extends RecyclerView.ViewHolder{


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


    }


}



@Override
public int getItemViewType(int position) {
    if (mWithTopReviewer && isPositionTopReviewer(position))
        return TYPE_TOPREVIEWER;
    if (mWithAdsBanner && isPositionAdsBanner(position))
        return TYPE_ADSBANNER;

    return TYPE_ITEM;
}


public boolean isPositionTopReviewer(int position) {
    return position == 1 && mWithTopReviewer;
}
public boolean isPositionAdsBanner(int position) {
    return position == getItemCount() - 1 && mWithAdsBanner;
}
public void setWithTopReviewer(boolean value) {
    mWithTopReviewer = value;
}
public void setWithAdsBanner(boolean value) {
    mWithAdsBanner = value;
}

}

这是我的模特

public class ModelGameReview implements Serializable {

private int contentpage;
private String idcontent;
private String gametitle;

private String gamedate;
private String gameimage;
private String gamedescription;

private int seencounter;
private int commentcounter;
private int likecounter;

private int iscomment;
private int islike;
private int isbookmark;

private String reviewer;

private String value;
private String message;


public ModelGameReview(int contentpage, String idcontent, String gametitle, String gamedate, String gameimage, String gamedescription, int seencounter, int commentcounter, int likecounter, int iscomment, int islike, int isbookmark, String reviewer, String value, String message) {
    this.contentpage = contentpage;
    this.idcontent = idcontent;
    this.gametitle = gametitle;
    this.gamedate = gamedate;
    this.gameimage = gameimage;
    this.gamedescription = gamedescription;
    this.seencounter = seencounter;
    this.commentcounter = commentcounter;
    this.likecounter = likecounter;
    this.iscomment = iscomment;
    this.islike = islike;
    this.isbookmark = isbookmark;
    this.reviewer = reviewer;
    this.value = value;
    this.message = message;
}

public int getContentpage() {
    return contentpage;
}

public void setContentpage(int contentpage) {
    this.contentpage = contentpage;
}

public String getIdcontent() {
    return idcontent;
}

public void setIdcontent(String idcontent) {
    this.idcontent = idcontent;
}

public String getGametitle() {
    return gametitle;
}

public void setGametitle(String gametitle) {
    this.gametitle = gametitle;
}

public String getGamedate() {
    return gamedate;
}

public void setGamedate(String gamedate) {
    this.gamedate = gamedate;
}

public String getGameimage() {
    return gameimage;
}

public void setGameimage(String gameimage) {
    this.gameimage = gameimage;
}

public String getGamedescription() {
    return gamedescription;
}

public void setGamedescription(String gamedescription) {
    this.gamedescription = gamedescription;
}

public int getSeencounter() {
    return seencounter;
}

public void setSeencounter(int seencounter) {
    this.seencounter = seencounter;
}

public int getCommentcounter() {
    return commentcounter;
}

public void setCommentcounter(int commentcounter) {
    this.commentcounter = commentcounter;
}

public int getLikecounter() {
    return likecounter;
}

public void setLikecounter(int likecounter) {
    this.likecounter = likecounter;
}

public int getIscomment() {
    return iscomment;
}

public void setIscomment(int iscomment) {
    this.iscomment = iscomment;
}

public int getIslike() {
    return islike;
}

public void setIslike(int islike) {
    this.islike = islike;
}

public int getIsbookmark() {
    return isbookmark;
}

public void setIsbookmark(int isbookmark) {
    this.isbookmark = isbookmark;
}

public String getReviewer() {
    return reviewer;
}

public void setReviewer(String reviewer) {
    this.reviewer = reviewer;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

}

答案

您实施此方法有误

@Override
public int getItemCount() {
    int itemCount = 0;
    if(mWithTopReviewer == true){
        itemCount++;
    }
        itemCount = modelGameReviews.size();

    return itemCount;
}

应该是类似的东西

@Override
public int getItemCount() {
    int itemCount = modelGameReviews.size();
    if(mWithTopReviewer == true){
        itemCount++;
    }

    return itemCount;
}

以上是关于在RecyclerView-AndroidX中的项目之间添加不同的布局的主要内容,如果未能解决你的问题,请参考以下文章

为啥我不能将数据从 RecyclerView 传递到另一个 RecyclerView - AndroidX

ActionScript 3 模板系统(用散列中的项替换字符串中的项)

模板系统(用散列中的项替换字符串中的项)

如何将数据表中的项添加到列表视图?

通过将第一个列表中的第一行项与第二个列表中的项进行比较来创建新列表

ruby 选择数组中的项并递归删除它。