我已经尝试为现有的 ListView 实现 SwipeRefreshLayout,但是每当我运行程序时,即使我通过拉动刷新它,它也只显示一个项目。但是,刷新动画有效。以下是相关代码:
items = (ListView)findViewById(R.id.myList);
adapter = new Adapter(ctx);
adapter.setTextKey("myItem");
//adapter.setImageKey("image");
items.setAdapter(adapter);
adapter.loadObjects();
final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe2);
swipeView.setEnabled(false);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(true);
( new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
}
}, 3000);
}
});
items.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
boolean enable = false;
if(items != null && items.getChildCount() > 0){
// check if the first item of the list is visible
boolean firstItemVisible = items.getFirstVisiblePosition() == 0;
// check if the top of the first item is visible
boolean topOfFirstItemVisible = items.getChildAt(0).getTop() == 0;
// enabling or disabling the refresh layout
enable = firstItemVisible && topOfFirstItemVisible;
}
swipeView.setEnabled(enable);
}
});