我是Android新手。我的要求是加载额外的数据和当滚动到达底部时,设置为列表视图。我可以调用下一个数据,但我以前的数据是隐藏的。如何纠正这个?
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view,
int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
// TODO Auto-generated method stub
if (listView.getLastVisiblePosition() == listView
.getAdapter().getCount() - 1
&& listView.getChildAt(
listView.getChildCount() - 1)
.getBottom() <= listView
.getHeight()) {
// scroll end reached
if (nextCall == null || nextCall == ""
|| nextCall.equals("null")
|| nextCall.equals("")) {
} else {
hostname=nextCall;
new ArticlesList().execute();
}
// Write your code here
}else{
}
}
});
dialog.dismiss();
} else {
Toast.makeText(getActivity(), "Went wrong",
Toast.LENGTH_LONG).show();
dialog.dismiss();
}
答案 0 :(得分:3)
访问此内容,描述如何在listview中进行分页。 PagingListView
答案 1 :(得分:1)
在listView适配器中:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (position == items.size() - 1)
{
items.add("new data1");
items.add("new data2");
notificationDataSetChanged();
}
//inject your view here
}
您丢失了数据,因为您正在覆盖指向该数据的链接。您应该添加数据,而不是替换它。
答案 2 :(得分:1)
你需要的是一个无尽的滚动听众,请看一下:https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews
只需将此项目添加到您的项目中并使用它:
yourListView.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
... your asynchTask
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
super.onScrollStateChanged(view, scrollState);
}
});
答案 3 :(得分:-1)
我使用以下代码段解决了同样的问题。
private int lastVisibleItem; private LinearLayoutManager linearLayoutManager; private int lastId; //首次从服务器加载数据后获取帖子的最后一个ID。 private int flag = 0;
linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView_news.setLayoutManager(linearLayoutManager);
recyclerView_news.addOnScrollListener(new RecyclerView.OnScrollListener(){
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState){
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
new Handler().post(new Runnable() {
@Override
public void run() {
if (list_news.size() - 1 == lastVisibleItem) {
if (flag == 0) {
flag = 1;
getMoreData(lastId); // pass lastId to load next posts.
}
} else if (list_news.size() - 1 != lastVisibleItem) {
flag = 0;
}
}
});
}
}); 希望这会有所帮助。感谢。