基于GridLayout封装的自定义依赖库控件ScheduleView
程序员文章站
2022-06-15 21:17:21
基于GridLayout封装的自定义日程信息控件ScheduleViewGit地址效果图同时实现多种不同的子布局,支持自定义layout添加依赖库Add it in your root build.gradle at the end of repositories:allprojects {repositories {...maven { url 'https://jitpack.io' }}}Add the dependency:dependencies...
基于GridLayout封装的自定义依赖库控件ScheduleView
效果图
简单实现日程表,同时实现多种不同的子布局,支持自定义layout
添加依赖库
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency:
dependencies {
implementation 'com.github.kirito0206:ScheduleView:1.0.2'
}
使用方式
在XML文件中添加
<com.example.scheduleview.ScheduleView
android:id="@+id/sv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="12"
android:columnCount="8"/>
设置适配器类
/*
可以传入数据类、横坐标Title、纵坐标Title的list
数据类需要实现获取对应的x、y范围
*/
class SVAdapter(val context : Context,val list: ArrayList<T>) : ScheduleAdapter() {
//返回日程表中的item数
override fun getItemCount(): Int {
return list.size
}
//获取item对应的横坐标
override fun getDay(position: Int): Int {
return list[position].day
}
//获取item对应的纵坐标范围
override fun getBoundary(position: Int): Pair<Int, Int> {
return Pair(list[position].timeX, list[position].timeY)
}
//设置item的布局样式,返回对应view
override fun getView(position: Int): View {
//子布局只有单一控件,如TextView、EditView、ImageView等
//可以像如下设置
val textView = TextView(context)
textView.text = position.toString()
textView.gravity = Gravity.CENTER
return textView
//自己创建layout,如下进行初始化
val inflater = LayoutInflater.from(context)
var view = inflater.inflate(R.layout.item_schedule, null)
//view.textview.text = "内容"...
view.foregroundGravity = Gravity.CENTER
return view
}
//设置横坐标Title
override fun getXTitle(dayIndex: Int): String {
return "XTitle"
}
//设置纵坐标Title
override fun getYTitle(timeIndex: Int): String {
return "YTitle"
}
}
然后在代码中初始化view即可
var adapter = SVAdapter(this,list)
sv_main.adapter = adapter
sv_main.post {
sv_main.show()
}
项目局部实现方式
封装适配器基类,使得获取到对应的数据
abstract class ScheduleAdapter {
abstract fun getItemCount() : Int
abstract fun getDay(position: Int) : Int
abstract fun getBoundary(position : Int) : Pair<Int,Int>
abstract fun getView(position: Int) : View
abstract fun getXTitle(position: Int) : String
abstract fun getYTitle(position: Int) : String
}
Gridlayout动态添加自定义的布局(困扰了很久
view调用post方法(所有view绘制结束后进行,就可以获取到宽高
//在scheduleview中,调用adapter获取所需的对应数据
for (i in 0 until adapter!!.getItemCount()){
var t = adapter!!.getBoundary(i)
val rowSpec: Spec =
spec(t.first, t.second - t.first +1)
val columnSpec = spec(adapter!!.getDay(i), 1)
val params =
LayoutParams(rowSpec, columnSpec)
params.width = (this.width-48)/columnCount
params.height = (this.height-48)/rowCount * (t.second - t.first +1)
params.setMargins(5,5,5,5)
params.setGravity(Gravity.CENTER and Gravity.FILL)
this.addView(adapter!!.getView(i),params)
}
layout里的wrap_content和match_parent不起作用
所以也在gridlayout绘制之后,获取对应的宽高实现
本文地址:https://blog.csdn.net/qq_45212775/article/details/110249830
上一篇: Android文字转语音(TTS)
下一篇: 了解IM即时通讯