Shimmer animation not stopping on load of RecyclerView Code Answer

I have a Recycler View that gets its data from firebase and what i want to do is that when all the data is loaded and once the recycler view is populated then the shimmer animation should stop and go away. I have tried the same with progressbar and it worked without any hiccups. But incase of shimmer animation its not stopping. I have created 2 methods one to start the animation and other other one to stop the animation. Please help me with my code and suggest what i need to change in order to achieve the desired result. I am calling the stopLoading function inside my onBindViewHolder. Thanks in advance.

public class buynow extends AppCompatActivity {

private RecyclerView recyclerView;
private DatabaseReference ProductRef;
private FirebaseRecyclerAdapter<Products, UsersViewHolder> firebaseRecyclerAdapter;
private String searchText = "";

public void showLoading(){
    ShimmerFrameLayout shimmerFrameLayout = findViewById(R.id.shimmer_view);
    shimmerFrameLayout.startShimmerAnimation();
}

public void stopLoading(){
    ShimmerFrameLayout shimmerFrameLayout = findViewById(R.id.shimmer_view);
    shimmerFrameLayout.setVisibility(View.GONE);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buynow);
    Firebase.setAndroidContext(buynow.this);

    Bundle extras = getIntent().getExtras();
    if(extras != null){
        searchText = extras.getString("phone_name");
    }

    setActionBarTitle();

    showLoading();

    Toast.makeText(buynow.this,"Loading",Toast.LENGTH_SHORT).show();

    ProductRef = FirebaseDatabase.getInstance().getReference().child("Products");

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    firebaseUserSearch(searchText);
}

private void firebaseUserSearch(String searchText){

    Query firebaseSearchQuery = ProductRef.orderByChild("pname").startAt(searchText).endAt(searchText + "uf8ff");

    FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
            .setQuery(firebaseSearchQuery,Products.class).build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {


        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Products model) {

            holder.setDetails(getApplicationContext(), model.getPname(), model.getPprice(), model.getPmrp(), model.getPcondition(), model.getPimage());
            stopLoading();
        }

        @NonNull
        @Override
        public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buy_now_phones,parent,false);
            return new UsersViewHolder(view);

        }
    };

    recyclerView.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();

}

@Override
protected void onStart() {
    super.onStart();
    firebaseRecyclerAdapter.startListening();
}

//View Holder Class
public static class UsersViewHolder extends RecyclerView.ViewHolder{

    View mView;

    private UsersViewHolder(@NonNull View itemView) {
        super(itemView);
        mView = itemView;
    }

    private void setDetails(Context context, String phoneName, String phonePrice, String phoneMrp, String phoneCondition, String phoneImage){

        TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
        TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
        TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
        TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
        ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);

        phone_name.setText(phoneName);

        //fromatting phone price
        double amount = Double.parseDouble(phonePrice);
        DecimalFormat formatter = new DecimalFormat("#,###.00");
        phone_price.setText("₹"+formatter.format(amount));

        //fromatting phone mrp
        double amount0 = Double.parseDouble(phoneMrp);
        DecimalFormat formatter0 = new DecimalFormat("#,###.00");
        phone_mrp.setText("MRP "+formatter.format(amount0));
        phone_mrp.setPaintFlags(phone_mrp.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
        phone_condition.setText(phoneCondition);
        Picasso.with(context).load(phoneImage).into(phone_image);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.action_bar,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_search) {
        Intent intent = new Intent(buynow.this,buynow_device_search.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

public void setActionBarTitle() {
    getSupportActionBar().setTitle("Used Phones");
}

}

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".buynow">

<com.facebook.shimmer.ShimmerFrameLayout
    android:id="@+id/shimmer_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            layout="@layout/place_holder_layout"/>

        <include
            layout="@layout/place_holder_layout"/>

        <include
            layout="@layout/place_holder_layout"/>

        <include
            layout="@layout/place_holder_layout"/>

        <include
            layout="@layout/place_holder_layout"/>

        <include
            layout="@layout/place_holder_layout"/>

    </LinearLayout>

</com.facebook.shimmer.ShimmerFrameLayout>


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

Answer

At first declare ShimmerFrameLayout globally.

private ShimmerFrameLayout shimmerLayout;

Then declare onCreate() section

 shimmerLayout      =findViewById(R.id.shimmer_view);

For Show case,

  shimmerLayout.setVisibility(View.VISIBLE);
  shimmerLayout.startShimmer();

For Stop case,

 if(shimmerLayout.isShimmerVisible())
       {
           shimmerLayout.stopShimmer();
           shimmerLayout.setVisibility(View.GONE);
       }

CODE EDIT

Try with

public class buynow extends AppCompatActivity {

private RecyclerView recyclerView;
private DatabaseReference ProductRef;
private FirebaseRecyclerAdapter<Products, UsersViewHolder> firebaseRecyclerAdapter;
private String searchText = "";
ShimmerFrameLayout shimmerFrameLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_buynow);
    Firebase.setAndroidContext(buynow.this);

    Bundle extras = getIntent().getExtras();
    if(extras != null){
        searchText = extras.getString("phone_name");
    }

    setActionBarTitle();

    ProductRef = FirebaseDatabase.getInstance().getReference().child("Products");

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    shimmerFrameLayout = findViewById(R.id.shimmer_view);
    shimmerFrameLayout.setVisibility(View.VISIBLE);
    shimmerFrameLayout.startShimmer();
    recyclerView.setVisibility(View.INVISIBLE);


    Toast.makeText(buynow.this,"Loading",Toast.LENGTH_SHORT).show();


    firebaseUserSearch(searchText);
}

private void firebaseUserSearch(String searchText){

    Query firebaseSearchQuery = ProductRef.orderByChild("pname").startAt(searchText).endAt(searchText + "uf8ff");

    FirebaseRecyclerOptions<Products> options = new FirebaseRecyclerOptions.Builder<Products>()
            .setQuery(firebaseSearchQuery,Products.class).build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Products, UsersViewHolder>(options) {


        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Products model) {

            holder.setDetails(getApplicationContext(), model.getPname(), model.getPprice(), model.getPmrp(), model.getPcondition(), model.getPimage());

        }

        @NonNull
        @Override
        public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buy_now_phones,parent,false);
            return new UsersViewHolder(view);

        }
    };

    shimmerFrameLayout.setVisibility(View.INVISIBLE);
    shimmerFrameLayout.stopShimmer();
    recyclerView.setVisibility(View.VISIBLE);
    recyclerView.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();

}

@Override
protected void onStart() {
    super.onStart();
    firebaseRecyclerAdapter.startListening();
}

//View Holder Class
public static class UsersViewHolder extends RecyclerView.ViewHolder{

    View mView;

    private UsersViewHolder(@NonNull View itemView) {
        super(itemView);
        mView = itemView;
    }

    private void setDetails(Context context, String phoneName, String phonePrice, String phoneMrp, String phoneCondition, String phoneImage){

        TextView phone_name = (TextView) mView.findViewById(R.id.product_name);
        TextView phone_price = (TextView) mView.findViewById(R.id.product_price);
        TextView phone_mrp = (TextView) mView.findViewById(R.id.product_mrp);
        TextView phone_condition = (TextView) mView.findViewById(R.id.product_condition);
        ImageView phone_image = (ImageView)mView.findViewById(R.id.product_image);

        phone_name.setText(phoneName);

        //fromatting phone price
        double amount = Double.parseDouble(phonePrice);
        DecimalFormat formatter = new DecimalFormat("#,###.00");
        phone_price.setText("₹"+formatter.format(amount));

        //fromatting phone mrp
        double amount0 = Double.parseDouble(phoneMrp);
        DecimalFormat formatter0 = new DecimalFormat("#,###.00");
        phone_mrp.setText("MRP "+formatter.format(amount0));
        phone_mrp.setPaintFlags(phone_mrp.getPaintFlags()| Paint.STRIKE_THRU_TEXT_FLAG);
        phone_condition.setText(phoneCondition);
        Picasso.with(context).load(phoneImage).into(phone_image);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.action_bar,menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_search) {
        Intent intent = new Intent(buynow.this,buynow_device_search.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(item);
}

public void setActionBarTitle() {
    getSupportActionBar().setTitle("Used Phones");
}

}

Related Posts

© No Copyrights, All Questions are retrived from public domain.
Tutorial Guruji