如何在 recyclerview 中显示列表的所有项目?

How to show all items of list in recyclerview?

我有一个适配器在 Fragment 上显示有限的项目。

 @Override
public int getItemCount() {
    int limit = 7;
    return Math.min(latestProductModelList.size(),limit);
}

当我在另一个活动上使用相同的适配器单击 ViewAll 按钮时,我想在 recyclerview 中显示列表的所有项目。

这是我的适配器。 `

public class LatestProductAdapter extends RecyclerView.Adapter<LatestProductAdapter.ViewHolder> {
    List<LatestProductModel> latestProductModelList = new ArrayList<>();
    Context context;
    LatestProductClickInterface latestProductClickInterface;

    public LatestProductAdapter(Context context, LatestProductClickInterface latestProductClickInterface) {
        this.context = context;
        this.latestProductClickInterface = latestProductClickInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LatestProductModel latestProductModel = latestProductModelList.get(position);
        Glide.with(context).load(latestProductModel.getImage()).into(holder.itemImage);
        holder.itemTitle.setText(latestProductModel.getTitle());
        holder.itemView.setOnClickListener(v -> {
            latestProductClickInterface.OnLatestProductClicked(latestProductModelList.get(position));
        });
    }


    @Override
    public int getItemCount() {
        int limit = 7;
        return Math.min(latestProductModelList.size(),limit);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void updateList(List<LatestProductModel> latestProductModels){
        latestProductModelList.clear();
        latestProductModelList.addAll(latestProductModels);
        Collections.reverse(latestProductModelList);
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView itemImage;
        TextView itemTitle;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemImage = itemView.findViewById(R.id.item_img);
            itemTitle = itemView.findViewById(R.id.item_title);
        }
    }
}

`

如何实现? 还是有另一种方法来实现这一目标? 请帮助我。

您可以为您的适配器使用以下代码:

public class LatestProductAdapter extends RecyclerView.Adapter<LatestProductAdapter.ViewHolder> {
List<LatestProductModel> latestProductModelList = new ArrayList<>();
Context context;
LatestProductClickInterface latestProductClickInterface;
private boolean shouldShowAllItems;

public LatestProductAdapter(Context context, LatestProductClickInterface latestProductClickInterface , boolean shouldShowAllItems) {
    this.context = context;
    this.latestProductClickInterface = latestProductClickInterface;
    this.shouldShowAllItems = shouldShowAllItems;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    LatestProductModel latestProductModel = latestProductModelList.get(position);
    Glide.with(context).load(latestProductModel.getImage()).into(holder.itemImage);
    holder.itemTitle.setText(latestProductModel.getTitle());
    holder.itemView.setOnClickListener(v -> {
        latestProductClickInterface.OnLatestProductClicked(latestProductModelList.get(position));
    });
}


@Override
public int getItemCount() {
    if (shouldShowAllItems){
        return latestProductModelList.size();
    }else {
        int limit = 7;
        return Math.min(latestProductModelList.size(), limit);
    }
}

@SuppressLint("NotifyDataSetChanged")
public void updateList(List<LatestProductModel> latestProductModels){
    latestProductModelList.clear();
    latestProductModelList.addAll(latestProductModels);
    Collections.reverse(latestProductModelList);
    notifyDataSetChanged();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    ImageView itemImage;
    TextView itemTitle;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        itemImage = itemView.findViewById(R.id.item_img);
        itemTitle = itemView.findViewById(R.id.item_title);
    }
    }
}

并根据您的需要创建适配器对象:

LatestProductAdapter latestProductAdapter = LatestProductAdapter(context , this ,//true or false);