Custom RecyclerView writed with Kotlin
// Project
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
// Module
dependencies {
compile 'com.github.creasexul:ListLikeRecycler:1.0'
}
Just init it like a simple RecyclerView:
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
recyclerView.layoutManager = layoutManager
recyclerView.adapter = adapter
These features are disabled by default. You can enable these features in XML or in your code:
<com.crease.listlikerecyclerview.view.ListLikeRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="LinearLayoutManager"
app:loadable="true"
app:refreshable="true"/>
recyclerView.refreshEnabled = true
recyclerView.loadingEnabled = true
recyclerView.isRefreshing = true
recyclerView.loadCallback = object : ListLikeRecyclerView
.OnRecyclerViewLoadCallback {
override fun onHeadRefresh() {
mainBinding.root.postDelayed({
Toast.makeText(this@MainActivity, "Head refreshing finished", Toast
.LENGTH_LONG)
.show()
mainBinding.recyclerView.isRefreshing = false
}, 1000)
}
override fun onFootLoad() {
mainBinding.root.postDelayed({
Toast.makeText(this@MainActivity, "Foot loading finished", Toast.LENGTH_LONG).show()
// no more
mainBinding.recyclerView.noMore = true
}, 1000)
}
}
recyclerView.setOnItemClickListener(object : ListLikeRecyclerView
.OnItemClickListener {
override fun onItemClick(childView: View, itemPosition: Int) {
Toast.makeText(this@MainActivity, itemPosition.toString(), Toast.LENGTH_LONG).show()
}
override fun onItemLongClick(childView: View, position: Int) {
}
})
If you need customized header/footer, you can inherit HeadRefreshView
or FootLoadView
and implement the corresponding method
recyclerView.addHeaderView(id, view)
recyclerView.addFooterView(id, view)
recyclerView.removeHeaderView(id)
recyclerView.removeFooterView(id)
You can handle the drag or sliding events by using ItemTouchCallback
val callbackNew = object : ItemTouchCallback() {
override fun onItemSwiped(viewHolder: RecyclerView.ViewHolder, position: Int, direction: Int) {
stringList.removeAt(position)
adapter.removeItem(position)
}
override fun onItemCanMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
position: Int, targetViewHolder: RecyclerView.ViewHolder,
targetPosition: Int) = true
override fun onItemMoved(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, position: Int, targetViewHolder: RecyclerView.ViewHolder, targetPosition: Int, x: Int, y: Int) {
if (position < targetPosition) {
for (i in position until targetPosition) {
Collections.swap(stringList, i, i + 1)
}
} else {
for (i in position downTo (targetPosition + 1)) {
Collections.swap(stringList, i, i - 1)
}
}
adapter.moveItem(position, targetPosition)
}
}
val touchHelper = ListItemTouchHelper(callbackNew)
touchHelper.attachToRecyclerView(mainBinding.recyclerView)
Please note that if you set long press events and drag events at the same time, you should set the long press events after drag events
You can find more in MainActivity.kt
Copyright 2017 Crease
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.