保存书籍的最后一页并在书籍打开时加载?

时间:2017-12-28 01:43:08

标签: android android-viewpager adapter

我开发的应用程序将包含一些书籍,我不知道如何保存书籍的最后一页,并在书籍打开时打开,即使应用程序已关闭。我不会使用片段,只需CustomSwipeAdapterImageViewTouchViewPagerViewPager

一切正常,我只需要这个,实际上在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);
                    }
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

由于应用程序已关闭并再次打开,因此您需要将页码保存在持久存储中。由于它只是一个数字,您可以使用SharedPreference。

所以在SharedPreference中有一个键,例如" pageNumber"。每次加载新页面时,请将页码保存到" pageNumber"。这样,您就不必担心在应用关闭之前保存最后一个页码。

再次打开应用时,请检查" pageNumber"并加载。

答案 1 :(得分:0)

您可以参考:

  • 在关闭应用
  • 时,使用共享首选项保存图书的最后一页
  • 当您打开应用时,您可以获取最后一页并设置最后一页的位置:
  

viewPager.setCurrentItem(/ ** lastpage get from Shared Preferences * /);

我希望它可以帮助你解决问题!