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

Season發表於2018-06-06

yaksa.png

Yaksa

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

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

我們一次又一次不斷的重複著這樣無聊的工作,顯得那麼的麻木不仁

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

讓我們一起和噩夢一樣的Adapter和ViewHolder說聲再見,一起來擁抱Yaksa吧!

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

Talk is cheap, show me the code

渲染一個Linear列表:

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就好了!

效果圖:

screenshot.png

渲染一個Grid列表:

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)
複製程式碼

相關文章