这个,个人建议使用自己写的布局使用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

    1. 将需要悬浮的layout放到CollapsingToolbarLayout之外,AppBarLayout之内
    2. 将CollapsingToolbarLayout的app:layout_scrollFlags设置为scroll
    3. 给滚动的NestedScroolView设置
      app:layout_behavior="@String/appbar_scrolling_view_behavior"
      就大功告成了(记得根布局要是CoordinatorLayout)

这种方法如果是要固定下拉列表等等比较复杂的布局,就会很是尴尬

ScrollView 实现子视图滑动到顶部时固定不动的更多相关文章

  1. 【转】 UIView如何管理它的子视图

    原文:http://my.oschina.net/u/1984662/blog/293690 目录[-] Core Animation基础 改变视图的层 动画支持 视图坐标系统 边框.边界.和中心的关 ...

  2. Android ScrollView监听滑动到顶部和底部的两种方式(你可能不知道的细节)

    Android ScrollView监听滑动到顶部和底部,虽然网上很多资料都有说,但是不全,而且有些细节没说清楚 使用场景: 1. 做一些复杂动画的时候,需要动态判断当前的ScrollView是否滚动 ...

  3. iOS UITableViewCell点击时子视图背景透明的解决方法

    在做iOS项目的开发中,UITableView控件的应用十分广泛.在进行自定义UITableViewCell时,经常遇到这样的问题:在UITableViewCell上面添加了一个有背景颜色的子视图,当 ...

  4. [iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素

    情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...

  5. listview、gradview滚动到最后时,滑动至顶部

    listview.gradview滑动顶端.底部的判断及底部滑动至顶端 mPhotoWall.setOnScrollListener(new AbsListView.OnScrollListener( ...

  6. [RN] React Native ScrollView自动滑动到顶部

    react-native 自动滚到屏幕顶部,模仿微信朋友圈评论自动定位 <ScrollView showsVerticalScrollIndicator={false} style={[styl ...

  7. iOS开发之多表视图滑动切换示例(仿"头条"客户端)---优化篇

    前几天发布了一篇iOS开发之多表视图滑动切换示例(仿"头条"客户端)的博客,之所以写这篇博客,是因为一位iOS初学者提了一个问题,简单的写了个demo做了个示范,让其在基础上做扩展 ...

  8. iOS开发-自己定义重用机制给ScrollerView加入子视图

    事实上这个问题我非常早就想过,仅仅是没有通过去写程序实现,昨天有人提起,我就巧了一下 不知道大家打印郭tableview:cellforrow中cell初始的次数,也就是重用池中的cell个数.这个是 ...

  9. android去掉滑动到顶部和底部的阴影

    android去掉滑动到顶部和底部的阴影 <ListView android:id="@+id/listView" android:layout_width="ma ...

随机推荐

  1. logcat日志文件

    android日志系统提供了记录和查看系统调试信息的功能,日志都是从各个软件和一些系统的缓冲区中记录下来的,缓冲区可以通过logcat命令来进行查看和使用 开发者选项,有个选项叫做“日志记录器缓冲区大 ...

  2. matlab-选择-循环-函数

    1 选择 3 循环 break 3 函数

  3. “玲珑杯”ACM比赛 Round #11 B题

    http://www.ifrog.cc/acm/problem/1097?contest=1013&no=1 //LIS的高端写法 #include <iostream> #inc ...

  4. jQuery3动画+创建元素

    一.jQuery的动画 1.jQuery自带的动画 1>变化的是width height opacity display <!DOCTYPE html> <html lang= ...

  5. LeetCode简单算法之分割平衡字符串 #1221

    在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的. 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串. 返回可以通过分割得到的平衡字符串的最大数量. 示例 1: 输入:s = ...

  6. 谈谈数据库sql编写

    本文主要给初学者关于关系数库的一个浮光掠影式的介绍,如果想深入理解,必须对于下文提到的每个内容单独深入学习! it-information technology的简称,中文是信息机技术,信息其实就是数 ...

  7. MySQL数据库 字段操作 多表关系(更新中...)

    外键 (foreign key) ## 外键 ```mysql # 作者(author):id,name,sex,age,mobile, detail_id # 作者详情(author_detail) ...

  8. docker启动单节点server模式的consul | Bitdoom

    原文:docker启动单节点server模式的consul | Bitdoom docker启动单节点server模式的consul 2017-09-07 环境:MacOSX, consul_0.9. ...

  9. hbase表内存的分布

  10. Vuejs实战项目五:数据列表

    1.在EasyMock 中添加数据列表模拟接口 请求url:/suyuan/list 请求方式:get 描述:数据列表 mock.js配置: 例: { "code": 2000, ...