Code Monkey home page Code Monkey logo

rvadapterforkotlin's Introduction

RvAdapterForKotlin

使用Kotlin写的实现Android的Recyclerview的多种item布局

Gradle

dependencies {
    ...
    compile 'com.jowanlfm.support:rvadapter:1.0.0'
}

使用

一种item布局

1.创建一个itemView

class SingleItemDelegate : DelegateType<TestBean> {
    override val itemViewLayoutId: Int
        get() = R.layout.item_left

    override fun isItemViewType(item: TestBean, position: Int): Boolean = true

    override fun convert(context: Context, holder: ViewHolder, item: TestBean, position: Int) {
        with(holder.itemView) {
            item_left_text.text = item.text
            setOnClickListener {
                context.toast("SingleItemDelegate")
            }
        }
    }
}

2.添加到Adapter里面

class SingleItemAdapter(mContext: Context, mDatas: List<TestBean>)
    : DelegateItemAdapter<TestBean>(mContext, mDatas) {
    init {
        addItemViewDelegate(SingleItemDelegate())
    }
}

3.设置RecyclerView的Adapter

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val datas = listOf<TestBean>(TestBean("one", "one"), TestBean("two", "two"), TestBean("three", "three"), TestBean("two", "two"), TestBean("three", "three"), TestBean("one", "one"), TestBean("one", "one"), TestBean("one", "one"), TestBean("three", "three"), TestBean("two", "two"), TestBean("three", "three"), TestBean("two", "two"), TestBean("three", "three"), TestBean("one", "one"), TestBean("one", "one"), TestBean("one", "one"), TestBean("three", "three"), TestBean("one", "one"))
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = SingleItemAdapter(this, datas)
    }
}

4.效果图

SingleItem

多种item布局

1.创建多个个itemView

class LeftDelegate : DelegateType<TestBean> {
    override val itemViewLayoutId: Int
        get() = R.layout.item_left

    override fun isItemViewType(item: TestBean, position: Int): Boolean = "one" == item.type

    override fun convert(context: Context, holder: ViewHolder, item: TestBean, position: Int) {
        with(holder.itemView) {
            item_left_text.text = item.text
            setOnClickListener {
                context.toast("LeftDelegate")
            }
        }
    }
}

class CenterDelegate : DelegateType<TestBean> {
    override val itemViewLayoutId: Int
        get() = R.layout.item_center

    override fun isItemViewType(item: TestBean, position: Int): Boolean = "two" == item.type

    override fun convert(context: Context, holder: ViewHolder, item: TestBean, position: Int) {
        with(holder.itemView) {
            item_center_text.text = item.text
            setOnClickListener {
                context.toast("CenterDelegate")
            }
        }
    }
}

class RightDelegate : DelegateType<TestBean> {
    override val itemViewLayoutId: Int
        get() = R.layout.item_right

    override fun isItemViewType(item: TestBean, position: Int): Boolean = "three" == item.type

    override fun convert(context: Context, holder: ViewHolder, item: TestBean, position: Int) {
        with(holder.itemView) {
            item_right_text.text = item.text
            setOnClickListener {
                context.toast("RightDelegate")
            }
        }
    }
}

2.添加到Adapter里面

class MultiItemAdapter(mContext: Context, mDatas: List<TestBean>) 
    : DelegateItemAdapter<TestBean>(mContext, mDatas) {
    init {
        addItemViewDelegate(LeftDelegate())
        addItemViewDelegate(CenterDelegate())
        addItemViewDelegate(RightDelegate())
    }
}

3.设置RecyclerView的Adapter

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val datas = listOf<TestBean>(TestBean("one", "one"), TestBean("two", "two"), TestBean("three", "three"), TestBean("two", "two"), TestBean("three", "three"), TestBean("one", "one"), TestBean("one", "one"), TestBean("one", "one"), TestBean("three", "three"), TestBean("two", "two"), TestBean("three", "three"), TestBean("two", "two"), TestBean("three", "three"), TestBean("one", "one"), TestBean("one", "one"), TestBean("one", "one"), TestBean("three", "three"), TestBean("one", "one"))
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = MultiItemAdapter(this, datas)
    }
}

4.效果图

MultiItem

rvadapterforkotlin's People

Contributors

wangzailfm avatar

Stargazers

king_z avatar Nokomi_Ei avatar  avatar  avatar Keke avatar 顾朝辉 avatar  avatar  avatar  avatar yhui avatar Bing avatar  avatar

Watchers

James Cloos avatar  avatar

rvadapterforkotlin's Issues

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.