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 ...
随机推荐
- Linux 启动、关闭、重启服务的两种方式
1.一种是可以使用service脚本来调度,如: service 服务名 startservice 服务名 stopservice 服务名 restart 2.第二种可以直接进入/etc/init.d ...
- linux查看系统内容
- python学习笔记3.1_数据读取常用函数参数
一.read_table/read_csv常用函数参数 1.path:表明文件系统位置的字符串.url或文件型对象 2.sep或delimiter:用于分隔每行字段的字符序列或正则表达式 3.head ...
- 网络结构解读之inception系列一:Network in Network
网络结构解读之inception系列一:Network in Network 网上有很多的网络结构解读,之前也是看他人博客的介绍,但当自己看论文的时候,发现存在很多的细节和动机解读,而这部分能加深 ...
- innodb状态
Innodb_buffer_pool_pages_data Innodb buffer pool缓存池中包含数据的页的数目,包括脏页.单位是page. Innodb_buffer_pool_pages ...
- Django项目:CRM(客户关系管理系统)--78--68PerfectCRM实现CRM业务流程(bpm)报名缴费分页
# pagination.py # ————————68PerfectCRM实现CRM业务流程(bpm)报名缴费分页———————— from django.utils.safestring impo ...
- LUOGU P4195 Spoj3105 Mod
题面 bsgs问题.因为p可能不为质数,所以我们将原先解题的式子变形 每次除以p与a的最大公约数,直到最大公约数为1或b不能整除为止 代码 #include<iostream> #incl ...
- location.hash的不一样用法
除了可以当做锚点,定位到同name位置,location.hash还有两个用法. 平时开发都会用得到. 一:使js事件在浏览器中产生历史记录. 举个栗子: 我们在JS里面改变了页面的数据.样式等,比如 ...
- JasperReport生命周期3
JasperReports的主要目的是为了在一个简单而灵活的方式创建页面为导向,准备好打印文档.下面的流程图描述了一个典型的工作流程,同时创建报表. 如在图片的生命周期具有以下明显的阶段 设计报表在这 ...
- TZOJ 4267 An Easy Puz(深搜)
描述 Wddpdh find an interesting mini-game in the BBS of WHU, called “An easy PUZ”. It’s a 6 * 6 chess ...