gpt4 book ai didi

java - 当我更改页面方向时出现在页面开头

转载 作者:行者123 更新时间:2023-12-02 13:02:08 25 4
gpt4 key购买 nike

我正在使用barteksc-AndroidPdfViewer 。我正在使用这段代码

package com.github.barteksc.sample;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.OpenableColumns;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;
import com.shockwave.pdfium.PdfDocument;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.NonConfigurationInstance;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;

import java.util.List;

@EActivity(R.layout.activity_main)
@OptionsMenu(R.menu.options)
public class PDFViewActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener {

private static final String TAG = PDFViewActivity.class.getSimpleName();

private final static int REQUEST_CODE = 42;
public static final int PERMISSION_CODE = 42042;

public static final String SAMPLE_FILE = "sample.pdf";
public static final String READ_EXTERNAL_STORAGE = "android.permission.READ_EXTERNAL_STORAGE";

@ViewById
PDFView pdfView;

@NonConfigurationInstance
Uri uri;

@NonConfigurationInstance
Integer pageNumber = 0;

String pdfFileName;

@OptionsItem(R.id.pickFile)
void pickFile() {
int permissionCheck = ContextCompat.checkSelfPermission(this,
READ_EXTERNAL_STORAGE);

if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{READ_EXTERNAL_STORAGE},
PERMISSION_CODE
);

return;
}

launchPicker();
}

void launchPicker() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
//alert user that file manager not working
Toast.makeText(this, R.string.toast_pick_file_error, Toast.LENGTH_SHORT).show();
}
}

@AfterViews
void afterViews() {
if (uri != null) {
displayFromUri(uri);
} else {
displayFromAsset(SAMPLE_FILE);
}
setTitle(pdfFileName);
}

private void displayFromAsset(String assetFileName) {
pdfFileName = assetFileName;

pdfView.fromAsset(SAMPLE_FILE)
.defaultPage(pageNumber)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.load();
}

private void displayFromUri(Uri uri) {
pdfFileName = getFileName(uri);

pdfView.fromUri(uri)
.defaultPage(pageNumber)
.onPageChange(this)
.enableAnnotationRendering(true)
.onLoad(this)
.scrollHandle(new DefaultScrollHandle(this))
.load();
}

@OnActivityResult(REQUEST_CODE)
public void onResult(int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
uri = intent.getData();
displayFromUri(uri);
}
}

@Override
public void onPageChanged(int page, int pageCount) {
pageNumber = page;
setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
}

public String getFileName(Uri uri) {
String result = null;
if (uri.getScheme().equals("content")) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
if (result == null) {
result = uri.getLastPathSegment();
}
return result;
}

@Override
public void loadComplete(int nbPages) {
PdfDocument.Meta meta = pdfView.getDocumentMeta();
Log.e(TAG, "title = " + meta.getTitle());
Log.e(TAG, "author = " + meta.getAuthor());
Log.e(TAG, "subject = " + meta.getSubject());
Log.e(TAG, "keywords = " + meta.getKeywords());
Log.e(TAG, "creator = " + meta.getCreator());
Log.e(TAG, "producer = " + meta.getProducer());
Log.e(TAG, "creationDate = " + meta.getCreationDate());
Log.e(TAG, "modDate = " + meta.getModDate());

printBookmarksTree(pdfView.getTableOfContents(), "-");

}

public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
for (PdfDocument.Bookmark b : tree) {

Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

if (b.hasChildren()) {
printBookmarksTree(b.getChildren(), sep + "-");
}
}
}

/**
* Listener for response to user permission request
*
* @param requestCode Check that permission request code matches
* @param permissions Permissions that requested
* @param grantResults Whether permissions granted
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
@NonNull int[] grantResults) {
if (requestCode == PERMISSION_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
launchPicker();
}
}
}

}

更改页面方向。但是,如果我必须假设当我们开始横向时,纵向模式和页码 15 是否会改变页面的页面方向。

enter image description here enter image description here

请帮我解决这个问题。

最佳答案

当您更改方向时,Android 将默认重新加载 Activity,这就是您丢失当前页面的原因。

有 2 种解决方案,一种是最佳实践,另一种可能有效,具体取决于具体情况。

解决方案1:

onSaveInstanceState 上保存最后一个已知页面,然后在 onCreate 上恢复该页面

protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("current_page", pageNumber);
}

public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null){
pageNumber = savedInstanceState.getInt("current_page");
}
}

解决方案2:

在 list 文件中设置 android:configChanges="orientation|screenSize" 属性。这将保留屏幕的状态,就像方向更改之前一样。

另请阅读 Activity 生命周期:https://developer.android.com/guide/components/activities/activity-lifecycle.html

关于java - 当我更改页面方向时出现在页面开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44245373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com