subrata sharma
subrata sharma

Reputation: 344

Always positioning an item at the top of recycler view

How can I show a particular item in recycler view always at the top?

public void onBindViewHolder(@NonNull final XpertViewHolder holder, final int position) {
        db = FirebaseFirestore.getInstance();
        db.collection(XPERT_MASTER_KEY).whereEqualTo("slug","xpert").get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()) {
                                xpert_name= String.valueOf(queryDocumentSnapshot.get("name"));}
                        }
                        if(xpertName.get(position).equals(xpert_name)){
                            holder.itemView.scrollTo(1,1);
                        }

                    }
                });

Upvotes: 2

Views: 421

Answers (1)

Nitish
Nitish

Reputation: 1053

I don't know what type of list you have. I am assuming you have object type list. Suppose your POJO file name is Data and you have a condition to set that item at top. for example Data have a string name title and condition is if title.equals("value")

You can use below code.

    for (int i = 0; i< dataList.size(); i++) {
            if (dataList.get(i).title.equals("value")){
                Collections.rotate(dataList, i);
            }
        }

Upvotes: 2

Related Questions