A RecyclerViewAdapter library that can make you feel happy
[Language] English | 中文文档
YasuoRecyclerViewAdapter ! Let you happy realization list in Android ! 掘金 简书
Here's a brief introduction to this library:YasuoRecyclerViewAdapter ,Why Yasuo, because Yasuo = = happy! This library is for everyone to feel happy when writing code!
①、Normal layout and multi layout of list, grid and stageredgrid
②、EmptyLayout/Header/Footer
③、LoadMore
**④、 Folding layout (support multi-level fold) **
⑤、Drag, slide delete
⑥、Two ItemDecoration are attached, which can be selected according to different needs
⑦、The ObservableList is used as the data source without manual notify
⑧、Support findViewById, ViewBinding, DataBinding three modes, according to your existing project mode or preferences to change!
⑨、Highly configurable animation (after comprehensive consideration, the itemanimator scheme of recyclerview is adopted. If necessary, please rely on the ItemAnimators Library of mikepenz)
⑩、Stick header (using sticky-layoutmanager Because there are some bugs in the position acquisition of the original library, we integrate them into this project and fix the bugs.)
2、For the latest version, please see jitpack
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.q876625596:YasuoRecyclerViewAdapter:x.y.z'
}
If you want to use Ctrl + CV code directly, please move sample directly
You must use YasuoList or its subclass as the data source. Yasuolist inherits from observablearraylist, adds some common methods, and listens inside the adapter. Therefore, you can use this type of data source without manually notifying
fun findViewByIdMode(){
//data source
val list = YasuoList<Any>()
val headerList = YasuoList<Any>()
val footerList = YasuoList<Any>()
binding.myRV.layoutManager = GridLayoutManager(this, 3)
//findViewById mode
binding.myRV.adapterBinding(this,list){
//do something
//Binding text layout
//Just configure holderConfig for the corresponding layout to realize multi layout, header and footer
holderConfig(R.layout.item_layout_text, TextBean::class) {
onHolderBind { holder, item ->
holder.getView<TextView>(R.id.itemText).apply {
text = item.text.value
}
}
}
}
//ViewBinding mode
binding.myRV.adapterViewBinding(this,list){
//do something
//Binding text layout
//Just configure holderConfig for the corresponding layout to realize multi layout, header and footer
holderConfig(R.layout.item_layout_text, TextBean::class, { ItemLayoutTextBinding.bind(it) }) {
onHolderBind { holder, item ->
itemText.text = item.text.value
}
}
}
//DataBinding mode
binding.myRV.adapterDataBinding(this,list){
//do something
//Binding text layout
//Just configure holderConfig for the corresponding layout to realize multi layout, header and footer
holderConfig(R.layout.item_layout_text_data_binding, TextBean::class, ItemLayoutTextDataBindingBinding::class) {
onHolderBind { holder ->
//The databinding schema has already bound data in XML, so there is no need to set it manually
}
}
}
}
There is only one difference between the above three modes, and it is quite convenient to switch between them.
Additional ViewPager2Adapter adapterDataBinding can be replaced by adapterViewBinding, adapterBinding When creating a layout, you should pay attention to: br.vpItem is the default value in adapterDataBinding
binding.viewPager.adapterDataBinding(this, list) {
holderConfigVP(R.layout.empty_layout_one_data_binding, EmptyBeanOne::class, EmptyLayoutOneDataBindingBinding::class) {
}
holderConfigVP(R.layout.empty_layout_two_data_binding, EmptyBeanTwo::class, EmptyLayoutTwoDataBindingBinding::class) {
}
}
The use of empty layout is also very simple. First configure the holderConfig of empty layout, and then call adapter.showEmptyLayout That's it.
binding.myRV.adapterViewBinding(this,list){
//do something
holderConfig(R.layout.item_layout_text, TextBean::class, { ItemLayoutTextBinding.bind(it) }) {
onHolderBind { holder, item ->
itemText.text = item.text.value
itemText.setOnClickListener {
showEmptyLayout(/*Empty layout entity*/EmptyBeanTwo(), /*Clear header*/true, /*Clear footer*/true)
}
}
}
}
There are two ways to set the proportion. The first is to set the proportion for one type of layout:
binding.myRV.adapterViewBinding(this,list){
//do something
holderConfig(R.layout.item_layout_text, TextBean::class, { ItemLayoutTextBinding.bind(it) }) {
//do something
//Set the layout of an itemViewType uniformly
//The staggeredGridLayouytManager filled the line
staggeredGridFullSpan = true
//gridLayoutManager Span
gridSpan = 3
}
}
Second, set the proportion of an item separately:
list.add(ImageBean(MutableLiveData(ContextCompat.getDrawable(this@MainActivity, R.drawable.eee))).apply {
//To set an item individually
//The staggeredGridLayouytManager filled the line
staggeredGridFullSpan = true
//gridLayoutManager Span
gridSpan = 3
}
Judging priority: single item setting > type setting
Loading more layouts is similar to loading empty layouts. After loading the holderConfig configuration of more layouts, call adapter.showLoadMoreLayout Make the empty layout display and add it at last adapter.onLoadMoreListener Just monitor.
binding.myRV.adapterViewBinding(this,list){
//Show load more
showLoadMoreLayout(DefaultLoadMoreItem())
//Set to load more listeners
onLoadMoreListener(binding.myRV) {
//Request data...
}
//do something
}
Just use adapter.enableDragOrSwipeYou can enable drag and drop, set monitor, set gesture direction, and disable certain layouts
binding.myRV.adapterViewBinding(this,list){
//Drag / swipe delete
enableDragOrSwipe(binding.myRV, isLongPressDragEnable = true, isItemViewSwipeEnable = true)
//do something
}
First, set the layout manager: stickylinear layout manager, stickygridlayout manager, stickystaged GridLayout manager There are two ways of Sticky header. The first is to set Sticky header for a certain type of layout
binding.myRV.adapterViewBinding(this,list){
//do something
holderConfig(R.layout.item_layout_text, TextBean::class, { ItemLayoutTextBinding.bind(it) }) {
//Set the layout of an itemViewType uniformly
//Sticky header. Note that sticky header will fill one line by default
sticky = true
//do something
}
}
Second, set the Sticky header for an item
list.add(ImageBean(MutableLiveData(ContextCompat.getDrawable(this@MainActivity, R.drawable.eee))).apply {
//To set an item individually
//Sticky header. Note that sticky header will fill one line by default
sticky = true
}
Judging priority: single item setting > type setting
Folding layout requires data class to inherit YasuoFoldItem, and then only need to use adapter.expandOrFoldItem It supports multi-level folding. If you need to delete or add an item in the folding layout, it is recommended to use adapter.removeAndFoldListItem And adapter.addAndFoldListItem Methods
The animation uses mikepenz's ItemAnimators library. If necessary, please rely on this library.
binding.myRV.itemAnimator = SlideLeftAlphaAnimator()
It supports setting styles for each edge individually
binding.myRV.addYasuoDecoration {
setDecoration(R.layout.item_layout_text, this@MainActivity, defaultRes)
setDecoration(R.layout.item_layout_image, this@MainActivity, defaultRes)
}
An additional span equal grid layout space is attached to separate itemDecoration
binding.myRV.addItemDecoration(GridSpacingItemDecoration(3, 20, true))
Property name / method name | introduce | Default value |
---|---|---|
itemList | main list | YasuoList() |
headerList | header list | YasuoList() |
footerList | footer list | YasuoList() |
showLoadMoreLayout() | Configure and show loading more layouts | ------ |
removeLoadMore() | Remove load more layouts | ------ |
enableLoadMoreListener() | Enable list scrolling to the bottom to load more listeners | ------ |
disableLoadMoreListener() | Disable list scrolling to the bottom when loading more listeners | ------ |
isShowEmptyLayout() | Determine whether the current display is empty layout state | ------ |
showEmptyLayout() | Configure and display empty layouts | ------ |
expandOrFoldItem() | Expand / fold an item | ------ |
removeAndFoldListItem() | Remove an item and remove the same item in its collapsed list at the same time | ------ |
getAllListSize() | Gets the length of all lists | ------ |
getItemListTrueSize() | Get the actual length of [itemList] | ------ |
getHeaderListTrueSize() | Get the actual length of [headerList] | ------ |
getFooterListTrueSize() | Get the actual length of [footerList] | ------ |
getHeaderTruePosition() | Get the real position of [headerList] | ------ |
getItemTruePosition() | Get the real position of [itemList] | ------ |
getFooterTruePosition() | Get the real position of [footerList] | ------ |
inHeaderList() | Judge that position is in [headerList] | ------ |
inItemList() | Judge that position is in [itemList] | ------ |
inFooterList() | Judge that position is in [footerList] | ------ |
setAfterDataChangeListener() | After the list data changes, the monitor will be triggered after notify | ------ |
enableDragOrSwipe() | Set whether the item can be dragged or deleted by sliding | ------ |
onLoadMoreListener() | Set to load more listeners | ------ |
holderConfig() | Configure holder to establish the matching relationship between data class and layout file | ------ |
Property name / method name | introduce | Default value |
---|---|---|
sticky | Is this type of layout a sticky header | false |
isFold | Whether this type of layout supports expand/fold | false |
gridSpan | The proportion of this type of layout in Grid | 0 |
staggeredGridFullSpan | Is this type of layout full in the staggeedgrid | false |
holderCreateListener | Listen when the type holder is created | null |
holderBindListener | The type of holder is used to listen when binding | null |
createBindingFun | The creation method of viewbinding is only viewbinding mode | null |
variableId | The corresponding data ID in XML is only in the data binding mode | BR.item |
Property name / method name | introduce | Default value |
---|---|---|
sticky | Is this type of layout a sticky header | false |
gridSpan | The proportion of this type of layout in Grid | 0 |
staggeredGridFullSpan | Is this type of layout full in the staggeedgrid | false |
Property name / method name | introduce | Default value |
---|---|---|
list | Next level list | YasuoList() |
isExpand | Expanded | false |
parentHash | Parent hash, which will be assigned after expansion | false |
sticky | Is this type of layout a sticky header | false |
gridSpan | The proportion of this type of layout in Grid | 0 |
staggeredGridFullSpan | Is this type of layout full in the staggeedgrid | false |
First of all, thank you for your reading. The new year is coming soon. I also wish you a happy new year. I hope this library can help you. If you like YasuoRecyclerViewAdapter this library, I hope to give you a star on GitHub as a driving force for my progress! If he has deficiencies or needs new functions, he can ask me issue or add my QQ: 876625596