1
我想創建一個ListView來分頁web服務RestFull的結果。問題是onLoadMore
總是在onCreateView
中執行,當我設置setOnScrollListener
,並且這總是返回來自webservice的所有數據。我的疑問是,只有當我需要更多的數據觸及ListView而不是當它在onCreateView
中打開時,我怎麼才能執行這個onLoadMore
。嘗試創建一個EndViewScrollListener到ListView?
我正在試着這個。
EndlessScrollListener
public abstract class EndlessScrollListener implements AbsListView.OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 2;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;
public EndlessScrollListener() {
}
public EndlessScrollListener(int visibleThreshold) {
this.visibleThreshold = visibleThreshold;
}
public EndlessScrollListener(int visibleThreshold, int startPage) {
this.visibleThreshold = visibleThreshold;
this.startingPageIndex = startPage;
this.currentPage = startPage;
}
// This happens many times a second during a scroll, so be wary of the code you place here.
// We are given a few useful parameters to help us work out if we need to load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
{
// If the total item count is zero and the previous isn't, assume the
// list is invalidated and should be reset back to initial state
if (totalItemCount < previousTotalItemCount) {
this.currentPage = this.startingPageIndex;
this.previousTotalItemCount = totalItemCount;
if (totalItemCount == 0) { this.loading = true; }
}
// If it’s still loading, we check to see if the dataset count has
// changed, if so we conclude it has finished loading and update the current page
// number and total item count.
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
currentPage++;
}
// If it isn’t currently loading, we check to see if we have breached
// the visibleThreshold and need to reload more data.
// If we do need to reload some more data, we execute onLoadMore to fetch the data.
if (!loading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + visibleThreshold)) {
onLoadMore(currentPage + 1, totalItemCount);
loading = true;
}
}
// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Don't take any action on changed
}
}
片段
public class NoticiaFrag extends Fragment {
private ListView lvNoticias;
private List<Noticia> listaNoticias = new ArrayList<Noticia>();
private NoticiaListAdapter noticiaLA;
private static final String TAG = "NoticiaFrag";
protected ProgressDialog progressDialog;
//offset start in 0
private static Integer OFFSET = 0;
//singleton
private static NoticiaFrag mFrag;
public static NoticiaFrag newInstance() {
if(mFrag == null){
mFrag = new NoticiaFrag();
}
return mFrag;
}
public NoticiaFrag() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.noticia_frag, container, false);
lvNoticias = (ListView)view.findViewById(R.id.lvNoticias);
lvNoticias.setOnScrollListener(new EndlessScrollListener() {
@Override
public void onLoadMore(int page, int totalItemsCount) {
OFFSET += 2;
moreData();
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getAllNoticias();
}
/** retorna todas as noticias */
private void getAllNoticias(){
progressDialog = new CustomProgressDialog().getCustomProgress(null, getView().getContext());
progressDialog.show();
try {
JsonObjectRequest app = new NoticiaDAO().getAllNoticias(OFFSET, new NoticiasAdapter() {
@Override
public void getAllNoticias(List<Noticia> lista) {
Log.w("SIZELIST->", lista.size() + "") ;
if(!lista.isEmpty()){
listaNoticias = lista;
noticiaLA = new NoticiaListAdapter(getView().getContext(), listaNoticias);
lvNoticias.setAdapter(noticiaLA);
}else{
Toast.makeText(getView().getContext(), "Nenhuma noticia encontrada", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
CustomVolleySingleton.getInstance().addToRequestQueue(app);
}catch (Exception e){
Log.e("ERROR: " + TAG, "Method: " + "getAllNoticias: " + e);
}
}
// Append more data into the adapter
public void moreData() {
progressDialog = new CustomProgressDialog().getCustomProgress(null, getView().getContext());
progressDialog.show();
try {
JsonObjectRequest app = new NoticiaDAO().getAllNoticias(OFFSET, new NoticiasAdapter() {
@Override
public void getAllNoticias(List<Noticia> lista) {
if(listaNoticias.size() > 0){
noticiaLA.changeLista(lista);
Log.w("MORE_DATA->", noticiaLA.getCount() + "");
}else{
Toast.makeText(getView().getContext(), "Nenhuma noticia encontrada", Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
CustomVolleySingleton.getInstance().addToRequestQueue(app);
}catch (Exception e){
Log.e("ERROR: " + TAG, "Method: " + "getAllNoticias: " + e);
}
}
@Override
public void onResume() {
super.onResume();
ChangeActionBar.changeActionBar(getActivity(), "Notícias", false, "");
}
@Override
public void onPause() {
super.onPause();
CustomVolleySingleton.getInstance().cancelPendingRequests(CustomVolleySingleton.TAG);
ChangeActionBar.changeActionBar(getActivity(), null, false, "");
OFFSET = 0;
}
}