Skip to content
forked from j2esu/rv-joiner

Android RecyclerView Joiner Library allows you to join several RecyclerView adapters and layouts into single RecyclerView adapter. Optionally, joined layouts can be used to add header, footer and devider to your RecyclerView.

License

Notifications You must be signed in to change notification settings

opacapp/rv-joiner

 
 

Repository files navigation

Android RecyclerView Joiner Library

This library provides you a functionality of joining together several adapters and layouts into a single RecyclerView. The result of joining will be an adapter, which you can set to your RecyclerView. Actually, this library allows you to construct multitype adapter from separate parts (such as an adapters and layouts). This approach gives you next advantages:

  • More flixibility of using adapters (they also can be used separately or in different constructions)
  • You can dynamically change your RecyclerView structure by adding and removing parts programmatically
  • Add header, footer, or devider layout without adding a new item type to your adapter

For example, you can combine two adapters and use title layout to separate them, like in full demo app:

Full demo app screenshot

Get started

Get started app source code

Dependencies

Configure dependencies in you module build.gradle file. Example


dependencies {
    //your other dependencies
    compile 'su.j2e:rv-joiner:1.0.9'//latest version by now
}

NOTE: You need to use jCenter repositiory to do this (example).

See releases for new versions.

Add RecyclerView to your xml

We need RecyclerView. Not surprising. Example

Create an adapter if you've not

You need just your RecyclerView.Adapter, nothing special (except of getting your view or view holder position, see Cautions section). Example

Create xml layout if needed

You can join your layout to recycler view. Just a usual xml layout, no magic. Example

Do the magic!

Usage example from get started source:


//init your RecyclerView as usual
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));

//construct a joiner
RvJoiner rvJoiner = new RvJoiner();
rvJoiner.add(new JoinableLayout(R.layout.header));
rvJoiner.add(new JoinableAdapter(new MyAdapter()));
rvJoiner.add(new JoinableLayout(R.layout.devider));
rvJoiner.add(new JoinableAdapter(new MyAdapter()));
rvJoiner.add(new JoinableLayout(R.layout.footer));

//set join adapter to your RecyclerView
rv.setAdapter(rvJoiner.getAdapter());

Thats it! Result looks like this:

Get started app

That's only the basic usage. Check full demo app and read Extra customization section to learn how to bring more customization into your app with joiner.

Cautions

Getting view or ViewHolder position (or type)

Be careful when using these methods of RecyclerView:

and these methods of RecyclerView.ViewHolder

returns JOINED position and JOINED type. I've named joined values the values, which were assigned to items after joining. Joined adapter has item positions from 0 to sum of joined adapter sizes and unique type id for every real type in joined adapters. But often we need real position of view or view holder in adapter (for ex. when reacting on click). To get real position (real type and other info) for joined position you can use RvJoiner.getPositionInfo(joinedPosition). Also it can be useful to hide this dependency in a wrapper class (to avoid code modifications if decide to use this adapter without joiner). I wrote RvJoiner.RealPositionProvider for you. You can see usage example in full demo app.

Good news: you can use getItemId() for ViewHolder and getChildItemId(View child) for RecyclerView, this methods works as expected (or not as expected, if you forget to support stable ids).

Supporting stable ids

If you want to setHasStableIds(true) in your adapter, do this in constructor of your adapter, or use JoinableAdapter constructor to achieve this. If you set it in other time, you will get an error, because RvJoiner adds data observers to your adapter.

Also don't forget to set has stable ids for joined adapter in RvJoiner constructor (it's the only way to do this). And the last thing: don't forget to support stable ids in all joined adapters (otherwise, unpredictable behaviour can occurs).

Extra customization

I'm working on this topic and more detailed description, but all examples already ready and works. Check Full Demo App source. I hope it helps.

License

Library is distributed under Apache 2.0 license, see LICENSE.txt

Enjoy!

About

Android RecyclerView Joiner Library allows you to join several RecyclerView adapters and layouts into single RecyclerView adapter. Optionally, joined layouts can be used to add header, footer and devider to your RecyclerView.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%