Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Get position of item in section #463

Closed
IonutNegru87 opened this issue Oct 6, 2017 · 7 comments
Closed

[Question] Get position of item in section #463

IonutNegru87 opened this issue Oct 6, 2017 · 7 comments

Comments

@IonutNegru87
Copy link

Hello,

First of all I want to say this is a great library which offers many nice features and additions in comparison to the default RV.

My use case is a little specific, but I want to check if there is an feature like this already implemented or if I have to manually do it.
I need to get the position of an item, relative to it's section. For example:
Header0
Item 1
Item 2
Header1
Item 3

The title of my items depends on their position in the section. Currently I do not have an way to get the position of an item relative to it's section, thus it displays:
Header0
Item1
Item2
Header3
Item4

Is there an functionality like this already implemented? Or do you recommend any way to do it?
I need to do this directly in the ItemHolder I use, when binding the view holder.

Thank you.

@davideas
Copy link
Owner

davideas commented Oct 6, 2017

Hi @IonutNegru87, thanks for the question and compliments.

The adapter has getSubPositionOf but is for expandables, it can possibly be modified also for headers. However, if a sectionable has header, you take the global position of both and calculate the difference.

@IonutNegru87
Copy link
Author

Hello @davideas,

I've tried getSubPositionOf, but as you mentioned it is only for expandable items (which I do not currently support in my component) and in my case it was returning -1.

What I did for now is to store an int value which represents the order in the subsection, but this is not a very useful option as when i update my list of data, I need to update all the items.

Your recommendation with the difference between the position of the header and the item is working great.
Although, I do not use this in my custom adapter (as I don't want to bind the item & header holders directly to my adapter), I've made an utility method which does this:

  /**
   * Utility method for computing the position of the current item in it's section.
   * <br/>
   * The position is an real position, starting with the value {@code 1}.
   *
   * @param adapter
   *     The adapter required to calculate the global position of the item.
   *
   * @return The position of the item in it's section (based on the header) or {@code -1} if the adapter is not
   *      valid, or {@code 0} if there is no header set for the item.
   */
  private int getPosInSection(FlexibleAdapter adapter) {
    if (null == adapter) {
      return -1;
    }
    if (null == this.getHeader()) {
      return 1;
    }

    return adapter.getGlobalPositionOf(this.getHeader()) - adapter.getGlobalPositionOf(this);
  }

This is implemented in the my ItemHolder which is an AbstractSectionableItem.

If you have any other ideas on how to optimize this, it would be great. Maybe at some point this functionality can be embedded directly in the adapter(even though it has an very specific use-case).

Thank you again for your great work and for the help on this issue.

@davideas davideas added this to the 5.0.0-rc3 milestone Oct 9, 2017
@davideas
Copy link
Owner

davideas commented Oct 9, 2017

Yes, it can be embedded, I keep this open for the modification.

@IonutNegru87
Copy link
Author

Thank you for your support and for your great job :)
Waiting for the new release.

@IonutNegru87
Copy link
Author

Hello,
I've used the latest version which includes this changed getSubPositionOf(@NonNull T child).
It returns the correct position, but I'm getting a lint warning due to unchecked call.

I'm using this directly in AbstractSectionableItem#bindViewHolder(FlexibleAdapter adapter, ItemViewHolder holder, int position, List payloads).
The type of the adapter is not set here, thus the warning.

It is not a major issue, but could create consistency problems in the future.
Do you recommend a specific approach for this?

Best regards and keep up the good work.

@davideas
Copy link
Owner

davideas commented Nov 22, 2017

Thank you @IonutNegru87, well in this case, I cannot provide the type (I don't have it), however the declared type is for sure the most common one and all in the end extends IFlexible or AbstractFlexibleItem.

You can suppress the warning.

@IonutNegru87
Copy link
Author

@davideas ,
I've checked it's type manually.
Thank you and best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants