일부 책이 포함될 앱을 개발 중이며 책이 열렸을 때 마지막 페이지를 저장하고 책을 열 때 열리는 방법을 알지 못합니다. 앱이 닫힌 경우에도 마찬가지입니다. 조각을 사용하지 않고 단지 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);
}
}
}
});
}
}
대단히 감사합니다. 아주 잘 설명했다. – djalmafreestyler
@djalmafreestyler 도와 줘서 기뻐요. 이 방법으로 문제가 해결되면 친절하게 답변을받습니다. – Henry
어댑터 또는 MainActivity에서 SharedPreferences는 어디에서 사용합니까? – djalmafreestyler