ScrollView 实现子视图滑动到顶部时固定不动
这个,个人建议使用自己写的布局使用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)
这种方法如果是要固定下拉列表等等比较复杂的布局,就会很是尴尬
ScrollView 实现子视图滑动到顶部时固定不动的更多相关文章
- 【转】 UIView如何管理它的子视图
原文:http://my.oschina.net/u/1984662/blog/293690 目录[-] Core Animation基础 改变视图的层 动画支持 视图坐标系统 边框.边界.和中心的关 ...
- Android ScrollView监听滑动到顶部和底部的两种方式(你可能不知道的细节)
Android ScrollView监听滑动到顶部和底部,虽然网上很多资料都有说,但是不全,而且有些细节没说清楚 使用场景: 1. 做一些复杂动画的时候,需要动态判断当前的ScrollView是否滚动 ...
- iOS UITableViewCell点击时子视图背景透明的解决方法
在做iOS项目的开发中,UITableView控件的应用十分广泛.在进行自定义UITableViewCell时,经常遇到这样的问题:在UITableViewCell上面添加了一个有背景颜色的子视图,当 ...
- [iOS]在NavigationController中的ScrollView中的子视图都会下移64个像素
情况是这种: 我有一个UINavigationController,设置为self.window的root视图, 然后有一个UIVIewController是UINavigtionController ...
- listview、gradview滚动到最后时,滑动至顶部
listview.gradview滑动顶端.底部的判断及底部滑动至顶端 mPhotoWall.setOnScrollListener(new AbsListView.OnScrollListener( ...
- [RN] React Native ScrollView自动滑动到顶部
react-native 自动滚到屏幕顶部,模仿微信朋友圈评论自动定位 <ScrollView showsVerticalScrollIndicator={false} style={[styl ...
- iOS开发之多表视图滑动切换示例(仿"头条"客户端)---优化篇
前几天发布了一篇iOS开发之多表视图滑动切换示例(仿"头条"客户端)的博客,之所以写这篇博客,是因为一位iOS初学者提了一个问题,简单的写了个demo做了个示范,让其在基础上做扩展 ...
- iOS开发-自己定义重用机制给ScrollerView加入子视图
事实上这个问题我非常早就想过,仅仅是没有通过去写程序实现,昨天有人提起,我就巧了一下 不知道大家打印郭tableview:cellforrow中cell初始的次数,也就是重用池中的cell个数.这个是 ...
- android去掉滑动到顶部和底部的阴影
android去掉滑动到顶部和底部的阴影 <ListView android:id="@+id/listView" android:layout_width="ma ...
随机推荐
- Hie with the Pie (POJ 3311) 旅行商问题
昨天想练习一下状态压缩,百度搜索看到有博客讨论POJ 3311,一看就是简单的旅行商问题,于是快速上手写了状态压缩,死活样例都没过... 画图模拟一遍原来多个城市可以重复走,然后就放弃思考了... 刚 ...
- PAT甲级——A1079 Total Sales of Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...
- Flask ——路由系统
Flask中的路由系统其实我们并不陌生了,从一开始到现在都一直在应用 @app.route("/",methods=["GET","POST" ...
- java流对象
Java和C++都是静态类型的面向对象编程语言 stream结尾都是字节流,reader和writer结尾都是字符流 区别: 就是读写的时候一个是按字节读写,一个是按字符. 实际使用通常差不多. 在读 ...
- 解决Couldn't resolve host 'mirrorlist.centos.org
这个错误引起主要是因为环境读取不到yun引起的可以考虑/etc/sysconfig/network-scripts/ifcfg-eth0 配置出错导致网络不通.或者/etc/resolv.conf的D ...
- codeforces600E. Lomsat gelral(dsu on tree)
dsu on tree先分轻重儿子先处理轻边,再处理重儿子再加上轻儿子的答案 #include<iostream> #include<cstdio> #include<q ...
- 阿里OSS ajax方式 web直传
部分js代码 send_request = function(){//这是从后台获取认证策略等信息. var htmlobj=$.ajax({url:root+"/service/polic ...
- 深入浅出 Java Concurrency (13): 锁机制 part 8 读写锁 (ReentrantReadWriteLock) (1)[转]
从这一节开始介绍锁里面的最后一个工具:读写锁(ReadWriteLock). ReentrantLock 实现了标准的互斥操作,也就是一次只能有一个线程持有锁,也即所谓独占锁的概念.前面的章节中一直在 ...
- VirtualBox安装CentOS后分辨率和鼠标无缝切换问题
问题:VirtualBox安装完后出现分辨率只有800*600和1024*768,鼠标不能在虚拟机和本机件无缝切换. 解决办法:在终端中执行以下命令 yum install kernel yum in ...
- jqGrid首次加载时不加载任何数据
1. 首次加载时候设置 jqGrid 属性 datatype: 'local' $("#grid").jqGrid({ url:"#", datatype:&q ...