Recyclerview Loadmore搜索后无法正常工作

时间:2017-02-27 10:07:53

标签: android android-recyclerview adapter

过去几天,面对这个问题。 RecyclerView onscroll到Loadmore和EditText搜索工作正常。但是当我搜索数据并获得相应的结果列表然后如果我滚动 RecyclerView 然后Loadmore无法正常工作。

MainActivity

 public class MainActivity extends FragmentActivity {

    private RecyclerView mRecyclerView;

    private  LinearLayoutManager mLayoutManager;
    private  EditText editsearch;
    private RecyclerAdapter mAdapter;

    private int ival = 1;
    private int loadLimit = 10;

    ArrayList<Student> mDataSet = new ArrayList();

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

        ArrayList<Student> my_Dataset = getDataSet();
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new RecyclerAdapter(my_Dataset);
        mRecyclerView.setAdapter(mAdapter);

        editsearch = (EditText) findViewById(R.id.et_search);

        // add the listener for EditText
        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                mAdapter.filter(text);
            }
        });

        mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(
                mLayoutManager) {
            @Override
            public void onLoadMore(int current_page) {

                loadMoreData();

            }

        });

    }

    private ArrayList<Student> getDataSet()
    {

        for (int i = 1; i <=10; i++)
        {
            Student std=new Student();
            std.setName("Root" + i);
            mDataSet.add(std);
        }
        return mDataSet;
    }

    private void loadMoreData() {

       loadLimit = ival +5;

        for (int i = ival; i <= loadLimit; i++) {
            Student st = new Student("Student " + i,false);
            mDataSet.add(st);
            ival++;
        }

        mAdapter.notifyDataSetChanged();

    }
}

RecyclerAdapter

 public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

    private ArrayList<Student> mDataset;
    private ArrayList<Student> mCleanCopyDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.tv_recycler_item);
        }
    }

    public RecyclerAdapter(ArrayList<Student> dataset) {
        mDataset = dataset;
        mCleanCopyDataset = mDataset;
    }

    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                         int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_item, parent, false);

        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.mTextView.setText(mDataset.get(position).getName());

    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        mDataset = new ArrayList<Student>();
        if (charText.length() == 0) {
            mDataset.addAll(mCleanCopyDataset);
        } else {
            for (Student item : mCleanCopyDataset) {
               /* if (item.toLowerCase(Locale.getDefault()).contains(charText)) {
                    mDataset.add(item);
                }*/
                if(item.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
                    mDataset.add(item);
                }
            }
        }
        notifyDataSetChanged();
    }
}

这是 EndlessRecyclerOnScrollListener

我是Android-studio的新手。有没有人遇到过这个问题?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

问题是您在想要添加项目的位置丢失了参考。您目前的步骤是:

  1. 在主要活动ArrayList<Student> mDataSet = new ArrayList();
  2. 中创建列表
  3. 在mainactivty中创建适配器并传递my_Dataset(my_Dataset是mDataSet) mAdapter = new RecyclerAdapter(my_Dataset);
  4. Init适配器(在这里保存mDataSet的引用) mDataset = dataset; mCleanCopyDataset = mDataset;
  5. 在此处过滤mDataset = new ArrayList<Student>();,您将失去参考
  6. <强>解决方案

    将适配器内的mDataset重命名为mFilteredDataSet并更改构造函数:

    public RecyclerAdapter(ArrayList<Student> dataset) {
      mFilteredDataset = new ArrayList<>(); // init your filtered dataset just one time and clear it on filtering
      mFilteredDataset.addAll(dataset); // here you have your copy of the dataset
      mCleanCopyDataset = dataset; // this is your reference of the complete dataset
    }
    

    更改过滤器功能中的mDataset = new ArrayList<Student>();mFilteredDataset.clear()

    不要忘记在mAdapter.filter()结束时致电loadMoreData(),因为您向适配器添加了更多数据,但新添加的数据未包含在已过滤的列表中。