您的位置:首页 > 其它

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

2016-04-06 18:08 323 查看
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);
}
}

}


转载于:

/article/3985560.html(assets读txt)

/article/4864175.html(自定义scrollView)

/article/8988848.html(scrollView移动到某一位置)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: