zoukankan      html  css  js  c++  java
  • 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

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

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

     

  • 相关阅读:
    ADO.NET基础学习 二(Command对象)
    如何使ElementUI中的el-dropdown传入多参数
    Vue router Element 重复点击导航路由报错解决方法
    vue-svgicon基本使用
    js 两个相等的数组,修改其中一个怎么做到不改变另外一个
    LocalStorage存储JSON对象
    js取整数、取余数的方法
    JS正则截取两个字符串之间及字符串前后内容的方法
    Vuetify 表单规则验证
    JS正则表达式
  • 原文地址:https://www.cnblogs.com/mrszhou/p/7101246.html
Copyright © 2011-2022 走看看