I am using Volley for requests and want to add shimmer while data is loading. I can perform it with Handler but I don’t know when data will come and can’t set exact time.
Here is code in my Fragment. I want to set value false
to variable isShimmer
and it makes so fast so my shimmer is not running:
String url = "my_url"; JsonArrayRequest getArticleOfTheDayRequest = new JsonArrayRequest(Request.Method.GET, url, null, response -> { try { for(int i = 0; i < response.length(); i++){ JSONObject articleObject = response.getJSONObject(i); int article_id = articleObject.getInt("article_id"); String title = articleObject.getString("title"); String text = articleObject.getString("text"); ArticleOfTheWeek articleOfTheWeek = new ArticleOfTheWeek(article_id, title, text); articleOfTheWeekList.add(articleOfTheWeek); } articleOfTheWeekAdapter = new ArticleOfTheWeekAdapter(getContext(), articleOfTheWeekList); recyclerView.setAdapter(articleOfTheWeekAdapter); articleOfTheWeekAdapter.isShimmer = false; articleOfTheWeekAdapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } }, error -> Log.d("Error.Response", error.getMessage() + " ") );
Here code in my adapter:
public boolean isShimmer = true; int shimmerNumber = 2; @Override public void onBindViewHolder(@NonNull final ArticleOfTheWeekViewHolder holder, int position) { final ArticleOfTheWeek articleOfTheWeek = articleOfTheWeekList.get(position); if(isShimmer) { holder.shimmerFrameLayout.startShimmer(); }else { holder.shimmerFrameLayout.stopShimmer(); holder.shimmerFrameLayout.setShimmer(null); holder.textViewTitle.setBackground(null); holder.textViewTitle.setText(articleOfTheWeek.getTitle()); holder.textViewDesc.setBackground(null); holder.textViewDesc.setText(Html.fromHtml(articleOfTheWeek.getText())); } }
As you see if I set isShimmer
true, shimmer runs infinite. So I can’t get a period when all data is loaded to change value of isShimmer
Advertisement
Answer
I’ve used this library for show shimmer effect while fetching data. First set visibility true to your shimmer layout in your xml file then start shimmer on the start of the activity shimmerViewContainer.startShimmer()
.
Set shimmerViewContainer.stopShimmer()
in your volley method after set data to your adapter in activity file.
As well as stop shimmer on onPause()
and onDestroy()