一行代码即可完成 RecycleView 的侧滑菜单、添加头部和底部、加载更多内容,就像 Glide 一样简洁的链式调用,轻松实现 Adapter
一行代码即可完成 RecycleView 的侧滑菜单、添加头部和底部、加载更多内容,就像 Glide 一样简洁的链式调用,轻松实现 Adapter
在 动手打造史上最简单的 Recycleview 侧滑菜单 中,萌生了将这种方案封装为一个开源库的想法,旨在实现调用方式最简单,且又不失可定制性。本库最大的特点的是采用了 Glide 简洁明了的链式调用方式,一句代码即可添加侧滑菜单、头部底部等。
1.自定义侧滑菜单布局
2.添加头部、底部
3.轻松实现加载更多
4.设置 item 间距
5.多种 item 类型
6.支持 LinearLayout 及 GridLayout
7.一句代码实现所有功能
左侧滑菜单、右侧滑菜单、自定义菜单布局:
头部、多头部:
底部、多底部、加载更多:
第 1 步、在工程的 build.gradle 中添加:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}第 2 步、在应用的 build.gradle 中添加:
dependencies {
compile 'com.github.yhaolpz:SlideAdapter:1.0.0'
}下面通过简单案例演示如何在程序中使用 SlideAdapter,假设 item.xml 为:
数据类为:
public class Bean {
private String info;
public Bean(String info) {
this.info = info;
}
// 省略 get、set 方法...
}
数据为:
final List data = new ArrayList<>();
for (int i = 0; i
对 rightMenu_Like 控件设置点击事件监听,就像对 item 中的普通控件设置一样:
ItemBind itemBind = new ItemBind() {
@Override
public void onBind(ItemView itemView, Bean data, int position) {
itemView.setOnClickListener(R.id.rightMenu_Like, new View.OnClickListener() {
@Override
public void onClick(View view) {
//点击rightMenu_Like
}
});
}
};SlideAdapter.load(data)
.item(R.layout.item,0,0,R.layout.menu,0.35) // 添加次序为 1
.item(R.layout.item) // 添加次序为 2
.type(new ItemType() {
@Override
public int getItemOrder(Bean data, int position) {
// 返回 item 添加次序
return position % 2 == 0 ? 1 : 2;
}
})
.padding(2)
.bind(itemBind)
.into(recyclerView);SlideAdapter.load(data)
.item(R.layout.item)
.header(R.layout.head,0.1f) //添加一个头部,高为屏幕高度的 10%
.padding(2)
.bind(itemBind)
.into(recyclerView);SlideAdapter.load(data)
.item(R.layout.item)
.header(R.layout.head) //添加第1个头部
.header(R.layout.head2,0.1f) //添加第2个头部
.padding(2)
.bind(itemBind)
.into(recyclerView);假设头部布局 head.xml 为下:
对头部的数据绑定及事件监听在 HeaderBind 中实现:
SlideAdapter.load(data)
.item(R.layout.item)
.header(R.layout.head,0.2f) //添加第1个头部
.header(R.layout.head2) //添加第2个头部
.bind(new HeaderBind() {
@Override
public void onBind(ItemView header, int order) {
if (order == 1) {
header.setText(R.id.headText, "我是第一个头部");
}
...
}
})
.padding(2)
.bind(itemBind)
.into(recyclerView);SlideAdapter.load(data)
.item(R.layout.item)
.footer(R.layout.foot,0.1f) //添加一个底部,高为屏幕高度的 10%
.padding(2)
.bind(itemBind)
.into(recyclerView);SlideAdapter.load(data)
.item(R.layout.item)
.footer(R.layout.foot) //添加第1个底部
.footer(R.layout.foot,0.1f) //添加第2个底部
.padding(2)
.bind(itemBind)
.into(recyclerView);假设底部布局 foot.xml 为下:
对底部的数据绑定及事件监听在 FooterBind 中实现:
……onBottom 方法中的 footer 为最后一个底部,若未添加底部,则 footer 为空。其中 SlideAdapter 的 loadMore() 方法专门用来加载更多数据,需在主线程中调用。
这是我的第一个开源库,功力尚浅,如果你在使用过程中遇到了什么问题,随时可以向我反馈,我会第一时间回复并改正。如果你想在此库的基础上添加自己的功能,欢迎 fork、欢迎 star 。
Copyright 2017 Wang YingHao
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.暂无开放 Issues,或尚未同步最近议题。