Solution 1 :
From what I understand, you need to handle 2 states:
- LOADING – When the search button is clicked and you are getting your data from Api Or Db.
- LOADED – When the data is fetched, and set in the adapter of the recyclerview.
// In loading state,clear data, hide recyclerview and show progressbar
public void showLoadingState() {
pb_Search.setVisibility( View.VISIBLE );
rv_items.setVisbility(View.GONE);
// Clear your adapter here, otherwise old content will
// show when you do 2 searches in a row.
}
// In loaded state, show recyclerview and hide progressbar
public void showLoadedState() {
pb_Search.setVisibility( View.GONE );
rv_items.setVisbility(View.VISIBLE);
}
Problem :
I created an EditText
that is used in order to search.
Once the user click the search button I show a ProgressBar
until the RecyclerView
is loaded by doing this:
et_Search.setOnEditorActionListener( (v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
InputMethodManager imm =(InputMethodManager) getSystemService( Context.INPUT_METHOD_SERVICE);
View view = this.getCurrentFocus();
imm.hideSoftInputFromWindow( view.getWindowToken(), 0);
pb_Search.setVisibility( View.VISIBLE );
query = et_Search.getText().toString();
fetchAddItems(query);
return true;
}
return false;
} );
Where:
private void fetchAddItems(String query) {
B_client = new ItemClient();
B_client.getItems( query, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
List<Item> Items;
try {
JSONArray items;
if (response != null) {
items = response.getJSONArray( "items" );
Items = Item.fromJson( items );
AddItemsAdapter = new AddItemAdapter(Items, listenerAdd);
rv_Items.setAdapter(AddItemsAdapter);
rv_Items.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
pb_Search.setVisibility( View.GONE );
}
});
}
} catch (JSONException ignored) {
}
}
} );
}
Everything works ok.
The problem is in the case where the user performs two searches in a row.
In this case, the RecyclerView
is already filled from the first search, and therefore the ProgressBar
won’t show.
Is there a way to do what I want in both cases when the RecyclerView is filled/updated?
Thank you
SOLUTION:
What solved my problem is removing the listener and using this:
rv_Items.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rv_Items.setVisibility( View.VISIBLE );
pb_Search.setVisibility( View.GONE );
rv_Items.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Comments
Comment posted by Ben
Im not sure if this is the problem since I do reset my list from my understanding. I added the code.
Comment posted by Aman Arora
I only see you setting new data in adapter, but if in layout hierarchy your recyclerview is above the progressbar, then on search click progress won’t show if you don’t hide the recyclerview.