2014-09-01 75 views
0

我怎樣才能爲我的WebView創建paging,從Epub填充。如何在我的epub頁面中創建分頁? android

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

回答

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

}