【发布时间】:2015-03-03 09:53:41
【问题描述】:
我正在尝试将更多项目附加到 Android 项目中的列表视图。这是我正在使用的代码:
public class NewsFeedActivity extends ListActivity implements FetchDataListener{
boolean loadingMore = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_feed);
SharedPreferences shared = getSharedPreferences(MainActivity.class.getSimpleName(),Context.MODE_PRIVATE);
@SuppressWarnings("unused")
String storedUID = (shared.getString(UID, ""));
final ListView list = (ListView)findViewById(android.R.id.list);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setColorSchemeResources(R.color.black, R.color.white, R.color.black, R.color.white);
swipeLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
loadingMore = true;
initView();
}
}, 1000);
}
});
list.setOnScrollListener(new OnScrollListener(){
private int currentFirstVisibleItem;
private int currentVisibleItemCount;
private int totalItem;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.isScrollCompleted();
}
private void isScrollCompleted() {
// TODO Auto-generated method stub
int lastInScreen = currentFirstVisibleItem + currentVisibleItemCount;
if((lastInScreen == totalItem) && !(loadingMore)){
//Toast.makeText(getApplicationContext(), "Loading more...", Toast.LENGTH_LONG).show();
loadingMore = true;
initView();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.currentFirstVisibleItem = firstVisibleItem;
this.currentVisibleItemCount = visibleItemCount;
this.totalItem = totalItemCount;
}
});
initView();
}
这是我用来调用列表的另外两个函数:
private void initView() {
// show progress dialog
if(!loadingMore == true){
dialog = ProgressDialog.show(this, "", "Loading...");
}
String url = SERVER_URL+getBlackCards;
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
public void onFetchComplete(List<Application> data) {
// dismiss the progress dialog
if(dialog != null) dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
setListAdapter(adapter);
if(loadingMore == true){
adapter.notifyDataSetChanged();
}
loadingMore = false;
}
当前的行为本质上只是一个刷新,所有项目都被替换,我被滚动回列表的顶部。在环顾四周后,我发现了很多使用 notifyDataSetChanged() 附加项目的示例,但它似乎对我没有影响。
【问题讨论】:
-
您没有将新数据附加到适配器,您只是在使用新数据创建新的适配器。
标签: android listview scroll notifydatasetchanged