Loadmore ListView或RecyclerView

时间:2015-07-08 12:59:15

标签: android android-listview adapter android-adapter android-recyclerview

我正在为ListView做loadmore。当我滚动到最后一个项目时,事件已完成loadmore。一切都运行正常,除了一个问题,这是完整的数据加载更多的附加适配器,然后我仍然可以认识到新的适配器是从头开始的,当我看到listview作为重新加载而不是之前附加到ListView。 这是我的班级:

public class StoryListMenu extends Activity{

    private String URL;
    ListView listViewStory;
    private StoryListMenuAdapter adapter;
    private List<StoryMenu> listStory;
    private ProgressBar progressBar;
    private View progressLoadmore;
    private boolean loadMoreStatus;
    private boolean enableLoadMore;
    private int page;
    private int currentItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_story_list_menu);

        page = 0;
        loadMoreStatus = true;
        enableLoadMore = true;
        currentItem = 0;

        progressBar = (ProgressBar) findViewById(R.id.progress_story_list_menu);
        progressBar.setVisibility(View.VISIBLE);
        progressLoadmore = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.progress_row, null, false);

        URL = getIntent().getStringExtra("url");


        listViewStory = (ListView) findViewById(R.id.listview_story_list_menu);
        listViewStory.setDivider(null);
        listStory = new ArrayList<StoryMenu>();

        listViewStory.setOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {}
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if(loadMoreStatus && totalItemCount > 0 && firstVisibleItem + visibleItemCount == totalItemCount) {
                    loadMoreStatus = false;
                    page += AppConfig.pageSize;
                    currentItem = firstVisibleItem;
                    listViewStory.addFooterView(progressLoadmore);
                    new GetStoryList().execute();
                }
            }
        });


        new GetStoryList().execute();
    }

    private class GetStoryList extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {

            ....

                listStory.add(...);   // Data is added here
            ....
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            adapter = new StoryListMenuAdapter(listStory, getApplicationContext());
            listViewStory.setAdapter(adapter);
            progressBar.setVisibility(View.GONE);

            if(listViewStory.getFooterViewsCount() > 0) {
                listViewStory.removeFooterView(progressLoadmore);
                if(enableLoadMore)
                    listViewStory.setSelection(currentItem + 1);
                else
                    listViewStory.setSelection(currentItem);
            }

            loadMoreStatus = true;
        }

    }
}

我也遇到了与recyclerview类似的问题。 我看了但是找不到如何将数据添加到listview适配器的方法,用户不会注意到listview适配器的刷新。有没有人对我有任何想法?

**更新: 我在listStory然后notifyDataSetChanged()添加了项目。它在ListView中工作正常,但在RecyclerView中无效。在`RecyclerView&#39;我有一种方法来更新RecyclerAdapter中的数据:

public void addItem(int position, StoryMenu item) {
   listStory.add(position, item);
   notifyItemInserted(position);
  }

然后在获取更多数据后调用addItem方法。

1 个答案:

答案 0 :(得分:0)

你必须检查adapter == null并且它们创建一个新的,如果它不为null,你可以执行将刷新ListView,ReyclerView的adapter.add(items)和notifyDataSetChanged()。