How to get position in Sectioned Adapter (Android)?

Dusan Dimitrijevic

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.

Shashanth

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the position of the last visible item inside an Adapter in Android

From Dev

How to get position of added in adapter object

From Dev

How to get position of added in adapter object

From Dev

how to get the position of an array adapter in another activity

From Dev

Android - How to manage position of viewpager in adapter?

From Dev

How to get the Edit text position from Recycler View adapter using Text Watcher in android

From Dev

How to get view from Adapter based on position and set selected as true in android?

From Dev

How to get sectioned data from a CoreData entity relationship in Swift

From Dev

how to get the row item position number in listview adapter class

From Dev

How to get tapped List Item position in Adapter class on button click

From Dev

How to get the item position on a clickListener set in an Adapter class?

From Dev

How to get item from RecyclerView adapter in Android

From Dev

Custom Cursor Adapter ( get position )

From Dev

Android RecyclerView Adapter Position is -1

From Dev

Get Position of ImageView onClick listener in an Adapter Class

From Dev

Get data by position in custom adapter by onClickListener

From Dev

Separated list adapter - get the position of the item

From Dev

Get Position of ImageView onClick listener in an Adapter Class

From Dev

How to get the position using fancycoverflow in android

From Dev

How to get touch position in percentage in android

From Dev

How to get the position using fancycoverflow in android

From Dev

How to get touch position in percentage in android

From Dev

How to get active tab position in android material

From Dev

How to get arraylist from recyclerview adapter in android kotlin

From Dev

How to get the visible list items count in a Xamarin android Array adapter

From Dev

Android GridView Adapter uses wrong position

From Dev

How to customize an adapter in Android?

From Dev

How to get other views in same ViewHolder at same position as the onClicked view (use in a RecyclerView Adapter)

From Dev

How to implant a sectioned tableview with Parse in Swift

Related Related

  1. 1

    How to get the position of the last visible item inside an Adapter in Android

  2. 2

    How to get position of added in adapter object

  3. 3

    How to get position of added in adapter object

  4. 4

    how to get the position of an array adapter in another activity

  5. 5

    Android - How to manage position of viewpager in adapter?

  6. 6

    How to get the Edit text position from Recycler View adapter using Text Watcher in android

  7. 7

    How to get view from Adapter based on position and set selected as true in android?

  8. 8

    How to get sectioned data from a CoreData entity relationship in Swift

  9. 9

    how to get the row item position number in listview adapter class

  10. 10

    How to get tapped List Item position in Adapter class on button click

  11. 11

    How to get the item position on a clickListener set in an Adapter class?

  12. 12

    How to get item from RecyclerView adapter in Android

  13. 13

    Custom Cursor Adapter ( get position )

  14. 14

    Android RecyclerView Adapter Position is -1

  15. 15

    Get Position of ImageView onClick listener in an Adapter Class

  16. 16

    Get data by position in custom adapter by onClickListener

  17. 17

    Separated list adapter - get the position of the item

  18. 18

    Get Position of ImageView onClick listener in an Adapter Class

  19. 19

    How to get the position using fancycoverflow in android

  20. 20

    How to get touch position in percentage in android

  21. 21

    How to get the position using fancycoverflow in android

  22. 22

    How to get touch position in percentage in android

  23. 23

    How to get active tab position in android material

  24. 24

    How to get arraylist from recyclerview adapter in android kotlin

  25. 25

    How to get the visible list items count in a Xamarin android Array adapter

  26. 26

    Android GridView Adapter uses wrong position

  27. 27

    How to customize an adapter in Android?

  28. 28

    How to get other views in same ViewHolder at same position as the onClicked view (use in a RecyclerView Adapter)

  29. 29

    How to implant a sectioned tableview with Parse in Swift

HotTag

Archive