【问题标题】:how can i apply limit to recyclerview?我如何对 recyclerview 应用限制?
【发布时间】:2021-05-09 11:26:56
【问题描述】:

我想对 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);
    }
}
}

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

谢谢

【问题讨论】:

  • 能不能加分片码

标签: android android-studio android-recyclerview android-adapter


【解决方案1】:

您可以将list 分割为仅包含5 项目,然后将其传递给RecyclerView,或者只需更改Adapter 中的getItemCount 方法以返回5

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

【讨论】:

  • 当我更改 getItemCount() 时出现错误和应用程序崩溃
  • 这应该是公认的答案,因为它保存了IOB 异常。
【解决方案2】:

您可以使用 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>

然后在按钮单击时,您可以调用意图或类似内容来访问您的页面

【讨论】:

  • 我想把按钮放在滚动的末尾(我有水平回收器视图)?!
  • ​​ntalScrollView>
【解决方案3】:

您可以在 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, "Server Error", 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());
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2021-10-15
    • 2018-12-17
    相关资源
    最近更新 更多