Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
103 views
in Technique[技术] by (71.8m points)

android - how can i apply limit to recyclerview?

i want apply limit to recyclerview and only show 5 result in it and in the end of the list show a button for go to another place?!

my code in below;

adapter codes:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.Holder> {

ArrayList<Products> ProductsList;
Context context;

public ProductAdapter(ArrayList<Products> productsList, Context context) {
    ProductsList = productsList;
    this.context = context;
}

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

@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {

    Products products = ProductsList.get(position);

    holder.txtName.setText(products.getName());
    holder.txtPrice.setText(products.getPrice());
    Picasso.get().load(Config.ip_value + "/images/" + products.getPhoto()).into(holder.imgV);

}

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

public class Holder extends RecyclerView.ViewHolder {

    TextView txtName;
    TextView txtPrice;
    ImageView imgV;

    public Holder(@NonNull View itemView) {
        super(itemView);
        txtName = itemView.findViewById(R.id.rowTxtProductName);
        txtPrice = itemView.findViewById(R.id.rowTxtPrice);
        imgV = itemView.findViewById(R.id.rowImgProduct);
    }
}
}

i have some codes in main fragment but i dont think its necessary to put in this place but if you want to see them comment and i will put all in update text

thanks

question from:https://stackoverflow.com/questions/66057884/how-can-i-apply-limit-to-recyclerview

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can either slice the list to contain only 5 items before passing it to RecyclerView or just change getItemCount method in Adapter to return 5

@Override
public int getItemCount() {
    return ProductsList.size() > 5 ? 5 : ProductsList.size();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...