在写项目的时候一般都会写个BaseActivity ,来减少重复代码编写和共有内容的整理封装(侵删)
前几天,用kotlin语言写了个BaseActivity和BaseAdapter
界面是多么不堪入目,不过不要在意细节,我也挺绝望的(写PDA多了发现这样也比那好看多了)
引用
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
implementation "org.jetbrains.anko:anko-sdk15:0.9.1"
implementation "org.jetbrains.anko:anko-appcompat-v7:0.9.1"
implementation "org.jetbrains.anko:anko-design:0.9.1"
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.github.d-max:spots-dialog:0.7@aar'
implementation ('com.android.support:cardview-v7:26.1.0'){
exclude group: 'com.android.support'
}
BaseAdapter
abstract class BaseActivity:AppCompatActivity(), BaseView {
abstract fun getContentViewId():Int
var mContext: Context?=null
lateinit var dialog: SpotsDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(getContentViewId())
mContext=this
dialog=SpotsDialog(this)
initView()
initEvent()
}
//显示加载
fun showLoad(){
dialog.show()
}
//隐藏加载
fun disLoad(){
dialog.dismiss()
}
//提示
override fun showTipMessage( msg: String) {
Toast.makeText(this,msg,Toast.LENGTH_SHORT).show()
}
override fun getNetKey(): String {
return javaClass.simpleName
}
//移除
fun removeActivity(cls:Class<*>){
ActivityManager.instance.finishActivity(cls)
}
fun actCount(){
ActivityManager.instance.ListCount()
}
/**
* 触发手机返回按钮
*/
override fun onBackPressed(){
ActivityManager.instance.removeActivity(this@BaseActivity)
}
//anko
fun backShow(){
alert("返回会清除该页面数据") {
title("提示")
yesButton { onBackPressed() }
noButton { }
}.show()
}
//anko
fun setDialog(title:String, content:String ){
alert(content) {
title(title)
yesButton { }
noButton { }
}.show()
}
override fun onDestroy() {
// presenter.dispose()
// presenter.detach()
super.onDestroy()
}
/**
* 触发手机返回按钮
*/
fun onFinish(){
ActivityManager.instance.removeActivity(this@BaseActivity)
}
}
由于写BaseAdapter是用kotlin的file写的,emmmm。。。有点小问题,不过不影响使用
class BaseViewHolder(itemView: View, private val mOnItemClickListener:BaseAdapter.OnItemClickListener?, private val mOnItemLongClickListener: BaseAdapter.OnItemLongClickListener?): RecyclerView.ViewHolder(itemView) , View.OnClickListener, View.OnLongClickListener {
override fun onClick(v: View?) {
mOnItemClickListener.let { v?.let { it1 -> mOnItemClickListener?.onItemClick(it1, getLayoutPosition()) } }
}
override fun onLongClick(v: View?): Boolean {
if (mOnItemLongClickListener != null) {
v?.let { it ->
mOnItemLongClickListener?.onLongItemClick(it, getLayoutPosition())
}
}
return true
}
private var views: SparseArray? = null
init {
itemView.setOnClickListener(this)
itemView.setOnLongClickListener(this)
this.views = SparseArray()
}
fun getTextView(viewId: Int): TextView {
return retrieveView(viewId)
}
fun getButton(viewId: Int): Button {
return retrieveView(viewId)
}
fun getImageView(viewId: Int): ImageView {
return retrieveView(viewId)
}
fun getView(viewId: Int): View {
return retrieveView(viewId)
}
fun getLinearlayout(viewId: Int): LinearLayout {
return retrieveView(viewId)
}
fun getCheckBox(viewId: Int): CheckBox {
return retrieveView(viewId)
}
fun retrieveView(viewId: Int): T {
var v = views?.get(viewId)
if (v == null) {
v = itemView.findViewById(viewId)
views?.put(viewId, v)
}
return (v as T?)!!
}
abstract class BaseAdapter @JvmOverloads constructor(protected val context: Context, protected var layoutResId: Int, var datas: MutableList) : RecyclerView.Adapter() {
private var mOnItemClickListener: OnItemClickListener? = null
private var mOnItemLongClickListener: OnItemLongClickListener? = null
fun addDatas(datas: List) {
if (datas != null) {
this.datas?.addAll(datas)
notifyItemRangeChanged(this.datas!!.size - datas.size, datas.size)
}
}
fun clearData() {
datas?.clear()
notifyDataSetChanged()
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): BaseViewHolder {
val view = LayoutInflater.from(context).inflate(layoutResId, viewGroup, false)
return BaseViewHolder(view, mOnItemClickListener, mOnItemLongClickListener)
}
override fun onBindViewHolder(holder: BaseViewHolder?, position: Int) {
val item = getItem(position)
convert(holder as H, item)
}
override fun getItemViewType(position: Int): Int {
return super.getItemViewType(position)
}
fun getItem(position: Int): T? {
return if (position >= datas!!.size) null else datas!![position]
}
override fun getItemCount(): Int {
return if (datas == null || datas!!.size <= 0) 0 else datas.size
}
fun clear() {
val it = datas!!.iterator()
while (it.hasNext()) {
val position = datas!!.indexOf(it.next())
it.remove()
notifyItemRemoved(position)
}
}
fun addData(position: Int, list: List?) {
list.let {
if (list?.count() as Int > 0) {
for (t in list) {
datas!!.add(position, t)
notifyItemInserted(position)
}
}
}
}
fun removeItem(t: T) {
datas?.remove(t)
notifyItemRemoved(datas!!.indexOf(t))
}
public interface OnItemClickListener {
fun onItemClick(view: View, position: Int)
}
public interface OnItemLongClickListener {
fun onLongItemClick(view: View, position: Int)
}
protected abstract fun convert(viewHoder: H, item: T?)
public fun setOnItemClickListener(listener: OnItemClickListener) {
this.mOnItemClickListener = listener
}
public fun setOnLongClickListener(listener: OnItemLongClickListener) {
this.mOnItemLongClickListener = listener
}
}
}
写了个TestAdapter继承这个BaseAdapter
class TestAdapter (context:Context,layoutId:Int,list:ArrayList):BaseViewHolder.BaseAdapter(context,layoutId,list){
override fun convert(viewHoder: BaseViewHolder, item: String?) {
viewHoder.getTextView(R.id.textitem).text=item
}
}
在Activity的调用
adapter= TestAdapter(this,R.layout.test_item,list);
recyclerView.adapter=adapter
kotlin的绑定adapter这样的==java的setadapter()
recyclerView.adapter=adapter
对item的点击
//点击
adapter?.setOnItemClickListener(object :BaseViewHolder.BaseAdapter.OnItemClickListener{
override fun onItemClick(view: View, position: Int) {
showTipMessage(list[position])
}
})
//长按
adapter?.setOnLongClickListener(object :BaseViewHolder.BaseAdapter.OnItemLongClickListener{
override fun onLongItemClick(view: View, position: Int) {
showTipMessage("长点击:"+list[position])
}
})
大致就是这样吧,具体业务还是得具体分析,继续往里面进行拓展, 还需要多磨合才了解kotlin
代码