• ScrollView控件实现屏幕滚动


    滚动视图是指当拥有很多内容,屏幕显示不完全时,需要通过滚动来显示完整的视图

    ScrollView的种类:

    (1)水平滚动视图:HorizontalScrollView

    (2)垂直滚动视图:ScrollView(我们默认的就是垂直滚动)

    下面我们先来一个简单的例子(在文字多的屏幕无法显示的时候,把TextView控件嵌套在ScrollView里面实现滚动视图的效果):

    <ScrollView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
             <TextView 
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="在文字多的屏幕无法显示的时候,把TextView控件嵌套在ScrollView里面实现滚动视图的效果"
                 />
            
        </ScrollView>

    隐藏ScrollView

    (1) 标签属性:android:scrollbars=none

    (2) 代码设置:

    setHorizontalScrollBarEnabled(false);隐藏横向ScrollView

    setVerticalScrollBarEnabled(false);隐藏纵向ScrollView

    setOnTouchListener的使用

    判断ScrollView何时滑动到底部

    public class MainActivity extends Activity {
    private TextView text;
    private ScrollView scroll;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.text);

    scroll = (ScrollView) findViewById(R.id.scroll);
    scroll.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    switch (event.getAction()) {
    case MotionEvent.ACTION_UP:

    break;


    case MotionEvent.ACTION_DOWN:

    break;
    case MotionEvent.ACTION_MOVE:
    /*
    * (1)getScrollY()==滚动条滑动的距离
    * (2)getMeasuredHeight()
    * (3)getHeight()
    * */
    //顶部状态
    if (scroll.getScrollY() <= 0) {
    Log.i(main, 已经到到了顶部);
    }else if (scroll.getChildAt(0).getMeasuredHeight() <= scroll.getHeight() + scroll.getScrollY()) {
    Log.i(main, 已经到了底部);

    }
    break;
    }
    return false;
    }
    });

    }

    }

    那么我们还可以在文字滑动到底部的时候,继续加载文字,我们只需要加这样一条代码就可以了:

    text.append(getResources().getString(R.string.content));

    那么我们还可以设定滚动的位置:

    我们需要在布局中添加两个按钮向上和向下,

    然后在java代码中添加点击事件在点击事件中加入这样的两个方法:

    scroll.scrollBy(0, -30);

    scroll.scrollBy(0, 30);

    后面的那个数值为正,则向下滚动,数值为负,则向上滚动

  • 相关阅读:
    .net 运行中出现的错误解决方法记录
    SVC 工作过程中出现的错误记录(SEO项目)
    TreeCollection2
    晴天前100页评论标签云分析显示
    python numpy数组中的复制问题
    Task多线程进行多进程
    python list(列表)和tuple(元组)
    并发无锁队列学习(概念介绍)
    关联型容器
    【原创】k8s源代码分析-----EndpointController
  • 原文地址:https://www.cnblogs.com/896240130Master/p/6164620.html
Copyright © 2020-2023  润新知