使用文档仅供参考,项目已过时,已迁移到common-control仓库!!
a custom view for tornadofx to accomplish list view of many tasks,is based on scroll pane
I copy the idea of RecyclerView which is in Android,so call it FxRecyclerView
I have used tornadofx some times.In the time of using tornadofx,I found that it can dymanically add a view,that is the reason why I give up the javafx.
Although tornadofx provides option that dymanically add a view,I think it is also fussy.
So I copy the idea and usage of RecyclerView(a view in android) to create this view.
I think the usage of this view is easily than native way in tornadofx.
Here,some gif picture can show you what can it do.
download the file name FxRecyclerView
and put it in your tornadofx project.
FxRecyclerView.kt
in the demo,I use a simple data class
data class Person(var name: String,var age:String)
the item in the demo is simple,just show the name and age of the Person
Note:your view must extends the View which is belong to tornadofx
import javafx.scene.control.Button
import javafx.scene.text.Text
import tornadofx.*
/**
*
* @author StarsOne
* @date Create in 2020/1/21 0021 18:36
* @description
*
*/
class ItemView : View("My View") {
var nameTv by singleAssign<Text>()
var ageTv by singleAssign<Text>()
var deleteBtn by singleAssign<Button>()
override val root = hbox {
spacing = 20.0
nameTv = text()
ageTv = text()
deleteBtn = button("delete")
}
}
Note:you must use the generics when you new a instance of FxRecyclerView
package com.example.demo.view
import tornadofx.*
class MainView : View("Hello TornadoFX") {
//must use the generics
//first is the data generics,second is the itemView generics
val rv = FxRecyclerView<Person,ItemView>()
override val root = vbox {
//other...
this+=rv
}
}
RvAdapter is an abstract class,so need to be extended and implement its method.
//1.init data source
val dataList = arrayListOf<Person>()
for (i in 0..10) {
dataList.add(Person("person$i",(18+i).toString()))
}
//2.init adapter
val adapter = object :RVAdapter<Person,ItemView>(dataList){
//3.override some necessary methods
override fun onRightClick(itemView: ItemView, position: Int) {
println("right onclick $position")
}
//onclick event
override fun onClick(itemView: ItemView, position: Int) {
println("onclick $position")
}
override fun onCreateView(): ItemView {
return ItemView()
}
//bind data to change the itemview gui
override fun onBindData(itemView: ItemView, bean: Person, position: Int) {
itemView.nameTv.text = bean.name
itemView.ageTv.text = bean.age
itemView.deleteBtn.setOnAction {
rv.remove(position)
}
}
}
//4.set adapter
rv.adapter = adapter
Note:Following methods is in the FxRecyclerView object.
method name | parma description | method description |
---|---|---|
setWidth(double) | double类型的数值 | 设置宽度 |
setHegiht(double) | double of data | 设置高度 |
setIsShowHorizontalBar(String) | way,has 3 choose,never(never show) always(always show) asneed(show when it need) | set the horizontal bar show |
addList(arraylist) | ArrayList of data | add a group of data,and refresh the item view |
addList(list) | List of data | add a group of data,and refresh the item view |
add(beanT) | ||
add(bean,int) | ||
update(bean,int) | update the data of the assigned index and refresh item view | |
update(bean,oldBean) | update the old data which is exist in the bean list and refresh the item view | |
remove(bean) | ||
remove(index) | ||
removeAll() |