Android 分页:Notifydatasetchanged() 工作但不是它应该如何工作

Android Pagination : Notifydatasetchanged() works but not how it's supposed to

我正在尝试在我的 application.The notifydatasetchanged() 中实现分页只加载一个项目而不是整个新的 20 个列表 elements.If 有人可以帮我解决这个问题吗?这就是我初始化列表的方式。

public static ArrayList<HashMap<String, String>> artistsData = new ArrayList<>();

这是我的 post 执行设置分页并通知数据集已更改,显示更多按钮:

protected void onPostExecute(ArrayList<HashMap<String, String>> list) {
    super.onPostExecute(list);

    if (pd.isShowing()) {
        pd.dismiss();
    }
    if (list != null) {
        if (ActiveProductsFragment.page == 1 ) {
            mMostViewedAdapter = new ActiveListAdapter(context, list);
            listView.setAdapter(mMostViewedAdapter);
            Utils.b = new Button(context);

            Utils.b.setText(context.getString(R.string.more));
            Utils.b.setTextColor(000000);
            Utils.b.setTextColor(context.getResources().getColor(android.R.color.white));
            Utils.b.setBackgroundColor(context.getResources().getColor(R.color.text_color));

            Utils.b.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ActiveProductsFragment.page++;

                    ActiveProductsTask artistTask = new ActiveProductsTask(context, listView);
                    artistTask.execute();
                }
            });


            listView.addFooterView(Utils.b);
        } else {


            mMostViewedAdapter = new ActiveListAdapter(context, list);
            mMostViewedAdapter.notifyDataSetChanged();
        }
    } else {
        Utils.showToastMessage(context, context.getString(R.string.no_record));
    }


}

这是我的适配器的主要代码。

 public ActiveListAdapter(Context context, ArrayList<HashMap<String, String>> data) {
    this.context = context;

    this.data = data;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

 @Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return data.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

不要像下面这样使用相同的方法创建适配器的新实例

        artistsData.clear();
        artistsData.addAll(list);
        mMostViewedAdapter.notifyDataSetChanged();

对于其他部分,请执行此操作而不是

        mMostViewedAdapter = new ActiveListAdapter(context, list);
        mMostViewedAdapter.notifyDataSetChanged();

这就是为我解决的问题

1-全局声明Arraylist

public static ArrayList<HashMap<String, String>> artistsData= new ArrayList<>();

然后在 post 上执行

if (list != null) {
        mMostViewedAdapter = new ProductShopMallAdapter(context, list);
      if (currentPage == 1) {
            listView.setAdapter(mMostViewedAdapter);
         } else {
             mMostViewedAdapter.notifyDataSetChanged();
        }
 }