保存书籍的最后一页并在书籍打开时加载?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了保存书籍的最后一页并在书籍打开时加载?相关的知识,希望对你有一定的参考价值。
我正在开发一个会有一些书籍的应用程序,我不知道如何保存书籍的最后一页,并在书籍打开时打开,即使应用程序已关闭。我不使用片段,只使用CustomSwipeAdapter
,ImageViewTouchViewPager
和ViewPager
。
一切正常,我只需要这个,实际上在MainActivity
我可以保存当前位置但我不知道如何设置保存设置当书打开以显示应用程序关闭之前的最后一页。
谢谢。
public class CustomSwipeAdapter1 extends PagerAdapter{
private int[] pagesBookOne = {R.drawable.pageone, R.drawable.pagetwo, R.drawable.pagethree};
private Context context;
private LayoutInflater layoutInflater;
private ViewPager viewPager;
public CustomSwipeAdapter1(Context context){
this.context = context;
}
@Override
public int getCount() {
return paginasLivroUm.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == (LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout, container, false);
ImageViewTouch imageViewTouch = item_view.findViewById(R.id.imageviewtouch_xml);
imageViewTouch.setImageResource(paginasLivroUm[position]);
imageViewTouch.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
//super.destroyItem(container, position, object);
}
}
==========================================================
import android.os.PersistableBundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class LivroUmActivity extends AppCompatActivity {
private ViewPager viewPager1;
private CustomSwipeAdapter1 adapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_livro_um);
viewPager1 = findViewById(R.id.viewpager1_xml);
adapter1 = new CustomSwipeAdapter1(this);
viewPager1.setAdapter(adapter1);
}
@Override
protected void onDestroy() {
positionSaved = viewPager1.getCurrentItem();
//Here I can save the position, just don't know how to set on the viewpager.
super.onDestroy();
}
}
==========================================================
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import it.sephiroth.android.library.imagezoom.ImageViewTouch;
import it.sephiroth.android.library.imagezoom.ImageViewTouchBase;
public class ImageViewTouchViewPager extends ViewPager{
private static final String TAG = "ImageViewTouchViewPager";
public static final String VIEW_PAGER_OBJECT_TAG = "image#";
private int previousPosition;
private OnPageSelectedListener onPageSelectedListener;
public ImageViewTouchViewPager(Context context) {
super(context);
init();
}
public ImageViewTouchViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setOnPageSelectedListener(OnPageSelectedListener listener) {
onPageSelectedListener = listener;
}
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ImageViewTouch) {
return ((ImageViewTouch) v).canScroll(dx);
} else {
return super.canScroll(v, checkV, dx, x, y);
}
}
public interface OnPageSelectedListener {
public void onPageSelected(int position);
}
private void init() {
previousPosition = getCurrentItem();
setOnPageChangeListener(new SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (onPageSelectedListener != null) {
onPageSelectedListener.onPageSelected(position);
}
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == SCROLL_STATE_SETTLING && previousPosition != getCurrentItem()) {
try {
ImageViewTouch imageViewTouch = (ImageViewTouch)
findViewWithTag(VIEW_PAGER_OBJECT_TAG + getCurrentItem());
if (imageViewTouch != null) {
imageViewTouch.zoomTo(1f, 300);
}
previousPosition = getCurrentItem();
} catch (ClassCastException ex) {
Log.e(TAG, "This view pager should have only ImageViewTouch as a children.", ex);
}
}
}
});
}
}
答案
由于应用程序已关闭并再次打开,因此您需要将页码保存在持久存储中。因为它只是一个数字,您可以使用SharedPreference。
所以在SharedPreference中有一个像“pageNumber”的密钥。每次加载新页面时,将页码保存到“pageNumber”。这样,您无需担心在应用关闭之前保存最后一个页码。
当应用程序再次打开时,检查“pageNumber”并加载它。
另一答案
你可以参考:
- 关闭应用程序时,使用“共享首选项”保存书籍的最后一页
- 当您打开应用程序时,您可以获得最后一页并设置最后一页的位置:
viewPager.setCurrentItem(/ ** lastpage get from Shared Preferences * /);
我希望它可以帮助你解决问题!
以上是关于保存书籍的最后一页并在书籍打开时加载?的主要内容,如果未能解决你的问题,请参考以下文章
Flutter应用打开时如何从sqlite数据库加载初始状态数据
R - 当用户在不同的页面上时,闪亮的数据表(renderDataTable)重新加载到第一页并更新某个列值(selectInput)