TextView+ScrollView显示大量文本(小说)
2016-04-06 18:08
323 查看
1.记录ScrollView滑动的位置,用来下次重新进入可以回到这个位置。
必须重写ScrollView,将它的onScrollChanged()暴露出去。
2.初始化的时候要让ScrollView滑动的指定位置,必须使用ScrollView.post(runnable){…}
MainActivity
xml
ScrollViewListener接口用来暴露滑动时的数据(位置)
自定义ScrollView
转载于:
/article/3985560.html(assets读txt)
/article/4864175.html(自定义scrollView)
/article/8988848.html(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); } } }
转载于:
/article/3985560.html(assets读txt)
/article/4864175.html(自定义scrollView)
/article/8988848.html(scrollView移动到某一位置)
相关文章推荐
- 基于Http协议的Android网络编程
- OPRNGL之渲染过程大概梳理
- php 使用GD库压缩图片,添加文字图片水印
- 串口初始化
- /proc/sysrq-trigger文件的功能
- Android酷炫实用的开源框架(UI框架)
- webservice开发
- GDB 反向调试(Reverse Debugging)
- 问题1:javascript的函数对象问题
- mybatis generator maven-plugin 集成
- numpy 学习,里面有converters参数的使用方法
- IP格式合法问题研究
- mysql两大存储引擎的区别与选择
- Qt Quick实现的涂鸦程序
- LUA IO库
- 使用word模板生成pdf文件
- python为什么有私有方法和变量
- Android 发送广播更加方便快捷高效的方式
- 数据库索引
- In App Purchase(应用内购)