码迷,mamicode.com
首页 > 其他好文 > 详细

ScrollView实现文本分批加载小构思

时间:2015-08-09 10:53:26      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:scrollview   setontouchlistener   scrollto   scrollby   getscrolly   

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UP" />

    <Button
        android:id="@+id/down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DOWN" />

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

</LinearLayout>


MainActivity

package com.imooc.android_scrollview;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
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.os.Build;

public class MainActivity extends Activity implements 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);
		tv.setText(getResources().getString(R.string.content));

		up_btn = (Button) findViewById(R.id.up);
		down_btn = (Button) findViewById(R.id.down);
		up_btn.setOnClickListener(this);
		down_btn.setOnClickListener(this);

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

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_MOVE: {
					/**
					 * (1)getScrollY()————获取滚动条滑动的距离
					 * (2)getMeasuredHeight()包含隐藏的textview的高度
					 * (3)getHeight()显示在一屏幕的高度
					 */

					// 顶部状态
					if (scroll.getScrollY() <= 0) {
						Log.i("Main", "滑动到顶部");
					}

					// 底部状态
					// TextView的总高度<=一屏幕的高度+滚动条的滚动距离
					if (scroll.getChildAt(0).getMeasuredHeight() <= scroll
							.getHeight() + 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: {
			//定位到textview开始处
			scroll.scrollTo(0, 0);
			break;
		}

		case R.id.down: {

			scroll.scrollBy(0, 100);
			break;
		}
		}
	}
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

ScrollView实现文本分批加载小构思

标签:scrollview   setontouchlistener   scrollto   scrollby   getscrolly   

原文地址:http://blog.csdn.net/u013210620/article/details/47373195

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!