你還在用Adapter和ViewHolder寫RecyclerView嗎?Out了!

yaksa.png

Yaksa

一個輕量級的RecyclerView工具,讓你像Javascript同樣渲染Item.git

衆所周知,熟練使用RecyclerView展現列表數據已是每一個Android開發者必備的能力,然而,RecyclerView仍然有它的不足, 那就是過於繁瑣,相信每一個開發者都有過這樣的經歷,咱們爲了展現一個只有單一類型的簡單的列表,須要建立一個Adapter, 須要建立一個ViewHolder,而對於具備多個類型的稍微複雜的列表,咱們不但須要建立Adapter,同時還要建立多個ViewHolder..github

咱們一次又一次不斷的重複着這樣無聊的工做,顯得那麼的麻木不仁express

然而,生活不止眼前的苟且,還有詩和遠方!apache

讓咱們一塊兒和噩夢同樣的Adapter和ViewHolder說聲再見,一塊兒來擁抱Yaksa吧!bash

Yaksa(夜叉), 提升16點敏捷  15%攻擊速度  10%移動速度app

Talk is cheap, show me the code

渲染一個Linear列表:less

recycler_view.linear {
    item {
        HeaderItem("This is a Header")
    }

    itemDsl(index = 0) {
        xml(R.layout.header_item)
        render {
            it.tv_header.text = "This is a dsl Header"
            it.setOnClickListener { toast("DSL Header Clicked") }
        }
    }

    data.forEach { each ->
        itemDsl {
            xml(R.layout.list_item)
            render {
                it.textView.text = each.title
            }
        }
    }
}
複製代碼

就是這樣,沒有Adapter,也沒有ViewHolder,你只須要專心的渲染Item就行了!dom

效果圖:工具

screenshot.png

渲染一個Grid列表:ui

rv_list.grid {
    spanCount(SPAN_COUNT)
    
    item {
        HeaderItem("This is a Header")
    }
    
    itemDsl(index = 0) {
        gridSpanSize(SPAN_COUNT)
        
        xml(R.layout.header_item)
        render {
            it.tv_header.text = "This is a dsl Header"
            it.setOnClickListener { toast("DSL Header Clicked") }
        }
    }
    
    data.forEach { each ->
        itemDsl {
            xml(R.layout.list_item)
            render {
                it.textView.text = each.title
            }
            renderX { position, it ->
                it.setOnClickListener { toast("Clicked $position") }
            }
        }
    }
}

複製代碼

效果圖:

sceenshot-grid.png

瀑布流? 沒問題:

rv_list.stagger {
    spanCount(3)
    
    item {
        HeaderItem("This is a Header")
    }
    
    itemDsl(index = 0) {
        staggerFullSpan(true)
        
        xml(R.layout.header_item)
        render {
            it.tv_header.text = "This is a dsl Header"
            it.setOnClickListener { toast("DSL Header Clicked") }
        }
    }
    
    data.forEach { each ->
        item {
            ListItem(each.title, HEIGHTS[Random().nextInt(HEIGHTS.size)].px)
        }
    }
}
複製代碼

效果圖:

screenshot-stagger.png

其他的Header,Footer,多種type類型更是不在話下,並且重要的是,這些都不須要你寫任何的ViewHolder和Adapter

如今就開始裝備夜叉吧,開啓你的超神之路!

Github地址:github.com/ssseasonnn/…

License

Copyright 2018 Season.Zlc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```![yaksa.png](https://upload-images.jianshu.io/upload_images/1008453-bcdfe27bf44ebd16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
複製代碼
相關文章
相關標籤/搜索