In the past, ireader has also achieved the effect of bookshelves, but it is not easy to use using listview. Most of them are implemented using the gridview, and there is little information on the Internet. Some open-source e-books focus on reading, and there is no such shelf effect as ireader and QQ reading.
I have achieved this kind of effect for a long time. I originally wanted to make a perfect e-book, but because of my laziness, it only lasted for one or two days. Today I found anotherCodeI hope you can achieve a perfect open-source ebook together. Let's talk about the effect first:
The local part has not been done yet. After you have done a good job, you can load the local books into the bookshelf. This is just the beginning, and there are still a lot of complicated things to be done later.
Let's take a look at the implementation principles of the bookshelf!
First, let's take a look at the layout file main. XML in layout.
<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayoutxmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <include layout = "@ layout/head" Android: Id = "@ + ID/head"/> <cn.com. karl. view. mygridview Android: Id = "@ + ID/bookshelf" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: layout_below = "@ ID/head" Android: cachecolorhint = "#00000000" Android: columnwidth = "90.0dip" Android: fadingedge = "NONE" Android: horizontalspacing = "5dp" Android: listselector = "#00000000" Android: numcolumns = "3" Android: scrollbars = "NONE" Android: verticalspacing = "20dp"/> <slidingdrawer Android: Id = "@ + ID/sliding" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: content = "@ + ID/allapps" Android: handle = "@ + ID/imageviewicon" Android: orientation = "vertical"> <button Android: Id = "@ + ID/imageviewicon" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "local" Android: textsize = "18dp" Android: Background = "@ drawable/btn_local"/> <gridview Android: Id = "@ + ID/allapps" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Background = "@ drawable/file_list_bg" Android: columnwidth = "60dp" Android: gravity = "center" Android: horizontalspacing = "10dp" Android: numcolumns = "auto_fit" Android: padding = "10dp" Android: stretchmode = "columnwidth" Android: verticalspacing = "10dp"/> </sshortingdrawer> </relativelayout>
The above is a custom gridview mainly to implement the bookshelf, because each book is an item, calculate the height of each row in the Custom gridview, and then draw the bookshelf. Below is a drawer.
Public class mygridview extends gridview {private bitmap background; Public mygridview (context, attributeset attrs) {super (context, attrs); background = bitmapfactory. decoderesource (getresources (), R. drawable. bookshelf_layer_center) ;}@ overrideprotected void dispatchdraw (canvas) {int COUNT = getchildcount (); int Top = count> 0? Getchildat (0 ). gettop (): 0; int backgroundwidth = background. getwidth (); int backgroundheight = background. getheight () + 2; int width = getwidth (); int Height = getheight (); For (INT y = top; y
The above is the gridview of the custom bookshelf, which is also the core method to implement the bookshelf.
Then there is the layout of each item:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: gravity = "center" Android: orientation = "vertical"> <textview Android: layout_height = "110dp" Android: layout_width = "90dp" Android: layout_margintop = "10dp" Android: background = "@ drawable/cover_txt" Android: Id = "@ + ID/imageview1" Android: text = "tianlong Babu" Android: padding = "15dp" Android: textcolor = "#000000"/> </linearlayout>
Finally, it can be displayed in the main activity.
Public class bookshelfacti.pdf extends baseactivity {private gridview bookshelf; private int [] DATA = {R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. d Rawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. drawable. cover_txt, R. dr Awable. cover_txt}; private string [] Name = {"tianlong Babu", "soushenji", "Water Margin", "blackway grief"}; private gridview GV; private slidingdrawer SD; private button IV; private list <resolveinfo> Apps;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); this. requestwindowfeature (window. feature_no_title); setcontentview (R. layout. main); B Ookshelf = (gridview) findviewbyid (R. id. bookshelf); shlefadapter adapter = new shlefadapter (); bookshelf. setadapter (adapter); bookshelf. setonitemclicklistener (New onitemclicklistener () {@ overridepublic void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stubif (arg2> = data. length) {} else {toast. maketext (getapplicationcontext (), "" + arg2, toast. length_short ). show () ;}}); loadapps (); GV = (gridview) findviewbyid (R. id. allapps); SD = (slidingdrawer) findviewbyid (R. id. sliding); IV = (button) findviewbyid (R. id. imageviewicon); GV. setadapter (New gridadapter (); SD. setondraweropenlistener (New Slidingdrawer. ondraweropenlistener () // open the drawer {@ override public void ondraweropened () {IV. settext ("return"); IV. setbackgroundresource (R. drawable. btn_local); // respond to the drawer opening event //, set the image to the bottom}); SD. setondrawercloselistener (New s0000ingdrawer. ondrawercloselistener () {@ override public void ondrawerclosed () {IV. settext ("local"); IV. setbackgroundresource (R. drawable. btn_local); // response to close drawer event});} class shlefadapter ex Tends baseadapter {@ overridepublic int getcount () {// todo auto-generated method stubreturn data. length + 5 ;}@ overridepublic object getitem (INT arg0) {// todo auto-generated method stubreturn arg0 ;}@ overridepublic long getitemid (INT arg0) {// todo auto-generated method stubreturn arg0;} @ overridepublic view getview (INT position, view contentview, viewgroup arg2) {// todo auto-generated method stubcont Entview = layoutinflater. from (getapplicationcontext ()). inflate (R. layout. item1, null); textview view = (textview) contentview. findviewbyid (R. id. imageview1); If (data. length> position) {If (position <name. length) {view. settext (name [position]);} view. setbackgroundresource (data [position]);} else {view. setbackgroundresource (data [0]); view. setclickable (false); view. setvisibility (view. invisible) ;}return contentview ;}} @ Overridepublic Boolean onkeydown (INT keycode, keyevent event) {// todo auto-generated method stubif (keycode = keyevent. keycode_back) {alertdialog. builder = new alertdialog. builder (this); builder. setmessage ("are you sure you want to quit? "). Setcancelable (false ). setpositivebutton ("OK", new dialoginterface. onclicklistener () {public void onclick (dialoginterface Diener, int ID) {finish ();}}). setnegativebutton ("return", new dialoginterface. onclicklistener () {public void onclick (dialoginterface dialog, int ID) {dialog. cancel () ;}}); alertdialog Alert = builder. create (); alert. show (); Return true;} return Super. onkeydown (keycode, event);} private void loadapps () {intent = new intent (intent. action_main, null); intent. addcategory (intent. category_launcher); apps = getpackagemanager (). queryintentactivities (intent, 0);} public class gridadapter extends baseadapter {public gridadapter () {} public int getcount () {// todo auto-generated method stub return apps. size ();} public object getitem (INT position) {// todo auto-generated method stub return apps. get (position);} public long getitemid (INT position) {// todo auto-generated method stub return position;} public view getview (INT position, view convertview, viewgroup parent) {// todo auto-generated method stub imageview = NULL; If (convertview = NULL) {imageview = new imageview (bookshelfacti.pdf. this); imageview. setscaletype (imageview. scaletype. fit_center); imageview. setlayoutparams (New gridview. layoutparams (50, 50);} else {imageview = (imageview) convertview;} resolveinfo rI = apps. get (position); imageview. setimagedrawable (Ri. activityinfo. loadicon (getpackagemanager (); Return imageview ;}}}
Code writing is a bit messy, and it needs to be sorted out. Haha.
The above is just an eye-catching role. I really want to implement a good e-book, and there will be more work in the future. I also hope that interested friends can implement a perfect e-book on this basis.Source codeOpen, so that I don't have to proceed.
Add source code: Click to open the link