使用OnScroll侦听器实现CustomListview,以便从服务器加载内容。在backpress上,片段返回容器中的home片段。再次加载相同的列表视图,在按钮单击时,即使创建了子片段的新实例,也只有最后几个项被加载到列表视图中。
对数据集中的任何更改都会调用 notifydatasetchanged()
。
代码onclicklistener
用于显示fragment
:
card.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragmentManager = getActivity().getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentManager.popBackStack();
fragmentTransaction.replace(R.id.Container, new frag()).addToBackStack("card").commit();
}
Asynctask
用于在onScroll
方法中加载适配器。
lv.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;
}
private void isScrollCompleted() {
if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
&& this.currentScrollState == SCROLL_STATE_IDLE) {
// if ( this.currentScrollState == SCROLL_STATE_IDLE){
/** To do code here*/
scrollpos = this.totalItem;
int lastPos = this.currentFirstVisibleItem + this.currentVisibleItemCount;
if (lastPos == scrollpos && !loadingMore) {
if (this.totalItem < jsonobjectcount-1) {
int index = lv.getFirstVisiblePosition();
new Card.Getmembers().execute();
} else {
lv.removeFooterView(pb);
}
}
}
}