简体   繁体   English

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

[英]How to show all items of list in recyclerview?

I have an adapter that is showing limited Items on Fragment.我有一个适配器,它在片段上显示有限的项目。

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

and I want to show all the items of list in recyclerview when I click on ViewAll button using the same adapter on another acitivity.当我在另一个活动上使用相同的适配器单击ViewAll按钮时,我想在 recyclerview 中显示所有列表项。

this is my adapter.这是我的适配器。 ` `

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

` `

How to achieve this?如何做到这一点? Or is there another way to achieve this?还是有另一种方法可以实现这一目标? kindly help me.请帮助我。

You can use bellow codes for your adapter:您可以为适配器使用以下代码:

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

and create adapter object accourding your need:并根据您的需要创建适配器对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RecyclerView 未显示列表中的所有项目 - RecyclerView is not showing all items in the list 如何在包含 NestedScrollView 的视图中显示 RecyclerView 的所有项目? - how to show all items of a RecyclerView in a view that contains a NestedScrollView? Android-如何更改RecyclerView的小部件的可见性并将其显示在recyclerView的所有项目中? - Android - How to change the visibility of a widget of and show it in all items in recyclerView? 如何仅从 RecyclerView 的列表中显示 51 到 100 之间的项目 - How to show items between 51 to 100 only from list on RecyclerView RecyclerView 不显示 Fragment 中的所有项目 - RecyclerView does not show all items inside Fragment EndlessRecyclerViewScrollListener 不会在 recyclerview 上显示所有项目 - EndlessRecyclerViewScrollListener does not show all items on recyclerview 如何在RecyclerView中访问所有项目 - How to access all items in RecyclerView 如何对recyclerview中的列表项进行分类? - How to categorize list items in a recyclerview? 将列表中的项目分组并汇总所有其他 Android 字段,在 recyclerview 中显示结果 - Group items in a List and sum all other fields Android, Show the result in recyclerview 如何在recyclerview中显示所有复选框? - How to show all checkboxes in recyclerview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM