ScrollView 实现子视图滑动到顶部时固定不动
2024-08-30 05:33:32
这个,个人建议使用自己写的布局使用view的gon或者visble的方法,使用design包中的控件来的话,局限性很大
方法有倆
(1)自定义ScrollView
重写ScrollView 的 computeScroll()方法 监听滑动,然后去判断你想要的布局是否已经到了顶部,如果到了,其实我最开始就写了两个一模一样的布局一个放在屏幕的最上方只不过一直是隐藏的这个时,就需要把它显示出来就可以了
public class MScrollView extends ScrollView { View v1;
View v2; public MScrollView(Context context) {
super(context);
init();
} public MScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public MScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
init();
} private void init() {
v2 = findViewById(R.id.Weekend2);
} public void setV1(View v1) {
this.v1 = v1;
} @Override
public void computeScroll() {
if (getScrollY() >= v2.getTop()) {
v1.setVisibility(View.VISIBLE);
} else {
v1.setVisibility(View.GONE);
}
super.computeScroll();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"> <com.example.vsat.test.MScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="星期一" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="星期二" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="星期三" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="星期四" /> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="星期五" /> <TextView
android:id="@+id/Weekend2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#90C451"
android:gravity="center"
android:text="喂!你醒醒!再坚持一下,马上就周末了" /> <com.example.vsat.test.MListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </LinearLayout> </com.example.vsat.test.MScrollView> <TextView
android:id="@+id/Weekend1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#90C451"
android:gravity="center"
android:text="喂!你醒醒!再坚持一下,马上就周末了"
android:visibility="gone" />
</RelativeLayout>
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); MScrollView scrollView = (MScrollView) findViewById(R.id.scrollView);
MListView listView = (MListView) findViewById(R.id.listView);
View v = findViewById(R.id.Weekend1);
scrollView.setV1(v);
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("第" + i + "号机器人");
} ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_expandable_list_item_1,
list);
listView.setAdapter(adapter);
listView.setFocusable(false);
} }
(2)这种只是给无奈用了design的人使用的
附上链接:http://www.jianshu.com/p/abdb9828a00d
- 将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLayout之内
- 将CollapsingToolbarLayout的app:layout_scrollFlags设置为scroll
- 给滚动的NestedScroolView设置
app:layout_behavior="@String/appbar_scrolling_view_behavior"
就大功告成了(记得根布局要是CoordinatorLayout)
这种方法如果是要固定下拉列表等等比较复杂的布局,就会很是尴尬
最新文章
- 【Android】不依赖焦点和选中的TextView跑马灯【2】
- Mac OS 下 eclipse中文乱码解决方法(eclipse for mac 中文乱码)
- 使用Rsync进行文件的同步与备份
- Centos修改镜像为国内的163源
- for循环与for in循环
- Dubbo 源码安装与编译
- Delphi CxGrid 汇总(2)
- BZOJ1821: [JSOI2010]Group 部落划分
- MAX16054
- WPF中实现根据拼音查找汉字
- layout_weight 的解释及使用
- PowerDesigner建模应用(二)逆向工程,导出PDM文件前过滤元数据(表、视图、存储过程等)
- 总结下Redux
- 关于局域网 手机连接apache服务器报403forbidden错误参考如下2017.6.6
- PowerDesigner设置一对一关系
- React 添加对 Less 的支持, 使用 create-react-app 脚手架
- display:inline-block与float
- JAVA操作mysql
- SQL Server上DBLINK的创建,其实很简单!(上)
- 【Leetcode】179. Largest Number
热门文章
- java基础之BigInteger
- Python学习day05 - Python基础(3) 格式化输出和基本运算符
- elasticsearch 中文API(二)
- Vue配置多个跨域目标链接
- 跟我一起实战美团网一之[nodemon] app crashed - waiting for file changes before starting...
- [转]模块化——Common规范及Node模块实现(二)
- 工控安全入门(六)——逆向角度看Vxworks
- ArcGIS Server 10.1安装、配置、发布地图服务
- 仓库盘点功能-ThinkPHP_学习随笔
- day1(老男孩-Python3.5-S14期全栈开发)