Skip to main content
Improved readability, made things clearer
Source Link
David Medenjak
  • 34.7k
  • 16
  • 113
  • 137

Easy and reusable ItemDecoration

WithStatic headers can easily be added with an ItemDecoration you can easily and without any further changes. in your code add static views to the top of your list:

// add the decoration. done. HeaderDecoration headerDecoration = new HeaderDecoration(/* init */); recyclerView.addItemDecoration(headerDecoration); 

You can reuse theThe decoration is also reusable since there is no need to modify the adapter or the RecyclerView at all.

The downsides The sample code provided below will require a view to this solution are that, as ofadd to the current state, there is no possibility for user interaction - youtop which can not set a ClickListener. Also, since this is an ItemDecoration, you have to have at least one element in your list or it will not showjust be inflated like everything else. You could reuse the layout for an empty state or provide an empty dummy item when your dataset is empty to fixIt can look like this issue.:

Attached is the source code to the above demonstrated decoration. By measuring and drawing the View you can pass most layouts into the decoration and it will render nicely on top of your list, only wrap_content and match_parent might not behave as expected.

Why static?

If you just have to display text and images this solution is for you—there is no possibility for user interaction like buttons or view pagers, since it will just be drawn to top of your list.

Empty list handling

If there is no view to decorate, the decoration will not be drawn. You will still have to handle an empty list yourself. (One possible workaround would be to add a dummy item to the adapter.)

The code

You can find the full source code here on GitHub including a Builder to help with the initialization of the decorator, or just use the code below and supply your own values to the constructor.

Please be sure to set a correct layout_height for your view. e.g. match_parent might not work properly.

Please note: The GitHub project is my personal playground. It is not thorougly tested, which is why there is no library yet.

What does it do?

An ItemDecoration is additional drawing to an item of a list. In this case, a decoration is drawn to the top of the first item.

The view gets measured and laid out, then it is drawn to the top of the first item. If a parallax effect is added it will also be clipped to the correct bounds.

With an ItemDecoration you can easily and without any further changes in your code add static views to the top of your list:

HeaderDecoration headerDecoration = new HeaderDecoration(/* init */); recyclerView.addItemDecoration(headerDecoration); 

You can reuse the decoration since there is no need to modify the adapter or the RecyclerView.

The downsides to this solution are that, as of the current state, there is no possibility for user interaction - you can not set a ClickListener. Also, since this is an ItemDecoration, you have to have at least one element in your list or it will not show. You could reuse the layout for an empty state or provide an empty dummy item when your dataset is empty to fix this issue.

Attached is the source code to the above demonstrated decoration. By measuring and drawing the View you can pass most layouts into the decoration and it will render nicely on top of your list, only wrap_content and match_parent might not behave as expected.

You can find the full source code here on GitHub including a Builder to help with the initialization of the decorator, or just use the code below and supply your own values.

Easy and reusable ItemDecoration

Static headers can easily be added with an ItemDecoration and without any further changes.

// add the decoration. done. HeaderDecoration headerDecoration = new HeaderDecoration(/* init */); recyclerView.addItemDecoration(headerDecoration); 

The decoration is also reusable since there is no need to modify the adapter or the RecyclerView at all.

The sample code provided below will require a view to add to the top which can just be inflated like everything else. It can look like this:

Why static?

If you just have to display text and images this solution is for you—there is no possibility for user interaction like buttons or view pagers, since it will just be drawn to top of your list.

Empty list handling

If there is no view to decorate, the decoration will not be drawn. You will still have to handle an empty list yourself. (One possible workaround would be to add a dummy item to the adapter.)

The code

You can find the full source code here on GitHub including a Builder to help with the initialization of the decorator, or just use the code below and supply your own values to the constructor.

Please be sure to set a correct layout_height for your view. e.g. match_parent might not work properly.

Please note: The GitHub project is my personal playground. It is not thorougly tested, which is why there is no library yet.

What does it do?

An ItemDecoration is additional drawing to an item of a list. In this case, a decoration is drawn to the top of the first item.

The view gets measured and laid out, then it is drawn to the top of the first item. If a parallax effect is added it will also be clipped to the correct bounds.

added 240 characters in body
Source Link
David Medenjak
  • 34.7k
  • 16
  • 113
  • 137

You can reuse the decoration since there is no need to modify the adapter or the RecyclerView. The only downside to this solution is that, as of the current state, there is no possibility for user interaction - you can't set a ClickListener.

The downsides to this solution are that, as of the current state, there is no possibility for user interaction - you can not set a ClickListener. Also, since this is an ItemDecoration, you have to have at least one element in your list or it will not show. You could reuse the layout for an empty state or provide an empty dummy item when your dataset is empty to fix this issue.

You can reuse the decoration since there is no need to modify the adapter or the RecyclerView. The only downside to this solution is that, as of the current state, there is no possibility for user interaction - you can't set a ClickListener.

You can reuse the decoration since there is no need to modify the adapter or the RecyclerView.

The downsides to this solution are that, as of the current state, there is no possibility for user interaction - you can not set a ClickListener. Also, since this is an ItemDecoration, you have to have at least one element in your list or it will not show. You could reuse the layout for an empty state or provide an empty dummy item when your dataset is empty to fix this issue.

added 212 characters in body
Source Link
David Medenjak
  • 34.7k
  • 16
  • 113
  • 137

With an ItemDecoration you can easily and without any further changes in your code add static views to the top of your list.:

HeaderDecoration headerDecoration = new HeaderDecoration(/* init */); recyclerView.addItemDecoration(headerDecoration); 

You can reuse the decoration since there is no need to modify the adapter or the RecyclerView. The only downside to this methodsolution is, that, as of the current state, there is no possibility to interact for the user interaction - you can't set a ClickListener.

Attached is the source code forto the above animationdemonstrated decoration. By measuring and drawing the View you can pass most layouts into the decoration and it will render nicely on top of your list, althoughonly wrap_content and match_parent might not behave as expected.

You can find the full source code here on GitHub including a Builder to help with the initialization of the decorator, or just use the code below and supply your own values.

With an ItemDecoration you can easily and without any further changes in your code add static views to the top of your list.

recyclerView.addItemDecoration(headerDecoration); 

You can reuse the decoration since there is no need to modify the adapter or the RecyclerView. The only downside to this method is, that as of the current state, there is no possibility to interact for the user.

Attached is the source code for the above animation. By measuring and drawing the View you can pass most layouts into the decoration and it will render nicely, although wrap_content and match_parent might not behave as expected.

You can find the full source code here on GitHub including a Builder to help with initialization.

With an ItemDecoration you can easily and without any further changes in your code add static views to the top of your list:

HeaderDecoration headerDecoration = new HeaderDecoration(/* init */); recyclerView.addItemDecoration(headerDecoration); 

You can reuse the decoration since there is no need to modify the adapter or the RecyclerView. The only downside to this solution is that, as of the current state, there is no possibility for user interaction - you can't set a ClickListener.

Attached is the source code to the above demonstrated decoration. By measuring and drawing the View you can pass most layouts into the decoration and it will render nicely on top of your list, only wrap_content and match_parent might not behave as expected.

You can find the full source code here on GitHub including a Builder to help with the initialization of the decorator, or just use the code below and supply your own values.

Improved code sampale, attached gif
Source Link
David Medenjak
  • 34.7k
  • 16
  • 113
  • 137
Loading
Source Link
David Medenjak
  • 34.7k
  • 16
  • 113
  • 137
Loading