ScrollView实现滚动效果
标签(空格分隔): ScrollView Android 滚动效果
ScrollView实现滚动效果 使用ScrollView实现滚动条 监听ScrollView何时滑动到底部 控制ScrollView视图位置
滚动视图是指当拥有很多内容,屏幕显示不全时,需要通过滚动来显示完整的视图。
-
使用ScrollView实现滚动条
ScrollView的种类:
HorizontalScrollView:水平滚动视图
ScrollView:垂直滚动视图
activity_scroll_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="none"> <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content"/> </ScrollView> </LinearLayout>
隐藏ScrollView
1. 标签属性:
android:scrollbars="none"
代码设置:
setHorizontalScrollBarEnabled(false); 隐藏横向ScrollView setVerticalScrollBarEnabled(false); 隐藏纵向ScrollView
- 监听ScrollView何时滑动到底部
setOnTouchListener()
ScrollViewActivity.java
package com.excavator.scrollviewdemo; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.ScrollView; import android.widget.TextView; public class ScrollViewActivity extends Activity implements View.OnClickListener { private TextView mTextView; private ScrollView mScrollView; private Button up_btn; private Button down_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scroll_view); mTextView = (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); mTextView.setText(getResources().getString(R.string.content)); mScrollView = (ScrollView) findViewById(R.id.scrollView); mScrollView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: /** * 1.getScrollY()---滚动条滑动的距离 * 2.getMeasuredHeight() * 3.getHeight() */ //顶部状态 if (mScrollView.getScrollY()<=0) { Log.i("Main","滑动到顶部"); } //底部状态 //TextView的总高度<=一屏幕的高度+滚动条滚动的距离 if (mScrollView.getChildAt(0).getMeasuredHeight() <= mScrollView.getHeight() + mScrollView.getScrollY()) { Log.i("Main","滑动到底部"); Log.i("Main", "mScrollView.getChildAt(0).getMeasuredHeight()=" + mScrollView.getChildAt(0).getMeasuredHeight() + ",mScrollView.getHeight()=" + mScrollView.getHeight() + ",mScrollView.getScrollY()=" + mScrollView.getScrollY()); mTextView.append(getResources().getString(R.string.content)); } break; } return false; } }); } @Override public void onClick(View v) { switch (v.getId()) { /** * scrollTo:以滚动视图起始位置开始计算 * scrollBy:相对于前一个位置,去滚动对应的距离 */ case R.id.up: mScrollView.scrollBy(0, -30); break; case R.id.down: mScrollView.scrollBy(0, 30); break; } } }
- 控制ScrollView视图位置
scrollTo和scrollBy区别
@Override public void onClick(View v) { switch (v.getId()) { /** * scrollTo:以滚动视图起始位置开始计算 * scrollBy:相对于前一个位置,去滚动对应的距离 */ case R.id.up: mScrollView.scrollBy(0, -30); break; case R.id.down: mScrollView.scrollBy(0, 30); break; } }
<p>版权声明:本文为博主原创文章,未经博主允许不得转载。</p>