使用ScrollView实现滚动效果
16lz
2021-01-26
滚动视图是指当拥有很多内容,屏幕显示不完时,需要通过滚动来显示完整的视图
ScrollView的种类
HorizontalScrollView:水平滚动视图
ScrollView:垂直滚动视图
<?xml version="1.0" encoding="utf-8"?>
隐藏ScrollView
标签属性:android:scrollbars="none" 隐藏滚动条
代码设置:
setHorizontalScrollBarEnabled(false); 隐藏横向ScrollView
setVerticalScrollBarEnable(flase); 隐藏纵向ScrollView
水平滚动视图:直接将标签改为
setOnTouchListener的使用
判断ScrollView何时滑动到底部
public class MainActivity extends AppCompatActivity { private TextView tv; private ScrollView scroll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.content); tv.setText(getResources().getString(R.string.content)); scroll = (ScrollView) findViewById(R.id.scroll); scroll.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) {// case MotionEvent.ACTION_UP:// break;// case MotionEvent.ACTION_DOWN:// break; case MotionEvent.ACTION_MOVE: /** * 1.getScrollY()----滚动条滑动的距离(从0开始计算) * 2.getMeasuredHeight() (整个的高度) * 3.getHeight() (一屏的高度) */ //顶部状态 if (scroll.getScrollY() <= 0) { Log.i("Main", "滑动到顶部"); } //底部状态 // TextView的总高度 <= 一屏幕的高度 + 滚动条的滚动距离 if (scroll.getChildAt(0).getMeasuredHeight() <= scroll.getHeight() + scroll.getScrollY()) { Log.i("Main", "滑动到底部"); Log.i("Main", "scroll.getChildAt(0).getMeasuredHeight() = " + scroll.getChildAt(0).getMeasuredHeight() + "scroll.getHeight() = " + scroll.getHeight() + "scroll.getScrollY() = " + scroll.getScrollY()); tv.append(getResources().getString(R.string.content)); } break; } return false; } }); }}
scrollTo和scrollBy区别
public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y); }
控制ScrollView视图的位置
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView tv; private ScrollView scroll; private Button up_btn; private Button down_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.content); up_btn = (Button) findViewById(R.id.up); down_btn = (Button) findViewById(R.id.down); up_btn.setOnClickListener(this); down_btn.setOnClickListener(this); tv.setText(getResources().getString(R.string.content)); scroll = (ScrollView) findViewById(R.id.scroll); scroll.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) {// case MotionEvent.ACTION_UP:// break;// case MotionEvent.ACTION_DOWN:// break; case MotionEvent.ACTION_MOVE: /** * 1.getScrollY()----滚动条滑动的距离(从0开始计算) * 2.getMeasuredHeight() (整个的高度) * 3.getHeight() (一屏的高度) */ //顶部状态 if (scroll.getScrollY() <= 0) { Log.i("Main", "滑动到顶部"); } //底部状态 // TextView的总高度 <= 一屏幕的高度 + 滚动条的滚动距离 if (scroll.getChildAt(0).getMeasuredHeight() <= scroll.getHeight() + scroll.getScrollY()) { Log.i("Main", "滑动到底部"); Log.i("Main", "scroll.getChildAt(0).getMeasuredHeight() = " + scroll.getChildAt(0).getMeasuredHeight() + "scroll.getHeight() = " + scroll.getHeight() + "scroll.getScrollY() = " + scroll.getScrollY()); tv.append(getResources().getString(R.string.content)); } break; } return false; } }); } @Override public void onClick(View v) { switch (v.getId()) { /** * scrollTo:以滚动视图起始位置开始计算的(所以只有第一次的点击是有效的) * scrollBy:相对前一次的位置,去滚动对应的距离 */ case R.id.up: scroll.scrollBy(0, -30); break; case R.id.down: scroll.scrollBy(0, 30); break; } }}
https://github.com/LiuchangDuan/demo
更多相关文章
- 2014.04.22 ——— android listview header和左右滑动冲突
- Android(安卓)Activity生命周期举例说明(图文)
- UltimateRecyclerView发布,Android下新Listview的大杀器
- Android(安卓)仿微信对话列表滑动删除效果
- Android(安卓)新控件学习
- android ListView的item侧滑删除
- Android(安卓)Studio 的抽屉导航
- android ViewFilpper(二)
- Android(安卓)SeekBar控件