Giter VIP home page Giter VIP logo

databindingbaseadapter's Introduction

DataBindingBaseAdapter

结合DataBinding的万能Adapter,使代码更简洁,快速提高开发效率的通用Adapter

Add it in your root build.gradle at the end of repositories:

《一》添加依赖:

(1)Add it in your root build.gradle at the end of repositories:

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
}

(2) Add the dependency

dependencies {
	         implementation 'com.github.GraceJoJo:DataBindingBaseAdapter:1.0.1'
}

《二》代码中使用: (1)RecyclerView的Adapter

class ADA_RecyclerItem constructor(context: Context): CommonAdapter<DataBean, ItemLayoutBinding>(context) {
    override fun convert(viewBinding: ItemLayoutBinding?, holder: ViewHolder.BindingHolder?, bean: DataBean?, position: Int) {
        viewBinding!!.dataBean = bean
    }

    override fun itemLayoutId(): Int {
        return R.layout.item_layout
    }
}

Activity或Fragment使用:

        val dataList= ArrayList<DataBean>() //模拟数据
        recyclerview.layoutManager = LinearLayoutManager(this)
        var mAdapter = ADA_RecyclerItem(this)
        recyclerview.adapter = mAdapter
        mAdapter.update(dataList,true)

(2)ListView或者GridView的Adapter

class ADA_ListItem constructor(context: Context): CommonAdapterListView<DataBean, ItemLayoutBinding>(context) {

    override fun convert(viewBinding: ItemLayoutBinding?, holder: ViewHolderListView?, bean: DataBean?, position: Int) {
        viewBinding!!.dataBean = bean
    }

    override fun itemLayoutId(): Int {
        return R.layout.item_layout
    }
}

Activity或Fragment使用:

        val dataList= ArrayList<DataBean>() //模拟数据
        var mAdapter = ADA_ListItem(this)
        listview.adapter = mAdapter
        mAdapter.update(dataList,true)

《二》实现效果:

image

《三》优点:

(1)在布局中就把view的数据绑定工作全做好了。

(2)在Adapter中可以省略大量重复的设置数据的代码

(3)使得代码更加简洁,一目了然,易于维护。

致谢:鸿洋大神的[BaseAdapter](https://github.com/hongyangAndroid/baseAdapter)

databindingbaseadapter's People

Contributors

gracejojo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.