Как показать все элементы списка в recyclerview?

У меня есть адаптер, который показывает ограниченные элементы на фрагменте.

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

и я хочу показать все элементы списка в recyclerview, когда я нажимаю кнопку ViewAll, используя тот же адаптер в другой деятельности.

это мой адаптер. `

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);
        }
    }
}

`

Как этого добиться? Или есть другой способ добиться этого? Пожалуйста, помогите мне.

Сохраните глобальное логическое значение и установите его по щелчку View All . если установлено вернуть latestProductModelList.size() в противном случае вернуть math.min

ADM 16.05.2022 09:48
0
1
28
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

Вы можете использовать следующие коды для вашего адаптера:

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);

Другие вопросы по теме