我如何对recyclerview应用限制?

问题描述

我想对recyclerview应用限制并且只在其中显示5个结果,并且在列表的末尾显示一个按钮去另一个地方?!

我的代码在下面;

适配器代码

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

我在主要片段中有一些代码,但我认为没有必要放在这个地方,但如果你想看到他们的评论,我会把所有的都放在更新文本中

谢谢

解决方法

您可以在将 list 传递给 5 之前将其切片以仅包含 RecyclerView 项,或者只需更改 getItemCount 中的 Adapter 方法以返回 {{ 1}}

5
,

您可以使用 getItemCount() as 来设置限制

@Override
public int getItemCount() {
    return 5;
}

如果你的意思是其他活动或片段,则放在另一个地方 xml 文件中 recyclerview 布局末尾的按钮。

例如:如果使用RelativeLayout,

                  <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <androidx.recyclerview.widget.RecyclerView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentTop="true"
                            android:id="@+id/recyclerview"/>
                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Next"
                            android:layout_alignParentBottom="true"
                            android:layout_below="@+id/recyclerview"
                            android:id="@+id/btn_next"/>
                        
                    </RelativeLayout>

然后点击按钮,你可以调用意图或类似的东西去你的页面

,

您可以在 ProductsList API 中设置限制查询。 如果您使用的是改造2

您可以在您的 API 接口中定义限制参数,如果您的后端服务器中有该选项,

@GET("products")
Call<Products> getProducts(@Query("limit") int limit);

并在您传递 setAdapter 的活动类中定义方法,如下所示

   public void getProducts(int limit){

    Call<Products> call = yourAPI.getProducts(limit);

    call.enqueue(new Callback<Products>() {
        @Override
        public void onResponse(Call<Products> call,Response<Products> response) {
            if (response.isSuccessful() && response.body() != null) {
                ArrayList<Products> products = response.body();
                if (products == null) products = new ArrayList<>();
                productsAdapter.setData(products); // setData needs to be defined in your adapter

            } else {
                Toast.makeText(yourActivity.this,"Server Error",Toast.LENGTH_SHORT).show(); 
            }
        }

        @Override
        public void onFailure(Call<Products> call,Throwable t) {
            if(t.getMessage() != null) Log.e("ErrorTag",t.getMessage());
            Toast.makeText(yourActivity.this,Toast.LENGTH_SHORT).show();
            }
    });

不要忘记定义一个名为 setData 的新方法,

public void setData(ArrayList<Products> productsList) {
    this.ProductsList.addAll(productsList);
    notifyDataSetChanged();
};

为了获得其他 5 个产品,我不建议您创建下一个或上一个按钮, 最好在适配器的 onBindViewHolder 方法中添加以下代码,以便在用户滚动到最后一项时再加载 5 个产品,

   if (position == (ProductsList.size() - 1)){
        if (context instanceof YourActivity){
            YourActivity activity = (YourActivity) context;
            activity.getProducts(ProductsList.size());
        }
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...