Click here to Skip to main content
15,302,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my recyclerview their are countdown timer in each item..and when the first item countdown timer ends i want the item at the tom to go straight to bottom. The cycler should go on, till their are items in recylerview. I know how to add countdowntimer. Please let me know how to do the positioning logic Right image is what is required. eUEry — ImgBB[^]

What I have tried:

I know how to put timer and how to populate the recylerview..just help me in positiong the items
Posted
Comments
David Crow 17-Sep-21 8:14am
   
"just help me in positiong the items"

Use the add() method of the list object that is associated with the adapter.
Kumar Harsh 2021 17-Sep-21 8:49am
   
Can you please elabourate a bit..add() is giving me an infinte recylerview
David Crow 17-Sep-21 9:46am
   
I'm assuming that all of the items being shown in the RecyclerView are contained in some sort of List object. For example, if item 0 needs to be moved to the end, do something like:
ItemInfo itemInfo = list.remove(0);
list.add(itemInfo);
adapter.notifyDataSetChanged();
Kumar Harsh 2021 17-Sep-21 10:06am
   
Sir please Check this code..and let me know where to add the above 3 line which you told....


coursesArrayList = new ArrayList<>();
rcv.setHasFixedSize(false);
rcv.setItemViewCacheSize(4);
rcv.setLayoutManager(new LinearLayoutManager(getContext()));

// adding our array list to our recycler view adapter class.
myAdapter = new MyAdapter(coursesArrayList, this);

// setting adapter to our recycler view.
rcv.setAdapter(myAdapter);



// below line is use to get the data from Firebase Firestore.
// previously we were saving data on a reference of Courses
// now we will be getting the data from the same reference.
db.collection("categories").orderBy("categoryTime", Query.Direction.ASCENDING)
.addSnapshotListener(new EventListener<querysnapshot>() {
@Override
public void onEvent(@Nullable @org.jetbrains.annotations.Nullable QuerySnapshot value, @Nullable @org.jetbrains.annotations.Nullable FirebaseFirestoreException error) {
if (!value.isEmpty()) {
shimmerFrameLayout.stopShimmer();
shimmerFrameLayout.setVisibility(View.INVISIBLE);

List<documentsnapshot> list = value.getDocuments();

coursesArrayList.clear(); //clear old refresh new
for (DocumentSnapshot d : list) {
// after getting this list we are passing
// that list to our object class.
UserDet ud = d.toObject(UserDet.class);

assert ud != null;
ud.setCategoryID(d.getId());

// and we will pass this object class
// inside our arraylist which we have
// created for recycler view.
coursesArrayList.add(ud);

}
myAdapter.notifyDataSetChanged();
} else {
// if the snapshot is empty we are displaying a toast message.
Toast.makeText(getActivity() , "No data found in Database", Toast.LENGTH_SHORT).show();
}
}
});
David Crow 17-Sep-21 10:19am
   
According to your "...when the first item countdown timer ends i want the item at the tom to go straight to bottom..." comment, you would add that code at the point where the item's timer is ending.
Kumar Harsh 2021 17-Sep-21 10:33am
   
This is my adapter class...please let me know if here i have to insert the code.. I have user countdown timer in the onebindholder method....


public class MyAdapter extends RecyclerView.Adapter<myadapter.holder> {
// creating variables for our ArrayList and context
private ArrayList<userdet> UserArrayList;
private HomeFragment context;
String timey ;


// creating constructor for our adapter class
public MyAdapter(ArrayList<userdet> UserArrayList, HomeFragment context) {
this.UserArrayList = UserArrayList;
this.context = context;
}

@NonNull
@Override
public MyAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new Holder(LayoutInflater.from(context.getActivity()).inflate(R.layout.onerowforrecview, parent, false));
}

@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
// setting data to our text views from our modal class.
UserDet userDet = UserArrayList.get(position);

holder.ques_name_h.setText(userDet.getCategoryName());
}





long timer = userDet.getCategoryTime().toDate().getTime();
Date today = new Date();

final long currentTime = System.currentTimeMillis();

long expiryTime = timer - currentTime;


new CountDownTimer(expiryTime, 1000) {
public void onTick(long millisUntilFinished) {
long seconds = millisUntilFinished / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;

timey = days+"d, " +hours % 24 + "h left";

holder.ques_time_h.setText(timey);
}

public void onFinish() {
holder.ques_amount_h.setButtonColor(R.color.teal_200);
holder.itemView.setEnabled(false); //makig disclick
holder.ques_time_h.setText("Time up!");
holder.ques_amount_h.setEnabled(false); //btn disabled


Handler handler = new Handler();
Runnable run= new Runnable() {
@Override
public void run() {
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = 0;
params.width=0;

holder.itemView.setLayoutParams(params);
handler.postDelayed(this,5000);
}
};
handler.post(run);




}
}.start();



holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent= new Intent(context.getActivity(),TournamentDetails.class);
context.startActivity(intent);
}

});


}

@Override
public int getItemCount() {
// retu
Kumar Harsh 2021 26-Sep-21 9:44am
   
What is ItemInfo here can you please tell?
David Crow 26-Sep-21 21:35pm
   
It was just an example of a data type object that gets added to the list. It could be anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900