gpt4 book ai didi

android - 如何用 RecyclerView 实现无限滚动?

转载 作者:搜寻专家 更新时间:2023-11-01 09:36:14 25 4
gpt4 key购买 nike

我有一个回收器,里面有卡片 View ,我从 REST 服务中获取信息,我正在尝试实现无限滚动,假设用户每次向下滚动时都会看到 10 个卡片 View ,直到没有要显示更多的卡片 View ,我该如何实现?

我看过几个例子,但没有一个能真正帮助我了解如何去做。我什至不知道我需要在 adapter.class 或 Fragment.class 中放入什么,因为我不知道如何实现它,如果有人能告诉我实现无限滚动的正确方法,那就太好了我的代码...

提前致谢。

MainAdapter.class

public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> implements View.OnClickListener
{
private ArrayList<Business> businessList;
private Activity activity;
private int layoutMolde,idb;

public MainAdapter(Activity activity, ArrayList<Business> list, int layout)
{
this.activity = activity;
this.businessList = list;
layoutMolde = layout;
}

@Override
public MainAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_row, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
holder.mTitle.setText(businessList.get(position).getBusiness_name());
holder.number_rating.setText(businessList.get(position).getRating().toString());
Glide.with(activity).load(businessList.get(position).getLogo_url_string()).into(holder.mImg);
}

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

@Override
public void onClick(View v)
{

}

public class ViewHolder extends RecyclerView.ViewHolder {

public TextView mTitle;
public ImageView mImg;
public ImageView logo;
public RatingBar main_rating;
public TextView number_rating;

public ViewHolder( View itemView)
{
super(itemView);
mTitle = (TextView) itemView.findViewById(R.id.nom_business_main);
number_rating = (TextView) itemView.findViewById(R.id.number_rating);
mImg = (ImageView) itemView.findViewById(R.id.img_main);
main_rating=(RatingBar) itemView.findViewById(R.id.rating_main);
main_rating.setRating((float)1);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{

Intent in = new Intent(v.getContext(), BusinessPremium.class);
int position = getAdapterPosition();
idb = businessList.get(position).getId();
in.putExtra("no", idb);
v.getContext().startActivity(in);
}
});
}
}
}

FeedsFragment.class

    public class FeedsFragment extends Fragment
{

private ArrayList<Business> arrayBusiness,arrayBasics;
private Gson gson;

private static final Type BUSINESS_TYPE = new TypeToken<ArrayList<Business>>() {}.getType();
private RecyclerView.LayoutManager mLayoutManager;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View android = inflater.inflate(R.layout.fragment_feeds, container, false);

if (!internetConnectionCheck(FeedsFragment.this.getActivity()))
{
Toast.makeText(getApplicationContext(), "Error de Conexión", Toast.LENGTH_LONG).show();
}

new RequestBase(getActivity()) {

@Override
public JsonObject onHttpOk(JsonObject response) throws JSONException {

JsonObject objeto, pagination_details = null, details, premium_img;
JsonArray data;

if (getActivity() == null)
return response;

if (response.get("pagination") == null)
{
objeto = response;

} else {
objeto = response;
pagination_details = response.get("pagination").getAsJsonObject();
data = objeto.get("data").getAsJsonArray();
gson = new Gson();
arrayBusiness = gson.fromJson(data, BUSINESS_TYPE);
Log.d("size", String.valueOf(arrayBusiness.size()));
FeedsFragment.this.getActivity().runOnUiThread(new Runnable()
{
@Override
public void run()
{

RecyclerView recycler = (RecyclerView) FeedsFragment.this.getActivity().findViewById(R.id.recycler_main);
MainAdapter adapter = new MainAdapter(getActivity(), arrayBusiness, R.layout.main_row);

recycler.setNestedScrollingEnabled(false);
mLayoutManager = new GridLayoutManager(FeedsFragment.this.getActivity(), 2);
recycler.setLayoutManager(mLayoutManager);
recycler.setAdapter(adapter);

GifTextView loading = (GifTextView)FeedsFragment.this.getActivity().findViewById(R.id.loading);
TextView loadingText = (TextView)FeedsFragment.this.getActivity().findViewById(R.id.loadingText);
loading.setVisibility(View.GONE);
loadingText.setVisibility(View.GONE);

}
});
}
if (pagination_details.isJsonNull()) {
Log.d("Paginacion", pagination_details.toString());
}
return objeto;
}

@Override
public void onHttpCreate(JsonObject response) throws JSONException
{

}

@Override
public void onHttpUnprocessableEntity(JsonObject response) throws JSONException
{
this.cancel(true);
final String error = response.get("errors").toString();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(), error, Toast.LENGTH_LONG).show();
}
});
}
}.execute("businesses/premiums", "GET");

return android;
}
}

最佳答案

您可以在 android 中使用 SwipeRefreshLayout 刷新并在刷新覆盖方法中调用您的 api

注意:将您的 API 调用请求放在一个方法中,然后在您的 SwipeRefreshLayout 的 onRefresh 方法中调用该方法

关于android - 如何用 RecyclerView 实现无限滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42991161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com