安卓开发--scrollview

package com.cnn.scrollviewdemo01;

import android.R.integer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
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;


@SuppressLint("ClickableViewAccessibility") public class MainActivity extends Activity {
	private TextView textView;
	private ScrollView scrollView;
	private Button btnUp,btnDown;
	int scrollY=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView= (TextView) findViewById(R.id.textView1);
        textView.setText(getResources().getString(R.string.china_news));
        
        scrollView=(ScrollView) findViewById(R.id.scrollView1);
        scrollView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO 自动生成的方法存根
				switch (event.getAction()) {
					case MotionEvent.ACTION_UP://手指抬起
						
						break;
	
					case MotionEvent.ACTION_DOWN://手指落下
						
						break;
					case MotionEvent.ACTION_MOVE://手指滑动
						if (scrollView.getScaleY()<=0) {//顶部
							Log.i("tag", "没有滑动");
						}
						//textView总高度<=一屏幕的高度+滚动条的滚动距离
						if (scrollView.getChildAt(0).getMeasuredHeight()<=
								scrollView.getHeight()+scrollView.getScrollY()) {
							Log.i("tag", "没有到底部");
							textView.append("123111111111111111111111111111111111111111111111111111111");//(getResources().getString(R.string.china_news));
						}
						break;
				}
				return false;
			}
		});

        btnUp=(Button) findViewById(R.id.button2);
        btnDown=(Button) findViewById(R.id.button1);
        
        btnDown.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				//scrollY=scrollY+10;
				//scrollView.setScrollY(scrollY);
				scrollView.scrollBy(0, 20);
			}
		});
        
        btnUp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO 自动生成的方法存根
				//scrollY=scrollY-10;
				//scrollView.setScrollY(scrollY);
				scrollView.scrollBy(0, -20);
			}
		});
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Up" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Donw" />

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

        </LinearLayout>
    </ScrollView>

</LinearLayout>

  

原文地址:https://www.cnblogs.com/zxcnn/p/5071378.html