gpt4 book ai didi

android - 在滚动时动态地将项目添加到多列 ListView

转载 作者:行者123 更新时间:2023-11-29 18:19:56 26 4
gpt4 key购买 nike

我有一个多列的 listView,我想在滚动事件的列表中动态添加 10 个项目。动态添加亮起的项目已成功,但当添加下 10 个项目时, ListView 选择返回到列表的第一个项目。

这是我的代码:

public class ViewCheckInCheckOutHistory extends Activity {
ListView historyListView;

ProgressDialog pd;
List<CheckInCheckOutHistory> checkInCheckOutHistoryList = new ArrayList<CheckInCheckOutHistory>();

ArrayList<HashMap<String, String>> historyArrayList;
SimpleAdapter histroyListAdapter;

int itemsPerPage = 10;
boolean loadingMore = false;

int firstItemCount = 0;
int lastItemCount = 10;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.view_checkin_checkout_history);

pd = ProgressDialog.show(this, "", "Please wait...");

Thread thread = new Thread() {

public void run() {

synchronized (this) {
fetchHistory();

handler.post(new Runnable() {
public void run() {
pd.dismiss();

displayUI();
};
});
}
}
};

thread.start();
}

private void displayUI() {
if ((checkInCheckOutHistoryList != null)
&& (checkInCheckOutHistoryList.size() > 0)) {
historyArrayList = new ArrayList<HashMap<String, String>>();

histroyListAdapter = new SimpleAdapter(ViewCheckInCheckOutHistory.this,
historyArrayList,
R.layout.multi_colummn_list_text_style_small,
new String[] { "assetTag", "action", "actionTime" }, new int[] {
R.id.list_content_column1,
R.id.list_content_column3,
R.id.list_content_column4});

historyListView.setOnScrollListener(new OnScrollListener(){

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

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

int lastInScreen = firstVisibleItem + visibleItemCount;

if((lastInScreen == totalItemCount) && !(loadingMore)){
Thread thread = new Thread(null, loadMoreListItems);
thread.start();
}
}
});
}
}


//Runnable to load the items
private Runnable loadMoreListItems = new Runnable() {

@Override
public void run() {
//Set flag so we cant load new items 2 at the same time
loadingMore = true;

HashMap<String, String> historyObjectMap;

System.out.println("firstItemCount : "+firstItemCount);
System.out.println("lastItemCount : "+lastItemCount);

//Get 10 new listitems
for (int i = firstItemCount; i < lastItemCount; i++) {
System.out.println("i : "+i);
CheckInCheckOutHistory checkOutHistoryObj = checkInCheckOutHistoryList.get(i);

historyObjectMap = new HashMap<String, String>();
historyObjectMap.put("assetTag", checkOutHistoryObj.getAssetTag());
historyObjectMap.put("action", checkOutHistoryObj.getAction());

historyArrayList.add(historyObjectMap);
}

runOnUiThread(returnRes);
}
};

//Since we cant update our UI from a thread this Runnable takes care of that!
private Runnable returnRes = new Runnable() {
@Override
public void run() {

//Add the new items to the adapter
if(historyArrayList != null && historyArrayList.size() > 0){
histroyListAdapter.notifyDataSetChanged();
}

if (checkInCheckOutHistoryList.size() - lastItemCount > 10) {
firstItemCount = lastItemCount;
lastItemCount = lastItemCount + itemsPerPage;
} else {
if (checkInCheckOutHistoryList.size() - firstItemCount > 10) {
firstItemCount = lastItemCount;
lastItemCount = checkInCheckOutHistoryList.size();
} else {
if (checkInCheckOutHistoryList.size() - firstItemCount > 0){
firstItemCount = lastItemCount;
lastItemCount = checkInCheckOutHistoryList.size();
}
}
}

historyListView.setAdapter(histroyListAdapter);

//Done loading more.
loadingMore = false;
}
};
}

“fetchHistory()”是将值添加到“checkInCheckOutHistoryList”中的函数。

在此先感谢您的帮助。

最佳答案

displayUI() 方法中,您每次都创建一个新的适配器并将其设置到 listView 而不是使用 Adapter.notifyDataSetChanged()

关于android - 在滚动时动态地将项目添加到多列 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6067905/

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