gpt4 book ai didi

android - 如何将 OnScrollListener 实现到 ListView ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:55:13 27 4
gpt4 key购买 nike

我从服务器获得 500 条记录..我想在 ListView 中显示 10 个项目,当 ListView 结束时我需要加载另外 10 个,依此类推..我在网上看到很多例子,但我无法解决请帮助我。

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
Seen = (ImageView) findViewById(R.id.SeenStatus);


new GetDescendingDate().execute();
userArray = new ArrayList<listmodel>();

ListView lv = getListView();


lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String Id = ((TextView) view.findViewById(R.id.txtId))
.getText().toString();

// Starting single activity
Intent in = new Intent(getApplicationContext(),
details.class);
in.putExtra("TAG_Id", Id);
startActivity(in);


});



}

}


private class GetDescendingDate extends AsyncTask<Void, Void, Void> {

@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Atab.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();

}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected Void doInBackground(Void... arg0) {

JSONParser jParser = new JSONParser();

// Getting JSON Array node
JSONArray json;

session = new SessionManager(getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
String email = user.get(SessionManager.KEY_EMAIL);
String Status = "New";
String get;
get = URL.Get;
String url_new = get + email + "/" + Status + "/" + "SODD"
+ "/" + "null" + "/" + "0" + "/" + "10";
try {
json = jParser.getJSONFromUrl(url_new);

if (json != null) {
// looping through All Orders
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);

String number = c.getString("No");
String By = c.getString("By");
String Type = c.getString("Type");
String Descripton = c.getString("Desc");
String Date = c.getString("DateTime");
String Id = c.getString("Id");
String ONo = c.getString("ONo");
String FirstName = c .getString("FirstName");
String Price = c.getString("Price");


listmodel list = new listmodel();
list.setId(Id);
list.setBy(By);
list.setType(Type);
list.setDescription(Descripton);
list.setDate(Date);
list.setPrice(Price);
list.setName(FirstName);

userArray.add(list);

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

else {
Log.e("New", "Couldn't get any data from the url");

}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

return null;
}

protected void onPostExecute(Void result) {
super.onPostExecute(result);

if (pDialog.isShowing())
pDialog.dismiss();

adapter = new adapter(Atab.this,userArray);
setListAdapter(adapter);


}

}

最佳答案

你可以这样实现..

 listView.setOnScrollListener(new 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;


}

private void isScrollCompleted() {
if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
&& this.currentScrollState == SCROLL_STATE_IDLE) {
/** To do code here*/


}
});

关于android - 如何将 OnScrollListener 实现到 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28741645/

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