Android ListView 追加更多项目 notifyDataSetChanged() 不工作

Android ListView append more items notifyDataSetChanged() not working

我正在尝试将更多项目附加到 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() 来附加项目的示例,但它似乎对我没有影响。

The current behavior is essentially just a refresh where all items are replaced and I am scrolled back up to the top of the list

因为这里:

 ApplicationAdapter adapter = new ApplicationAdapter(this, data);
 setListAdapter(adapter);  

每次创建 adapter 的新对象 class 而不是在 ListView 的当前适配器中添加新项目。

这样做:

1. 在 class 级别创建 ApplicationAdapter 对象为:

ApplicationAdapter adapter;
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog

    if(dialog != null)  dialog.dismiss();
    // create new adapter
    if(adapter==null){
    adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);  
    }else{
         // update current adapter
    }
    loadingMore = false;
}

2.ApplicationAdapter中创建一个addAllItems方法class:

public void addAllItems(List<Application> data){
  this.data.addAll(data);
  this.notifyDataSetChanged();
}

3. 现在在 onFetchComplete 方法的 else 部分调用 addAllItems:

  adapter.addAllItems(data);