У меня есть RecyclerView, который я использую для отображения элементов в деятельности. Когда я прокручиваю до конца, я делаю еще один вызов API, чтобы получить дополнительный набор данных. Когда элементы загружаются, мой RecyclerView прокручивается вверх. Я пробовал другие решения, перечисленные без успеха. Я попытался многие, если не все предложения, упомянутые в аналогичных темах. Ни один из них не работал для меня.
activity_see_more. xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/see_more_frameLayout" />
</LinearLayout>
В SeeMoreActivity. java Я переопределяю следующее метод из MvpView
@Override
public void loadPropertiesForCategory(List<Property> propertyList) {
Preferences sharedPref = new Preferences(this);
if (!properties.containsAll(propertyList)) {
properties.addAll(propertyList);
}
sharedPref.setFilterList(properties);
ResultListFragment fragment = ResultListFragment.setTag("see_more_filter");
getSupportFragmentManager().beginTransaction().add(R.id.see_more_frameLayout, fragment, "search").commit();
}
ResultListFragment. java
private void setListOfCategoryProperties() {
page = 2;
mSearchList = mPreferences.getFilterList();
setListAdapter(false, false, View.VISIBLE, View.GONE, false);
mResultRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (linearLayoutManager != null && linearLayoutManager.findLastVisibleItemPosition() == mSearchList.size() - 1) {
((SeeMoreActivity) getActivity()).setLoadPropertiesParamsAndCallApi(page);
if (mSearchList.size() != mPreferences.getFilterList().size()) {
page++;
mSearchList = mPreferences.getFilterList();
//mAdapter.notifyDataSetChanged();
mAdapter.notifyItemInserted(mSearchList.size() - 1);
}
}
}
});
}
public void listAdapter() {
mResultRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
mResultRecycleView.setItemAnimator(new DefaultItemAnimator());
mResultRecycleView.setNestedScrollingEnabled(true);
mResultRecycleView.setAdapter(mAdapter);
}
private void setListAdapter(boolean showDeleteButton, boolean changeVisibility, int showNoResult, int showNoSavedSearch, boolean reverseFavorite) {
if (mSearchList != null && mSearchList.size() != 0) {
if (changeVisibility) {
setVisibility(View.VISIBLE, View.GONE, View.GONE);
}
mAdapter = new SearchListAdapter(R.layout.item_single_element, mSearchList, showDeleteButton, this, reverseFavorite);
listAdapter();
} else {
setVisibility(View.GONE, showNoSavedSearch, showNoResult);
}
}
public void handleTags() {
switch (mTag) {
case "result_search":
setListAdapterForHomeSearch();
break;
case "see_more_filter":
setListOfCategoryProperties();
break;
case Constants.USER_FAVORITES_KEY:
setListOfUserFavoriteProperties();
break;
}
}