java - 如何在 epub 页面中创建分页?安卓

标签 java android webview epub

如何为从 Epub 填充的 WebView 创建分页

我的MainActivity.java:

public class MainActivity extends ActionBarActivity implements OnClickListener {
    WebView webView;
    Handler hand;
    String data = null;
    LinearLayout books;
    int pagecount = 1;
    Book book;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hand = new Handler();
        webView = new WebView(this);
        books = (LinearLayout) findViewById(R.id.book);
        AssetManager assetManager = getAssets();
        try {
            InputStream epubInputStream = assetManager.open("mybook.epub");
            book = (new EpubReader()).readEpub(epubInputStream);

            Log.i("Log_one", "author(s): " + book.getMetadata().getAuthors());
            Log.i("Log_two", "title: " + book.getTitle());
            Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
            Log.i("Log_three", "Coverimage is " + coverImage.getWidth() + " by " + coverImage.getHeight() + " pixels");
            logTableOfContents(book.getTableOfContents().getTocReferences(), 0);

            Spine spine = book.getSpine();
            List<SpineReference> spineList = spine.getSpineReferences();
            int count = spineList.size();

            Button btnnext = (Button) findViewById(R.id.btnNext);
            Button btnPreviuse = (Button) findViewById(R.id.btnPreviuse);
            btnnext.setOnClickListener(this);
            btnPreviuse.setOnClickListener(this);
        } catch (IOException e) {
            Log.e("Log_four", e.getMessage());
        }

    }

    private void logTableOfContents(List<TOCReference> tocReferences, int depth) {
        if (tocReferences == null) {
            return;
        }
        for (TOCReference tocReference : tocReferences) {
            StringBuilder tocString = new StringBuilder();
            for (int i = 0; i < depth; i++) {
                tocString.append("\t");
            }
            tocString.append(tocReference.getTitle());
            Log.i("Log_five", tocString.toString());
            logTableOfContents(tocReference.getChildren(), depth + 1);

        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnNext) {
            try {
                int i = pagecount + 7;
                webView = new WebView(this);
                books = (LinearLayout) findViewById(R.id.book);
                data = new String(book.getContents().get(i).getData());
                webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);
                books.addView(webView);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (v.getId() == R.id.btnPreviuse) {
            try {
                int i = pagecount - 1;
                webView = new WebView(this);
                books = (LinearLayout) findViewById(R.id.book);
                data = new String(book.getContents().get(i).getData());
                webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);
                books.addView(webView);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

当我点击按钮时,我需要查看另一个页面,但我看不到。 我的activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/book"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/Footer"
        android:layout_width="match_parent"
        android:layout_height="100dip"
        android:layout_weight="1"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/btnNext"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:text="Next Page" />

            <Button
                android:id="@+id/btnPreviuse"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:text="Previuse Page" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

如何在我的页面上创建分页

最佳答案

@Override
public void onClick(View v) {
    if(v.getId() == R.id.btnNext){

                try {

                    books.removeAllViews();
                    int i = pagecount + y;
                    y = i;
                    webView = new WebView(this);
                    books = (LinearLayout) findViewById(R.id.book);
                    data = new String(book.getContents().get(y).getData());
                    webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);
                    books.addView(webView);
                } catch (IOException e) {
                    e.printStackTrace();
                }

    }else if(v.getId() == R.id.btnPreviuse){
                try {
                    books.removeAllViews();
                    int i = y - pagecount;
                    y = i;
                    webView = new WebView(this);
                    books = (LinearLayout) findViewById(R.id.book);
                    data = new String(book.getContents().get(y).getData());
                    webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html", "UTF-8", null);
                    books.addView(webView);
                } catch (IOException e) {
                    e.printStackTrace();
                }
    }

}

关于java - 如何在 epub 页面中创建分页?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25605835/

相关文章:

java - 从 WebView 存储 Facebook 的 session

java - 在 Robotium 中进行单元测试时检查 WebView 的 URL

java - webview 没有在 javafx 中打开弹出窗口

java - Android Studio : Items in RecycleView "jumps" to the left

java - 限制 SQL Server 2005 中的 SELECT 语句

Android:FaceDetector 不工作。 findface 始终检测零面;

javascript - 在锁屏上运行的混合应用程序

java - 在达到特定请求阈值之前,是否由 Spring Boot 中的单个线程处理多个请求?

java - 如何在 JScrollPane 中修改 JPanel 的大小?

android - 将带有解析的推送通知发送到单个设备