listView悬浮头部的简单实现
2024-10-09 18:23:15
简而言之 为listView设置onScrollListener 当滑动时 firstVisibleItem>=要悬浮的 item的position时 让悬浮部分显示 否则隐藏
其实就是在listView之上用帧布局覆盖了一个 悬浮部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sticky);
invis = (LinearLayout) findViewById(R.id.invis); strs = new String[100]; for (int i = 0; i < 20; i++) {
strs[i] = "data-----" + i;
} lv = (ListView) findViewById(R.id.lv);
View header = View.inflate(this, R.layout.stick_header, null);//头部内容
lv.addHeaderView(header);//添加头部
lv.addHeaderView(View.inflate(this, R.layout.stick_action, null));//ListView条目中的悬浮部分 添加到头部 lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, strs));
lv.setOnScrollListener(new OnScrollListener() { @Override
public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
if (firstVisibleItem >= 1) {
invis.setVisibility(View.VISIBLE);
} else { invis.setVisibility(View.GONE);
}
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#332b3b"
android:gravity="center"
android:text="标题"
android:textColor="#ffffff" /> <FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title" > <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <LinearLayout
android:id="@+id/invis"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#ccedc7"
android:orientation="horizontal"
android:visibility="gone" > <TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="悬浮部分" />
</LinearLayout>
</FrameLayout> </RelativeLayout>
最新文章
- [.net 面向对象编程基础] (22) 事件
- 《30天自制操作系统》笔记(03)——使用Vmware
- java 堆栈分析3
- MongoDB3.0新特性
- 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
- ylb:SQL Server中的时间函数
- Javascript oop深入学习笔记(三)--javascript中类的实现
- bzoj2679:[Usaco2012 Open]Balanced Cow Subsets
- git设置过滤忽略的文件或文件夹
- Windows server 2012 各版本 激活方法
- 数据库 版本号是 661,打不开。此server支持 655 和更早的版本号。不支持降级路径
- FastJson的用法
- A+B problems
- Windows Developer Day - Adaptive Cards
- Linux:Day4(上) 文件管理、管道
- 学习STM32,你不得不了解的五大嵌入式操作系统
- 10 Rules of Highly Successful Project Management
- SQL备份一张表的数据
- Linux CentOS7系统中ssh的用法
- centos7上安装 mysql