TextView+ScrollView显示大量文本(小说)

1.记录ScrollView滑动的位置,用来下次重新进入可以回到这个位置。
必须重写ScrollView,将它的onScrollChanged()暴露出去。

2.初始化的时候要让ScrollView滑动的指定位置,必须使用ScrollView.post(runnable){…}


MainActivity

package com.example.testclearedittext;

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity implements ScrollViewListener{

    private ObservableScrollView mScrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mScrollView = (ObservableScrollView) findViewById(R.id.scroll);
        mScrollView.setScrollViewListener(this);

        TextView tv = (TextView) findViewById(R.id.tv);

        try {
        //  拿到文件的输入流
        InputStream in = getAssets().open("santi.txt");
        //  获得内容的长度
        int size = in.available();

        byte[] buffer = new byte[size];
        //  把内存从inputstream内读取到数组上
        in.read(buffer);
        in.close();

        //  把内容复制给String
        String content = new String(buffer,"GB2312");
        tv.setText(content);

        //  初始化设计滑动距离只能这样写
        //  mScrollView.smoothScrollTo()或mScrollView.mScrollView.scrollTo()均无效!
        //  根据记录的y来回到上次离开的地方
        mScrollView.post(new Runnable() {

            @Override
            public void run() {
                //  500为模拟值,实际上可以从轻量级数据库orSQLITE获取上次记录的值
                mScrollView.smoothScrollTo(0, 500);
            }
        });

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
            int oldx, int oldy) {
        //  每次滑动记录y
        //  使用SharedPreferences或者SQLITE
    }

}

xml

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

   <com.example.testclearedittext.ObservableScrollView 
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/scroll">

       <TextView 
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:id="@+id/tv"
           />

   </com.example.testclearedittext.ObservableScrollView >

</LinearLayout>

ScrollViewListener接口用来暴露滑动时的数据(位置)

package com.example.testclearedittext;

public interface ScrollViewListener {  

    void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);  

}  

自定义ScrollView

package com.example.testclearedittext;

import android.content.Context;  
import android.util.AttributeSet;  
import android.widget.ScrollView;  

public class ObservableScrollView extends ScrollView {  

    private ScrollViewListener scrollViewListener = null;  

    public ObservableScrollView(Context context) {  
        super(context);  
    }  

    public ObservableScrollView(Context context, AttributeSet attrs,  
            int defStyle) {  
        super(context, attrs, defStyle);  
    }  

    public ObservableScrollView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }  

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {  
        this.scrollViewListener = scrollViewListener;  
    }  

    @Override  
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {  
        super.onScrollChanged(x, y, oldx, oldy);  
        if (scrollViewListener != null) {  
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);  
        }  
    }  

}  

转载于:
http://edison-cool911.iteye.com/blog/695145(assets读txt)
http://www.cnblogs.com/android100/p/Android-ScrollView.html(自定义scrollView)
http://blog.csdn.net/lin49940/article/details/6278592(scrollView移动到某一位置)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值