gpt4 book ai didi

android - 使用 setOnScrollListener 从服务器仅获取 20 行

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:46:18 25 4
gpt4 key购买 nike

这是我第一次使用 onscroll 监听器。我的问题是每次向下滚动时如何获取 20 个新行?。

我知道当我向下滚动 GridView 时将执行此代码。

public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub

this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;

if ((totalItemCount - visibleItemCount) <= (firstVisibleItem + 20)) {
// the below when executed will get all the rows ,I only need 20 rows
handler.execute().get();

}
}

这是我的代码

// the load more in the grid view, fetch new images.
mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
private int currentVisibleItemCount;
private int currentScrollState;
private int currentFirstVisibleItem;
private int totalItem;
private LinearLayout lBelow;

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
this.currentScrollState = scrollState;
this.isScrollCompleted();
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub

this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;

if ((totalItemCount - visibleItemCount) <= (firstVisibleItem + 20)) {
// load new 20 row but how to do that

}
}

private void isScrollCompleted() {
if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
&& this.currentScrollState == SCROLL_STATE_IDLE) {
Log.d("a", "poooppii");

}
}

;
});

这是连接发生的地方

 protected void showList(){
try {

JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray("result");
System.out.println("Length:"+peoples.length());
int J_length=peoples.length()-1;


jsonObj= peoples.getJSONObject(J_length);


Listitem = new ArrayList<Listitem>();

for (int i = 0; i < peoples.length(); i++) {
JSONObject c = peoples.getJSONObject(i);

String id = c.getString("id");
String url = c.getString("url");

int intid = 0;
try {
intid = Integer.parseInt(id.toString());
} catch (NumberFormatException nfe) {
System.out.println("Could not parse " + nfe);
}

Listitem.add(new Listitem(id, url));
System.out.println(Listitem);
}
//}
if (mListener != null)
mListener.myMethod(Listitem);


} catch (JSONException e) {
e.printStackTrace();
}

}

最佳答案

您需要为数据加载维护baselimit。初始化你的基础和限制变量。

int base = 0, limit = 20;

每当加载新项目时增加您的 base 变量。

base = base + limit;
handler.execute().get();

在您的API 调用中传递baselimit 变量,以便您可以在API 的查询中使用它> 侧面。

注意:base 是一个值,您需要从该位置开始获取值。 limit 是您需要获取的行数的值。

引用this用于sqlite中的限制查询。
引用this用于MySql中的limit查询。
引用this用于 SQL 中的限制查询。

关于android - 使用 setOnScrollListener 从服务器仅获取 20 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37898974/

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