It was also ireader to achieve the effect of the bookshelf, but that was implemented using ListView, which was not easy to use. Most of them are implemented with the GridView, the online data is relatively small, some open source of e-books are focused on reading, and did not like Ireader and QQ to read the Bookshelf effect.
Bookshelf This effect I have already realized, originally wanted to make a perfect ebook, but because of their laziness, only lasted a day or two, today again found the previous code to share out, I hope we can achieve a relatively perfect open source of the ebook. No more nonsense, first look at the effect:
The local part has not done, after completes can the local book load into the bookshelf, this is only a beginning, behind has many complex not to do.
Let's look at the principle of the bookshelf first!
First look at the layout file under Layout main.xml
Copy Code code as follows:
<?xmlversion= "1.0" encoding= "Utf-8"?>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<includelayout= "@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"/>
</SlidingDrawer>
</RelativeLayout>
The above is a custom gridview that mainly implements the bookshelf, because each book is an item, calculates the height of each row in the custom GridView, and then draws the bookshelf. Here is a drawer.
Copy Code code as follows:
publicclassmygridviewextendsgridview{
Privatebitmapbackground;
Publicmygridview (contextcontext,attributesetattrs) {
Super (CONTEXT,ATTRS);
Background=bitmapfactory.decoderesource (Getresources (),
R.drawable.bookshelf_layer_center);
}
@Override
Protectedvoiddispatchdraw (Canvascanvas) {
Intcount=getchildcount ();
Inttop=count>0?getchildat (0). GetTop (): 0;
Intbackgroundwidth=background.getwidth ();
Intbackgroundheight=background.getheight () +2;
Intwidth=getwidth ();
Intheight=getheight ();
for (Inty=top;yfor (Intx=0;x<width;x+=backgroundwidth) {
Canvas.drawbitmap (Background,x,y,null);
}
}
Super.dispatchdraw (canvas);
}
}
The above is a custom bookshelf of the GridView, but also to achieve the core of the Bookshelf method.
Then the layout of each item:
Copy Code code as follows:
<?xmlversion= "1.0" encoding= "Utf-8"?>
<linearlayoutxmlns: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= "Tian Long Eight"
Android:padding= "15DP"
Android:textcolor= "#000000"
/>
</LinearLayout>
It can be shown in the main activity at the end.
Copy Code code as follows:
publicclassbookshelfactivityextendsbaseactivity{
privategridviewbookshelf;
privateint[]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.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.drawable.cover_txt,r.drawable.cover_txt
};
privatestring[]name={
"Tian Long Eight", "Search God", "Outlaws of the Marsh", "the Underworld sad Feeling"
};
PRIVATEGRIDVIEWGV;
PRIVATESLIDINGDRAWERSD;
Privatebuttoniv;
privatelist<resolveinfo>apps;
/**calledwhentheactivityisfirstcreated.*/
@Override
Publicvoidoncreate (bundlesavedinstancestate) {
Super.oncreate (savedinstancestate);
This.requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.main);
bookshelf= (GridView) Findviewbyid (r.id.bookshelf);
Shlefadapteradapter=newshlefadapter ();
Bookshelf.setadapter (adapter);
Bookshelf.setonitemclicklistener (Newonitemclicklistener () {
@Override
Publicvoidonitemclick (ADAPTERVIEW<?>ARG0,VIEWARG1,INTARG2,
LONGARG3) {
Todoauto-generatedmethodstub
if (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 (Newgridadapter ());
Sd.setondraweropenlistener (Newslidingdrawer.ondraweropenlistener)/Open Drawer
{
@Override
Publicvoidondraweropened () {
Iv.settext ("return");
Iv.setbackgroundresource (r.drawable.btn_local);/In response to open drawer event
, set the image down to the
}
});
Sd.setondrawercloselistener (Newslidingdrawer.ondrawercloselistener () {
@Override
Publicvoidondrawerclosed () {
Iv.settext ("local");
Iv.setbackgroundresource (r.drawable.btn_local);/In response to close drawer event
}
});
}
classshlefadapterextendsbaseadapter{
@Override
Publicintgetcount () {
Todoauto-generatedmethodstub
returndata.length+5;
}
@Override
Publicobjectgetitem (intarg0) {
Todoauto-generatedmethodstub
returnarg0;
}
@Override
Publiclonggetitemid (intarg0) {
Todoauto-generatedmethodstub
returnarg0;
}
@Override
Publicviewgetview (INTPOSITION,VIEWCONTENTVIEW,VIEWGROUPARG2) {
Todoauto-generatedmethodstub
Contentview=layoutinflater.from (Getapplicationcontext ()). Inflate (R.layout.item1,null);
textviewview= (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);
}
Returncontentview;
}
}
@Override
Publicbooleanonkeydown (intkeycode,keyeventevent) {
Todoauto-generatedmethodstub
if (keycode==keyevent.keycode_back) {
Alertdialog.builderbuilder=newalertdialog.builder (this);
Builder.setmessage (Are you sure you want to quit?) ")
. Setcancelable (False)
. Setpositivebutton ("OK",
Newdialoginterface.onclicklistener () {
Publicvoidonclick (Dialoginterfacedialog,
IntID) {
Finish ();
}
})
. Setnegativebutton ("Back",
Newdialoginterface.onclicklistener () {
Publicvoidonclick (Dialoginterfacedialog,
IntID) {
Dialog.cancel ();
}
});
Alertdialogalert=builder.create ();
Alert.show ();
Returntrue;
}
Returnsuper.onkeydown (keycode,event);
}
Privatevoidloadapps () {
Intentintent=newintent (Intent.action_main,null);
Intent.addcategory (Intent.category_launcher);
Apps=getpackagemanager (). Queryintentactivities (intent,0);
}
publicclassgridadapterextendsbaseadapter{
Publicgridadapter () {
}
Publicintgetcount () {
Todoauto-generatedmethodstub
Returnapps.size ();
}
Publicobjectgetitem (intposition) {
Todoauto-generatedmethodstub
Returnapps.get (position);
}
Publiclonggetitemid (intposition) {
Todoauto-generatedmethodstub
Returnposition;
}
Publicviewgetview (intposition,viewconvertview,viewgroupparent) {
Todoauto-generatedmethodstub
Imageviewimageview=null;
if (convertview==null) {
Imageview=newimageview (Bookshelfactivity.this);
Imageview.setscaletype (ImageView.ScaleType.FIT_CENTER);
Imageview.setlayoutparams (Newgridview.layoutparams (50,50));
}else{
imageview= (ImageView) Convertview;
}
Resolveinfori=apps.get (position);
Imageview.setimagedrawable (Ri.activityinfo
. LoadIcon (Getpackagemanager ());
Returnimageview;
}
}
}
Code to write a little messy, to be sorted out, haha.
Above is just a finishing touch of the role, really want to achieve a good ebook, there are many jobs behind, but also hope that interested friends on this basis to achieve a perfect ebook, and then open the source code so that I do not have to do in the downward, quack.