I have imported code from here in my project for getting sectioned adapter with headers view in my Recycler View. Now i'm not sure how can i get position in method onBindItemViewHolder()
Here is my adapter:
public class NotificationHeaderListAdapter extends SectionedAdapter<Notification> {
// Allows to remember the last item shown on screen
private int lastPosition = -1;
private Context mContext;
public NotificationHeaderListAdapter(Context context, List<Notification> notifications) {
this.mContext = context;
this.setItemList(notifications);
this.setCustomHeaderLayout(R.layout.recycler_header_notification);
}
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, Notification item, @ViewType int viewType) {
NotificationHolder notificationHolder = (NotificationHolder) holder;
notificationHolder.tvNotification.setText(
item.getNotification() + " ti je poslao zahtev za clanstvo u ekipu "
+ item.getGroupName());
long timestamp = DateUtil.getDifference(item.getTimestamp());
// Converting timestamp into x ago format
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(String.valueOf(timestamp)),
System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
notificationHolder.tvTimestamp.setText(timeAgo);
// Here i need to call my background view for setting animation to list, but i need position for that method
// setAnimation(notificationHolder.mRelativeLayout, position) ???
}
@Override
public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, @ViewType int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.notification_list_item, parent, false);
return new NotificationHolder(view);
}
public static class NotificationHolder extends RecyclerView.ViewHolder {
MyTextView tvTimestamp;
MySecondTextView tvNotification;
RelativeLayout mRelativeLayout;
public NotificationHolder(View view) {
super(view);
tvTimestamp = (MyTextView) view.findViewById(R.id.tv_timestamp);
tvNotification = (MySecondTextView) view.findViewById(R.id.tv_notification);
mRelativeLayout = (RelativeLayout) view.findViewById(R.id.relativeLayout);
}
}
/**
* Here is the key method to apply the animation
*/
private void setAnimation(View viewToAnimate, int position) {
// If the bound view wasn't previously displayed on screen, it's animated
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
}
I need to get position of row in list, so i can call method setAnimation()
which need two arguments. One for passing view on which i'm setting animation and second one is for position of the row, but i don't have position.
By using holder.getAdapterPosition
you can get the row position
@Override
public void onBindItemViewHolder(RecyclerView.ViewHolder holder, Notification item, @ViewType int viewType) {
...
int position = holder.getAdapterPosition();
...
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments