在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):StephenVinouze/AdvancedRecyclerView开源软件地址(OpenSource Url):https://github.com/StephenVinouze/AdvancedRecyclerView开源编程语言(OpenSource Language):Kotlin 51.9%开源软件介绍(OpenSource Introduction):AdvancedRecyclerViewBefore the appearance of This library comes with two focuses:
Migrate to v2If you are already using the v1 of this library and considering migrating to the v2, here are a few things worth mentioning
Basic usageThe core module contains the basic logic to easily manipulate a Create your model : data class Sample(val id: Int, val rate: Int, val name: String) Create your view : class SampleItemView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {
init {
LayoutInflater.from(context).inflate(R.layout.view_sample_item, this, true)
}
fun bind(sample: Sample) {
// Update your subviews with the Sample data
}
} Then define your own adapter that extends from class SampleAdapter : RecyclerAdapter<Sample>() {
override fun onCreateItemView(parent: ViewGroup, viewType: Int): View = SampleItemView(parent.context)
override fun onBindItemView(view: View, position: Int) {
when (view) {
is SampleItemView -> view.bind(items[position])
}
}
} Finally in your val adapter = SampleAdapter(this)
adapter.items = mutableListOf()
recyclerView.adapter = adapter Your Click eventsListening to click events are often required and usually requires to transit such an event from your adapter to your adapter.onClick = { view, position ->
val sample = items[position]
// Do whatever you want
}
adapter.onLongClick = { view, position ->
val sample = items[position]
// Do whatever you want
} Choice modeA useful feature that can be found in either Choice mode can be either NONE (default), SINGLE or MULTIPLE. If a choice mode other than NONE is declared for your adapter, your selected items will be internally stored at each view click. You can also manually select an item : adapter.toggleItemView(position) You can obviously retrieve which items are selected : adapter.selectedItemViewCount // Returns the selected item view count
adapter.getSelectedItemViews() // Returns a list of selected item views
adapter.isItemViewToggled(position) // Returns true if item is selected If you need to remove all selected items : adapter.clearSelectedItemViews() Advanced usageIn most cases the core module should handle most of the heavy work.
This section presents more advanced concepts that can help you while using SectionYou may encounter lists that need to be regrouped within sections to present a clear sorting or your items.
This can be easily done natively by overriding Create a section item view to render your sections. Let's call it Extends your adapter class from class SampleSectionAdapter : RecyclerSectionAdapter<Int, Sample>({ it.rate }) {
override fun onCreateItemView(parent: ViewGroup, viewType: Int): View = SampleItemView(parent.context)
override fun onBindItemView(view: View, position: Int) {
when (view) {
is SampleItemView -> view.bind(items[position])
}
}
// Override these two new methods to render your sections
override fun onCreateSectionItemView(parent: ViewGroup, viewType: Int): View =
SampleSectionItemView(parent.context)
override fun onBindSectionItemView(sectionView: View, sectionPosition: Int) {
sectionAt(sectionPosition)?.let {
when (sectionView) {
is SampleSectionItemView -> sectionView.bind(it)
}
}
}
} Note the Int generic type in the class declaration that indicates the type that will contains the section. In our case, we want to sort them by rate.
The building of the sections will be automatically taken care of by a lambda that you must provide in your constructor () Wraps things up the way you were doing with your val sectionAdapter = SampleSectionAdapter(this)
sectionAdapter.items = mutableListOf()
recyclerView.adapter = sectionAdapter Setting items to your adapter will take care of the section building and relayout your list. You may want to sort your items before setting them in your adapter to obtain consistent sections. PaginationPagination is a common pattern especially when interacting with an API to lazy load your items into your list.
The pagination module provides an extension to the Extend your adapter from class SamplePaginationAdapter : RecyclerPaginationAdapter<Sample>() {
override fun onCreateItemView(parent: ViewGroup, viewType: Int): View = SampleItemView(parent.context)
override fun onBindItemView(view: View, position: Int) {
when (view) {
is SampleItemView -> view.bind(items[position], isItemViewToggled(position))
}
}
override fun onCreateLoaderView(parent: ViewGroup, viewType: Int): View =
LayoutInflater.from(parent.context).inflate(R.layout.view_progress, parent, false)
} Activate pagination on your val paginationAdapter = SamplePaginationAdapter(this)
recyclerView.adapter = paginationAdapter
recyclerView.enablePagination(
isLoading = {
paginationAdapter.isLoading
},
hasAllItems = {
// return true if you have loaded all your items to stop the pagination to try loading more
},
onLoad = {
// Fetch and/or load your items within your list
paginationAdapter.isLoading = true
// Fetch items from API. Indicate loading done once fetch is finished
paginationAdapter.isLoading = false
// Append items into your list
paginationAdapter.appendItems(items)
}
)
GestureThe You can enable gestures by using the recyclerView.enableGestures(
dragDirections = ItemTouchHelper.UP or ItemTouchHelper.DOWN,
swipeDirections = ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT,
onMove = { fromPosition, toPosition ->
// Do whatever you want
true
},
onSwipe = { position, direction ->
// Do whatever you want
}
) Note that all lambdas are optionals so that you may enable/configure your desired gestures. Also, the gesture module includes the section module so that all your gestures also works while using your list with sections. The only limitation is that you cannot move an item from one section to another. Gradle DependencyThe gradle dependency is available via JitPack. Add this in your root allprojects {
repositories {
maven { url "https://jitpack.io" }
}
} Then add the dependencies that you need in your project. def advancedrecyclerview_version = "{latest_version}"
dependencies {
implementation "com.github.StephenVinouze.AdvancedRecyclerView:core:${advancedrecyclerview_version}"
// If you need to display lists with sections
implementation "com.github.StephenVinouze.AdvancedRecyclerView:section:${advancedrecyclerview_version}"
// If you need to paginate your lists
implementation "com.github.StephenVinouze.AdvancedRecyclerView:pagination:${advancedrecyclerview_version}"
// If you need to handle gestures within your lists
implementation "com.github.StephenVinouze.AdvancedRecyclerView:gesture:${advancedrecyclerview_version}"
} Pull requestsI welcome and encourage all pull requests. I might not be able to respond as fast as I would want to but I endeavor to be as responsive as possible. All PR must:
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论