22

我正在尝试使用 RecyclerView 实现 Endless Infinite Scrolling,但我只获得前 10 条记录,没有获得接下来的 10 条记录,甚至在尝试滚动底部时也没有任何进展。

而我本想在滚动上获得接下来的 10 条记录,依此类推......但只获得前 10 条记录

在这里,我上传了我的 JSON 的副本- 但我无法从相同的 url 获取数据,这就是我使用客户端的 url 和本地主机的原因。

我正在关注教程

这是我的完整代码,我可以知道我在哪里做错了吗?

JSON:

{
  "names": [
    {
      "name": "Name 1"
    },
    {
      "name": "Name 2"
    },
    ....
    {
      "name": "Name 60"
    }
  ]
}

日志:

D/name -(13759): Name 1
D/name -(13759): Name 2
.......................
D/name -(13759): Name 60

这是我更新的代码,我用它来解析 JSON 数据

MainActivity.java:更新

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

private TextView tvEmptyView;
private RecyclerView mRecyclerView;
private DataAdapter mAdapter;
private LinearLayoutManager mLayoutManager;

private ArrayList<Student> studentList;

protected Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    tvEmptyView = (TextView) findViewById(R.id.empty_view);
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

    studentList = new ArrayList<Student>();

    handler = new Handler();
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Android Students");
    }

    loadData();

}

// load initial data
private void loadData() {       
    new Parser().execute("http://10.0.2.2/jsons/mytest.txt");               
}

class Parser extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {
            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);

                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("names");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    Student actor = new Student();

                    actor.setName(object.getString("name"));
                    Log.d("name - ", object.getString("name"));

                    studentList.add(actor);             
                }

                Log.d("MainActivity:StudentList ", "The size "+studentList.size());                 

                return true;
            }

            //------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;

    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();

        Log.d("MainActivity:StudentList ", "The size "+studentList.size());

        ArrayList< Student > temArray = 
                 new ArrayList< Student >(studentList.subList(0, 10));           
        mAdapter = new DataAdapter(temArray, mRecyclerView);

        Log.d("MainActivity:TempList ", "The size "+temArray.size());

        // set the adapter object to the Recyclerview
        mRecyclerView.setAdapter(mAdapter);     
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(MainActivity.this);

        // use a linear layout manager
        mRecyclerView.setLayoutManager(mLayoutManager);

        if (studentList.isEmpty()) {
            mRecyclerView.setVisibility(View.GONE);
            tvEmptyView.setVisibility(View.VISIBLE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            tvEmptyView.setVisibility(View.GONE);
        }

        mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                //add null , so the adapter will check view_type and show progress bar at bottom
                studentList.add(null);
                mAdapter.notifyItemInserted(studentList.size() - 1);

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //   remove progress item
                        studentList.remove(studentList.size() - 1);
                        mAdapter.notifyItemRemoved(studentList.size());
                        //add items one by one
                        int start = studentList.size();
                        int end = start + 10;

                        for (int i = start + 1; i < end; i++) {
                            mAdapter.notifyItemInserted(studentList.size());
                        }
                        mAdapter.setLoaded();
                       //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
                    }
                }, 2000);
            }
        });
    }
}

}

数据适配器.java:

public class DataAdapter extends RecyclerView.Adapter {
    private final int VIEW_ITEM = 1;
    private final int VIEW_PROG = 0;

    private List<Student> studentList;

    // The minimum amount of items to have below your current scroll position
    // before loading more.
    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;
    private boolean loading;
    private OnLoadMoreListener onLoadMoreListener;

    public DataAdapter(List<Student> students, RecyclerView recyclerView) {
        studentList = students;

        if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

            final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
                    .getLayoutManager();


                    recyclerView
                    .addOnScrollListener(new RecyclerView.OnScrollListener() {
                        @Override
                        public void onScrolled(RecyclerView recyclerView,
                                               int dx, int dy) {
                            super.onScrolled(recyclerView, dx, dy);

                            totalItemCount = linearLayoutManager.getItemCount();
                            lastVisibleItem = linearLayoutManager
                                    .findLastVisibleItemPosition();
                            if (!loading
                                    && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                                // End has been reached
                                // Do something
                                if (onLoadMoreListener != null) {
                                    onLoadMoreListener.onLoadMore();
                                }
                                loading = true;
                            }
                        }
                    });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
            int viewType) {
        RecyclerView.ViewHolder vh;
        if (viewType == VIEW_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.list_row, parent, false);

            vh = new StudentViewHolder(v);
        } else {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.progress_item, parent, false);

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

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof StudentViewHolder) {

            Student singleStudent= (Student) studentList.get(position);

            ((StudentViewHolder) holder).tvName.setText(singleStudent.getName());

            ((StudentViewHolder) holder).student= singleStudent;

        } else {
            ((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
        }
    }

    public void setLoaded() {
        loading = false;
    }

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

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }


    //
    public static class StudentViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;     

        public Student student;

        public StudentViewHolder(View v) {
            super(v);
            tvName = (TextView) v.findViewById(R.id.tvName);

            v.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(),
                            "OnClick :" + student.getName(),
                            Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

    public static class ProgressViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public ProgressViewHolder(View v) {
            super(v);
            progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
        }
    }
}

OnLoadMoreListener.java:

public interface OnLoadMoreListener {
     void onLoadMore();
}
4

8 回答 8

4

使用此代码解决它后,我遇到了同样的问题...首先..创建此类

public abstract class EndlessOnScrollListener extends OnScrollListener {

    public static String TAG = EndlessOnScrollListener.class.getSimpleName();

    // use your LayoutManager instead
    private LinearLayoutManager llm;

    public EndlessOnScrollListener(LinearLayoutManager sglm) {
        this.lm = llm;
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

        if (!recyclerView.canScrollVertically(1)) {
            onScrolledToEnd();
        }
    }

    public abstract void onScrolledToEnd();
}

第二..在你的活动中使用这个

recyclerView.addOnScrollListener(new EndlessOnScrollListener() {

    @Override
    public void onScrolledToEnd() {
        if (!loading) {
            loading = true;
            // add 10 by 10 to tempList then notify changing in data
        }
        loading = false;
    }
});

这对我有用....我希望它对你有用。

于 2015-10-25T20:34:47.927 回答
1

尝试 notifyItemRangeChanged

yourCurrentList.addAll(newData);    
mAdapter.notifyItemRangeChanged(yourCurretList.size() + 1, newDataSize);

我想这会对你有所帮助。

于 2015-10-19T06:38:53.983 回答
1

代替

private List<Student> studentList;

private List<Object> list;

也更换

@Override
public int getItemViewType(int position) {
    return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
}

@Override
public int getItemViewType(int position) {
  return list.get(position) instanceof Student ?  VIEW_ITEM : VIEW_PROG;
}

为了检测列表的结尾,您可以使用

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            // When went to the end of the list, load more posts
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                if (linearLayoutManager.findLastVisibleItemPosition() >= linearLayoutManager.getItemCount() - 1) {

                    // Grow List
                }
            }
}

也用于添加加载项。在适配器中添加此代码

public void addLoadingView(){
   list.add(new Object());
   notifyDataSetChanged();
}
于 2015-10-25T19:37:46.903 回答
0

问题 1mAdapter :您在设置LayoutManagerRecyclerView之前创建了一个新实例。DataAdapter因此,要添加的构造函数代码ScrollListener不会执行,因为recyclerView.getLayoutManager()返回null

    if (recyclerView.getLayoutManager() instanceof LinearLayoutManager){
        // code to add ScrollListener is never executed
    }

修复:首先LayoutManager为 Recyclerview 设置,然后创建适配器,如下所示:

    // use a linear layout manager
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new DataAdapter(temArray, mRecyclerView);
    // set the adapter object to the Recyclerview
    mRecyclerView.setAdapter(mAdapter);

问题 2:您曾经temArray创建DataAdapteronLoadMore()正在使用studentList添加/删除新项目,因为studentList未与mAdapter您的更改绑定不会反映在 UI 中。

修复:声明temArray为类级别变量并用于temArray操作项目。

 //class variable
 private ArrayList<Student> temArray = new ArrayList<Student>();

 handler.postDelayed(new Runnable() {
            @Override public void run() {
              //   remove progress item
              temArray.remove(temArray.size() - 1);
              mAdapter.notifyItemRemoved(temArray.size());
              //add items one by one
              int start = temArray.size();
              int end = start + 10;
              if(end<=studentList.size()){
                temArray.addAll(studentList.subList(start,end));
              }   
              mAdapter.setLoaded();
            }
          }, 2000);
于 2015-10-25T19:30:26.747 回答
0

1.

获取前 11 个 recyclerview 项目空白(并连续显示进度条),见下面的截图:

改变loadData方法为:

private void loadData() {
  new Parser().execute("http://clienturl.com/jsons/mytest.txt");  
 }

2.

而我应该得到前 10 条记录,然后滚动接下来的 10 条记录,依此类推......

as的改变onPostExecute方法Parser

protected void onPostExecute(Boolean result) {
    dialog.cancel();
    ArrayList< Student > temArray = 
                 new ArrayList< Student >(studentList.subList(0, 10));           
    mAdapter = new DataAdapter(temArray, mRecyclerView);

    // set the adapter object to the Recyclerview
    mRecyclerView.setAdapter(mAdapter);
 }

并从onCreate方法中删除以下行:

    mAdapter = new DataAdapter(studentList, mRecyclerView);

    // set the adapter object to the Recyclerview
    mRecyclerView.setAdapter(mAdapter);
于 2015-10-06T08:16:54.143 回答
0

尝试更改

int i = start + 1; i <= end; i++

在 for 循环中

int i = start + 1; i < end; i++

<=验证添加了一个额外的项目。

于 2015-10-13T03:02:21.303 回答
-1
for (int i = start + 1; i < end; i++) {
    studentList.add(add data here) ;
    mAdapter.notifyItemInserted(studentList.size());
}
于 2015-10-14T10:40:14.120 回答
-1
        mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore() {
                        //add null , so the adapter will check view_type and show progress bar at bottom
                        studentList.add(null);
                        mAdapter.notifyItemInserted(studentList.size() - 1);

                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //   remove progress item
                                studentList.remove(studentList.size() - 1);
                                mAdapter.notifyItemRemoved(studentList.size());
                                //add items one by one
                                int start = studentList.size();
                                int end = start + 10;

                                for (int i = start + 1; i < end; i++) {
   // studentList.add();
                                    mAdapter.notifyItemInserted(studentList.size());
                                }
                                mAdapter.setLoaded();
                               //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
                            }
                        }, 2000);
                    }
                });

好的,所以您插入了进度条,然后您也将其删除了,但是您从未插入下一个要显示的学生...类似 studentList.add();

我希望这能解决你的问题......祝你好运......

于 2015-10-26T00:25:09.630 回答