[英]Nested RecyclerView dynamic data loading
I have a RecyclerView nested in a scrollView in one of my activities. 我的一项活动中,有一个RecyclerView嵌套在scrollView中。
I implemented dynamic data loading for the recycler so that when x visible objects are left it will automatically issue an API call to fetch the next items. 我为回收站实现了动态数据加载,因此当剩下x个可见对象时,它将自动发出API调用以获取下一个项目。
for some reason, being nested in a scroll view cancels that out. 由于某种原因,嵌套在滚动视图中可以消除这种情况。 I actually have a couple of these API calls when activity starts, maybe because the scroll view wraps the content of the recycler, and the recycler wraps its own content.
在活动开始时,实际上我有几个API调用,可能是因为滚动视图包装了回收者的内容,而回收者也包装了自己的内容。 at any rate this is bad for me, and even worse is the fact that further dynamic loads are not performed when I scroll down recycler content.
无论如何,这对我来说是不好的,更糟糕的是,当我向下滚动回收器内容时,不会执行进一步的动态加载。
I did manage to fix bad scrolling behaviour by using NestedScrollView and recycler.setNestedScrollingEnabled(false). 我确实通过使用NestedScrollView和recycler.setNestedScrollingEnabled(false)来解决了不良的滚动行为。
I cant plant the the content above the recyclerView that I want to scroll along with the recycler as a static header in the recycler layout as it is not static content but 2 other recyclers and a bunch of other stuff I need my activity to have control over. 我无法将要与回收器一起滚动的内容放置在recyclerView上方,作为回收器布局中的静态标题,因为它不是静态内容,而是2个其他回收器和一堆其他东西,我需要我的活动来控制。
any ideas? 有任何想法吗?
THis is late but you could try adding an OnScrollListener
it always works for me even when RecyclerView
is nested: 太晚了,但是您可以尝试添加一个
OnScrollListener
,即使嵌套了RecyclerView
它也总是对我RecyclerView
:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private List<Movie> movies;
private RecyclerView recyclerView;
private GridLayoutManager gridLayout;
private MoviesAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
movies = new ArrayList<>();
getMoviesFromDB(0);
gridLayout = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(gridLayout);
adapter = new MoviesAdapter(this, movies);
recyclerView.setAdapter(adapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (gridLayout.findLastCompletelyVisibleItemPosition() == movies.size() - 1) {
getMoviesFromDB(movies.get(movies.size() - 1).getId());
}
}
});
}
private void getMoviesFromDB(int id) {
AsyncTask<Integer, Void, Void> asyncTask = new AsyncTask<Integer, Void, Void>() {
@Override
protected Void doInBackground(Integer... movieIds) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.1.35/movies.php?id=" + movieIds[0])
.build();
try {
Response response = client.newCall(request).execute();
JSONArray array = new JSONArray(response.body().string());
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
Movie movie = new Movie(object.getInt("id"), object.getString("movie_name"),
object.getString("movie_image"), object.getString("movie_genre"));
MainActivity.this.movies.add(movie);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
adapter.notifyDataSetChanged();
}
};
asyncTask.execute(id);
}}
With this,only when the user scrolls will the api call check for more.Kind of like in google play store. 有了这个,只有当用户滚动时,api调用才会检查更多。类似于Google Play商店中的种类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.