Android---ScrollView实现页面滚动效果

来源:互联网 发布:淘宝家装设计师平台 编辑:程序博客网 时间:2024/06/04 18:47

ScrollView实现页面滚动效果

直接上代码
layout布局中的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.scrollview.MainActivity" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/down"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="∨" />        <Button            android:id="@+id/up"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="∧" />    </LinearLayout>    <!--         android:scrollbars="none" 设置隐藏滚动条         代码中设置隐藏滚动条:    setVerticalScrollBarEnabled(false);隐藏横向    setHorizontalScrollBarEnabled(false);隐藏纵向-->    <!-- ScrollView:垂直滚动    HorizontalScrollView:横向滚动 -->    <ScrollView        android:id="@+id/scorll"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scrollbars="none" >        <!--         1、android:lineSpacingExtra 设置行间距,如”8dp”。2、android:lineSpacingMultiplier 设置行间距的倍数,如”1.5″。         -->        <TextView            android:layout_margin="10dp"            android:lineSpacingMultiplier="1.5"            android:id="@+id/content"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:textSize="18sp" />    </ScrollView></LinearLayout>
Java文件中的代码:
package com.example.scrollview;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.ScrollView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{private TextView tv;private ScrollView scroll;private Button down,up;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);down=(Button) findViewById(R.id.down);up=(Button) findViewById(R.id.up);down.setOnClickListener(this);up.setOnClickListener(this);tv = (TextView) findViewById(R.id.content);tv.setText(getResources().getString(R.string.content));scroll = (ScrollView) findViewById(R.id.scorll);scroll.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (event.getAction()) {/** 手指抬起 */case MotionEvent.ACTION_UP:break;/* * 手指落下*/case MotionEvent.ACTION_DOWN:break;/* * 手指滑动*/case MotionEvent.ACTION_MOVE:/** * 1.getScrollY()---滚动条滑动的距离 * 2.getMeasuredHeight()---显示视图的全部高度(手机显示+隐藏掉的) * 3.getHeight()---手机显示视图的高度 */// 顶部状态if (scroll.getScaleY() <= 0) {Toast.makeText(MainActivity.this, "现在在顶部", Toast.LENGTH_SHORT).show();}/** 底部状态: TextView的总高度 <= 一个屏幕的高度+滚动条的滚动距离 */if (scroll.getChildAt(0).getMeasuredHeight() <= scroll.getHeight() + scroll.getScaleY()) {Toast.makeText(MainActivity.this, "现在在底部", Toast.LENGTH_SHORT).show();Log.e("Main", "TextView的总高度(scroll.getChildAt(0).getMeasuredHeight()):"+ scroll.getChildAt(0).getMeasuredHeight() + "\n" + " 一个屏幕的高度(scroll.getHeight())"+ scroll.getHeight() + "\n" + "滚动条的滚动距离(scroll.getScaleY())" + scroll.getScaleY());tv.append(getResources().getString(R.string.content));//在底部重新追加文本/** * 可以在这里设置刷新和重新加载页面的操作 */}break;default:break;}return false;     }});}//@Override//public boolean onTouchEvent(MotionEvent event) {//// TODO Auto-generated method stub//return super.onTouchEvent(event);//}/** * 按钮控制页面滑动 */@Overridepublic void onClick(View v) {// TODO Auto-generated method stub/** * x:横坐标   y:纵坐标    * ScrollView为纵向的,所以x轴可设置为0 * scrollTo(x, y):以滚动视图起始位置开始计算的,即(0,0)开始,以起始位置为参照点 * scrollBy(x, y):相对前一次的位置,去滚动对应的距离,以上一次滚动的位置为参照点 */switch (v.getId()) {case R.id.up:scroll.scrollBy(0, -30);//scroll.scrollTo(0, -30);break;case R.id.down:scroll.scrollBy(0, 30);//scroll.scrollTo(0, 30);break;default:break;}}}




阅读全文
0 0
原创粉丝点击