При нажатии на лайк в RecyclerView мгновенно перекидывает на начало страницы
У меня есть лента RecyclerView, которая содержит посты или же объявления как в соцсетях, но при нажатии на кнопку лайка меня перебрасывает на начало RecyclerView. Код ленты содержится во фрагменте, ниже приведены код функционала и код макета одного поста в xml
public class HomeFragment extends Fragment {
DatabaseReference mDataBase;
RecyclerView rv;
LinearLayoutManager linearLayoutManager;
ArrayList<Announcment> announcements;
ArrayList<String> keys;
RecyclerViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
init(view);
getUrlAnnouncement();
return view;
}
public void init(View view) {
mDataBase = FirebaseDatabase.getInstance().getReference("Announcements");
rv = view.findViewById(R.id.rvHome);
linearLayoutManager = new LinearLayoutManager(getContext());
rv.setLayoutManager(linearLayoutManager);
announcements = new ArrayList<>();
keys = new ArrayList<>();
adapter = new RecyclerViewAdapter(announcements, getContext());
}
public void loadProduct() {
// RecyclerViewAdapter adapter = new RecyclerViewAdapter(announcements, getContext());
rv.setAdapter(adapter);
}
public void getUrlAnnouncement() {
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// if (announcements.size() > 0) {
// announcements.clear();
// }
ArrayList<Announcment> newAnnouncements = new ArrayList<>();
for (DataSnapshot ds : snapshot.getChildren()) {
Announcment ancmnt = ds.getValue(Announcment.class);
newAnnouncements.add(ancmnt);
// announcements.add(ancmnt);
keys.add(ds.getKey());
loadProduct();
}
announcements.clear();
announcements.addAll(newAnnouncements);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
mDataBase.addValueEventListener(valueEventListener);
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.AnnouncementViewHolder> {
private ArrayList<Announcment> adapterAnnouncements;
private Context context;
public RecyclerViewAdapter(ArrayList<Announcment> adapterAnnouncements, Context context) {
this.adapterAnnouncements = adapterAnnouncements;
this.context = context;
}
@NonNull
@Override
public RecyclerViewAdapter.AnnouncementViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.announc_item, parent, false);
AnnouncementViewHolder avh = new AnnouncementViewHolder(v);
return avh;
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.AnnouncementViewHolder holder, int position) {
holder.nickName.setText(adapterAnnouncements.get(position).getNickName());
holder.announcement_time.setText(adapterAnnouncements.get(position).getAnnouncement_time());
holder.announcement_title.setText(adapterAnnouncements.get(position).getAnnouncement_title());
holder.announcement_votes.setText(Integer.toString(adapterAnnouncements.get(position).getAnnouncement_votes()));
holder.announcement_comments_count.setText(Integer.toString(adapterAnnouncements.get(position).getAnnouncement_comments_count()));
Picasso.get().load(adapterAnnouncements.get(position).getProfile_avatar()).into(holder.profile_avatar);
Picasso.get().load(adapterAnnouncements.get(position).getMain_announcement()).into(holder.main_announcement);
if (adapterAnnouncements.get(position).isVoted == true) {
holder.participate_button.setImageResource(R.drawable.man_icon_blue);
holder.announcement_votes.setTextColor(Color.parseColor("#0096D7"));
}
}
@Override
public int getItemCount() {
return adapterAnnouncements.size();
}
class AnnouncementViewHolder extends RecyclerView.ViewHolder {
TextView nickName, announcement_time, announcement_title, announcement_votes, announcement_comments_count;
ImageView profile_avatar, main_announcement, participate_button;
public AnnouncementViewHolder(@NonNull View itemView) {
super(itemView);
nickName = itemView.findViewById(R.id.nickName);
announcement_time = itemView.findViewById(R.id.announcement_time);
announcement_title = itemView.findViewById(R.id.announcement_title);
announcement_votes = itemView.findViewById(R.id.announcement_votes);
announcement_comments_count = itemView.findViewById(R.id.announcement_comments_count);
profile_avatar = itemView.findViewById(R.id.profile_avatar);
main_announcement = itemView.findViewById(R.id.main_announcement);
participate_button = itemView.findViewById(R.id.imageView6);
participate_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int positionIndex = getAdapterPosition();
mDataBase = FirebaseDatabase.getInstance().getReference("Announcements").child(keys.get(positionIndex));
Announcment announcement = adapterAnnouncements.get(positionIndex);
if (announcement.isVoted == false) {
mDataBase.child("announcement_votes").setValue(announcement.announcement_votes + 1);
mDataBase.child("isVoted").setValue(true);
announcement.isVoted = true;
participate_button.setImageResource(R.drawable.man_icon_blue);
announcement_votes.setTextColor(Color.parseColor("#0096D7"));
} else if (announcement.isVoted == true) {
mDataBase.child("announcement_votes").setValue(announcement.announcement_votes - 1);
mDataBase.child("isVoted").setValue(false);
announcement.isVoted = false;
participate_button.setImageResource(R.drawable.man_icon);
}
}
});
}
}
}
}
Код для макета:
<androidx.constraintlayout.widget.ConstraintLayout 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="310dp">
<ImageView
android:id="@+id/profile_avatar"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/nickName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="nickName"
android:textColor="@color/black"
app:layout_constraintStart_toEndOf="@+id/profile_avatar"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/announcement_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="13dp"
android:text="5 ч"
app:layout_constraintBottom_toTopOf="@+id/main_announcement"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/main_announcement"
android:layout_width="match_parent"
android:layout_height="175dp"
android:layout_marginTop="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/profile_avatar"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/announcement_title"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_marginStart="174dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="175dp"
android:text="Title text"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_announcement" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginStart="16dp"
android:layout_marginTop="55dp"
android:layout_marginBottom="7dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_announcement"
app:srcCompat="@drawable/man_icon" />
<TextView
android:id="@+id/announcement_votes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="9dp"
android:layout_marginBottom="9dp"
android:text="0"
android:textColor="@color/black"
android:textSize="13dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView6" />
<ImageView
android:id="@+id/announcement_comments"
android:layout_width="22dp"
android:layout_height="21dp"
android:layout_marginStart="26dp"
android:layout_marginTop="50dp"
android:layout_marginBottom="7dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/announcement_votes"
app:layout_constraintTop_toBottomOf="@+id/main_announcement"
app:srcCompat="@drawable/comment_button" />
<TextView
android:id="@+id/announcement_comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginBottom="9dp"
android:text="0"
android:textColor="@color/black"
android:textSize="13dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/announcement_comments" />
<ImageView
android:id="@+id/announcement_menu_button"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="9dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/main_announcement"
app:srcCompat="@drawable/three_dots" />
</androidx.constraintlayout.widget.ConstraintLayout>```