gpt4 book ai didi

android - 在 android 中滚动时使用改造从 API 获取下 10 个项目的分页

转载 作者:行者123 更新时间:2023-11-29 01:09:49 25 4
gpt4 key购买 nike

在 android 中滚动时使用改造从 API 获取下 10 个项目的分页

最佳答案

相反,

如果您使用带有 BaseAdapter 的简单 ListView ,则用于分页,那么你可以按照下面的代码。

使用 2 arrayList 进行分页。一种是加载所有listview项(allDataList),一种是在adapter中设置10项(paginationList)。

首先使用retrofit加载allDataList中的所有数据。

public static int itemsCount = 10; 
//i need pagination of 10 records, so here i passed 10, you may increase this no. here as your requirement.

ArrayList<User_pojo> allDataList, paginationList;
Adapter_UserList adapter;
ListView listViewUser;
View footerView;
LayoutInflater layoutInflater;
int lastItem,preLast;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

context = this;
listViewUser = (ListView) findViewById(R.id.listViewUser);

allDataList = new ArrayList<>();
paginationList = new ArrayList<>();
layoutInflater = LayoutInflater.from(context);
footerView = layoutInflater.inflate(R.layout.load_more_view, null);
listViewUser.addFooterView(footerView);

Call_api();

listViewUser.setOnScrollListener(new AbsListView.OnScrollListener() {

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {

}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

lastItem = (firstVisibleItem + visibleItemCount);
if (lastItem == totalItemCount) {
if (preLast != lastItem) {
//to avoid multiple calls for last item

preLast = lastItem;

footerView.findViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
footerView.postDelayed(new Runnable() {
@Override
public void run() {
itemsCount += 10;
LoadMoreList();
listViewUser.setSelection(lastItem - 1); //to display more loaded records @ current displayed position.
}
}, 1500);
}
}
}
});

}


@Override
public void onStart() {
super.onStart();
LoadMoreList();

}

public interface AppService{

@FormUrlEncoded
@POST("getUserList")
Call<User_pojo> getUserList(@FieldMap(encoded = true) Map<String, String> map);

}

void Call_api() {

Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://your_api_url_here.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();


Call<User_pojo> call = retrofit.create(AppService.class).getUserList(getUserMap());

call.enqueue(new Callback<User_pojo>() {
@Override
public void onResponse(Call<User_pojo> call, Response<User_pojo> response) {
Log.e("Response", new Gson().toJson(response.body()));
if (response.body().getResponse().equalsIgnoreCase("true")) {
allDataList = response.body().getData();
Log.e("size", allDataList.size() + "");
LoadMoreList();
}
}

@Override
public void onFailure(Call<User_pojo> call, Throwable t) {
t.printStackTrace();
t.getMessage();
Log.e("Failed", t.getMessage());
}
});
}

private void LoadMoreList() {
try {
Log.e("size", String.valueOf(allDataList.size()));
if (allDataList.size() >= itemsCount) {
paginationList.clear();
for (int i = 0; i < itemsCount; i++) {
paginationList.add(allDataList.get(i));
}
Log.e("paginationList_Size", String.valueOf(paginationList.size()));
adapter = new Adapter_UserList(context, paginationList);
} else {
adapter = new Adapter_UserList(context, allDataList);
listViewUser.removeFooterView(footerView); // loaded all list then simply remove footerView.
}

if (footerView != null)
footerView.findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);

listViewUser.setAdapter(adapter);
} catch (Exception ignored) {

}
}


private Map<String, String> getUserMap() {
Map<String, String> map = new HashMap<>();
map.put("uid", "001");
map.put("token", "5526");

Log.e("logs", map.toString());
return map;
}

希望这对您有所帮助。

关于android - 在 android 中滚动时使用改造从 API 获取下 10 个项目的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43971798/

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