Android StickListView实现悬停效果
作者:pengkv 时间:2022-06-06 15:54:35
先看看效果图:
实现思路:
监听ListView的滑动,等目的项为列表第一个可见的ItemView时,添加一个一个的布局,产生悬停效果
实现代码:
public class CustomViewAcyivity extends BaseActivity {
Toolbar toolbar;
WindowManager mWindowManager;
WindowManager.LayoutParams mWindowLayoutParams;
TextView mTv;
boolean isShowing;//是否正在显示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_view);
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
List<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
list.add("我是第" + (i + 1) + "个选择项");
}
toolbar = $(R.id.toolbar);
ListView listView = $(R.id.view_list);
listView.setAdapter(new ArrayAdapter<>(this, R.layout.item_text, list));
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem > 8) {//滑动到目的项时,显示悬停布局
if (!isShowing)
show();
} else {
if (isShowing)
hide();
}
}
});
}
//显示悬停布局
public void show() {
isShowing = true;
mWindowLayoutParams = new WindowManager.LayoutParams();
mWindowLayoutParams.format = PixelFormat.TRANSLUCENT; //图片之外的其他地方透明
mWindowLayoutParams.gravity = Gravity.TOP;
mWindowLayoutParams.y = toolbar.getHeight() - SystemUtil.getStatusHeight(this);//设置悬停布局显示的Y坐标
mWindowLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
mWindowLayoutParams.height = UnitUtil.dp2px(this, 50);//设置悬停布局显示的高度
mWindowLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
//设置悬停布局,为了看起来是悬停效果,布局的内容要设置成与ItemView一致
mTv = new TextView(this);
mTv.setGravity(Gravity.CENTER);
mTv.setBackgroundColor(getResources().getColor(R.color.white));
mTv.setTextSize(UnitUtil.px2sp(this, UnitUtil.dp2px(this, 16)));
mTv.setText("我是第10个选择项");
//添加悬停布局
mWindowManager.addView(mTv, mWindowLayoutParams);
}
//隐藏悬停布局
public void hide() {
if (mTv != null) {
isShowing = false;
mWindowManager.removeView(mTv);
mTv = null;
}
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/view_toolbar" />
<ListView
android:id="@+id/view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
标签:Android,StickListView,悬停
0
投稿
猜你喜欢
MyBatisPlus中CRUD使用方法详解
2023-04-10 18:31:43
利用logback 设置不同包下的日志级别
2022-08-11 20:05:43
BeanUtils.copyProperties使用总结以及注意事项说明
2023-06-27 18:06:18
使用@Value值注入及配置文件组件扫描
2023-12-01 21:24:12
简单工厂模式_动力节点Java学院整理
2022-07-22 16:42:08
Android登录注册功能 数据库SQLite验证
2023-10-01 20:58:55
VC对自定义资源加密解密(AES)的详解
2023-01-10 00:27:15
SpringBoot LocalDateTime格式转换方案详解(前端入参)
2022-01-05 06:48:03
通过Java实现在Word中创建可填充表单
2023-08-05 21:11:40
C语言多种获取字符串长度的方法
2021-07-01 16:30:29
.net的命名空间类库的简单介绍
2023-01-19 17:56:27
SpringBoot+MQTT+apollo实现订阅发布功能的示例
2021-08-30 22:36:46
SpringBoot扫描不到Controller的解决方案
2022-07-19 02:05:07
Springboot2以代码的方式统一配置Jackson教程
2021-08-30 16:47:33
Java安全之Tomcat6 Filter内存马问题
2022-11-20 07:29:23
Android开发中amera2 Preview使用详解
2023-11-09 20:17:56
springboot集成mybatis plus和dynamic-datasource注意事项说明
2023-12-05 03:54:21
Spring main方法中如何调用Dao层和Service层的方法
2023-11-28 23:15:19
SpringBoot2零基础到精通之映射与常用注解请求处理
2022-06-11 15:41:51
C#超市收银系统设计
2023-06-21 00:00:09