如何在我的页面中为epub创建分页?安卓

时间:2014-09-01 12:26:20

标签: java android webview epub

如何为paging填充的WebView创建Epub

我的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();
            }
        }
    }
}

当我在按钮上click时,我需要看到另一个page,但我看不到。 我的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>

如何在我的网页上创建paging

1 个答案:

答案 0 :(得分:0)

@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();
                }
    }

}