I have two fragments: A & B.
Fragment B contains SearchView and RecyclerView.
adapter = new SearchListAdapter(items);
recyclerView = new RecyclerView(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.addRule(BELOW, SEARCH_VIEW_ID);
addView(recyclerView, params);
@Override
public boolean onQueryTextChange(String newText)
{
ArrayList<MyModel> filteredModelList = adapter.filter(newText);
recyclerView.scrollToPosition(0);
return true;
}
Implementing filtering is taken away: How to filter a RecyclerView with a SearchView
PROBLEM
1) At the beginning we are on a fragment A, then go to the fragment B is administered in SearchView more characters and get a filtered list (for example consisting of a single element).
2) Next, through getFragmentManager().popBackStack(); we return to fragment A.
3) At the same time, in the fragment B in the method of onStop(); we clean SearchView:
@Override
public void onStop()
{
if(searchView != null)
{
searchView.setQuery("", false);
}
}
4) Next, we return to the fragment B and get an error:
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 1(offset:35).state:35
I've been looking at this problem and found the following:
a) Issue 77846
b) Issue 77232
c) habra
d) recyclerview layoutmanager part 1 (generateDefaultLayoutParams)
e) IndexOutOfBoundsException Invalid item position XX(XX). Item count:XX
He came to the conclusion that this is a bug recyclerview and it has not yet been fixed.
As a result, it was necessary to implement filtering without animation:
public void filter(String charText)
{
charText = charText.toLowerCase();
mItems = new ArrayList<MyModel>();
if (charText.length() == 0)
{
mItems.addAll(mOriginalItems);
}
else
{
for (MyModel model : mOriginalItems)
{
String textId = model.getId().toLowerCase();
if (textId.contains(charText))
{
mItems.add(model);
}
}
}
notifyDataSetChanged();
}
Is this true, and there is an error in the code?
Thank you!
UPDATE
Activity:
public class MyActivity extends AppCompatActivity
SimpleFragmentManager:
private static void moveTo(BaseFragment fragment)
{
if(fragment != null)
{
FragmentManager fragmentManager = _activity.getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
BaseFragment currentFragment = getCurrentFragment();
if(fragment != currentFragment)
{
ft.replace(_containerId, fragment, fragment.getType());
if (currentFragment != null) ft.addToBackStack(null);
ft.commit();
}
}
}
Fragment A & B:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if(view == null)
{
view = new ALayout(container.getContext());
//if fragment B then > view = new BLayout(container.getContext());
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
}
return view;
}
ALayout & BLayout is extends RelativeLayout
BLayout with SearchView and RecycleView I showed above.
Important Notice:
1) if comment out line: recyclerView.scrollToPosition(0);
- no bug! (and no scroll top :) )
2) More precise conditions for obtaining bug:
a) Go to the fragment_1 track on the fragment_2;
b) Enter a query in the search (String is present: recyclerView.scrollToPosition(0));
c) Return to fragment_1 (this time a search query to clean - no errors!);
d) We will again go to the fragment_2.
>>We get a error!
a) Go to the fragment_1 track on the fragment_2;
b) Do not enter anything in the search (String is present: recyclerView.scrollToPosition (0));
c) Go back to the fragment_1. (this time a search query to clean - no errors!);
d) Once again we are going to fragment_2.
>>No error!