Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

SlideAdapter

> 编程语言
Open source

一句代码搞定 RecycleView 侧滑菜单、添加头部底部、加载更多 , 像 Glide 一样简洁的链式调用轻松实现 Adapter

189 stars0 likes1 views
WebsiteGitHub

About

一句代码搞定 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 为:

java

    

数据类为:

java
public class Bean {

    private String info;

    public Bean(String info) {
        this.info = info;
    }
    // 省略 get、set 方法...
}

数据为:

java
 final List data = new ArrayList<>();
 for (int i = 0; i 

        

对 rightMenu_Like 控件设置点击事件监听,就像对 item 中的普通控件设置一样:

java
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
                     }
                 });
     }
};

6.多种 item 布局:

java
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);

7.添加头部:

java
SlideAdapter.load(data)
            .item(R.layout.item)
            .header(R.layout.head,0.1f) //添加一个头部,高为屏幕高度的 10%
            .padding(2)
            .bind(itemBind)
            .into(recyclerView);
java
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);

8.头部的数据绑定及事件监听:

假设头部布局 head.xml 为下:

java

    

对头部的数据绑定及事件监听在 HeaderBind 中实现:

java
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);

9.添加底部:

java
SlideAdapter.load(data)
            .item(R.layout.item)
            .footer(R.layout.foot,0.1f)    //添加一个底部,高为屏幕高度的 10%
            .padding(2)
            .bind(itemBind)
            .into(recyclerView);
java
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);

10.底部的数据绑定及事件监听:

假设底部布局 foot.xml 为下:

java

    

对底部的数据绑定及事件监听在 FooterBind 中实现:

…

11.加载更多:

…

onBottom 方法中的 footer 为最后一个底部,若未添加底部,则 footer 为空。其中 SlideAdapter 的 loadMore() 方法专门用来加载更多数据,需在主线程中调用。

总结

这是我的第一个开源库,功力尚浅,如果你在使用过程中遇到了什么问题,随时可以向我反馈,我会第一时间回复并改正。如果你想在此库的基础上添加自己的功能,欢迎 fork、欢迎 star 。

License

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· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Java

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言