我有一个应用程序,包含几个嵌套在ScrollViews中的TextView,每个TextView包含几千个单词。
我希望用户能够阅读TextView的一部分然后离开屏幕(甚至关闭应用程序)并能够返回到他们离开的地方。
我尝试在TextView的活动中使用onPause()
和onResume()
,但它没有用。
StoryBodyActivity:
public class StoryBodyActivity extends AppCompatActivity {
private TextView storyBodyTextView;
private ScrollView storyBodyScrollView;
private int storyID;
Parcelable state;
int scrollY = 0;
@Override
public void onPause() {
Log.d("pause", "saving listview state @ onPause");
state = storyBodyTextView.onSaveInstanceState();
scrollY = storyBodyScrollView.getScrollY();
super.onPause();
}
@Override
public void onResume() {
storyBodyScrollView.post(new Runnable() {
@Override
public void run() {
storyBodyScrollView.scrollTo(0, scrollY);
}
});
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_story_body, menu);
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story_body);
Bundle extras = getIntent().getExtras();
String story = extras.getString("story");
int storyID = extras.getInt("story_id");
Log.i("stories", Integer.toString(storyID));
setTitle(story);
storyBodyTextView = (TextView) findViewById(R.id.story_body_text_view);
storyBodyScrollView = (ScrollView) findViewById(R.id.story_body_scroll_view);
DatabaseHelper db = new DatabaseHelper(this);
String storyBody = db.getStoryBody(storyID);
storyBodyTextView.setText(Html.fromHtml(storyBody));
if(state != null) {
Log.d("pause", "trying to restore textview state..");
storyBodyTextView.onRestoreInstanceState(state);
}
}
}
DatabaseHelper的一个片段:
public String getStoryBody(int storyID) {
String storyBody = "";
// Select all query
String selectQuery = "SELECT body FROM " + BOOKS + " WHERE id = '" + storyID + "'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
storyBody = cursor.getString(0);
} while (cursor.moveToNext());
}
return storyBody;
}
activity_story_body:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.apple.bookshelf.StoryBodyActivity">
<ScrollView
android:id="@+id/story_body_scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="80dp"
android:textSize="16sp"
android:lineSpacingExtra="5dp"
android:id="@+id/story_body_text_view" />
</ScrollView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
您需要在数据库或共享偏好设置中保存scrollY
并在onCreate()
答案 1 :(得分:0)
onPause
和onResume
仅在应用未被销毁之前。在这里你完全关闭应用程序,所以应用程序是destroyed
。因此,您可以尝试保留shared preferences
中的值,并在onCreate
中设置位置。我希望我的回答很有帮助。