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. However, my laziness only lasted for one or two days. Today I found my previous code and shared it, we hope that you can achieve a perfect open-source e-book. 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.Copy codeThe Code is 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 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.Copy codeThe Code is 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; y For (intx = 0; x <width; x + = backgroundWidth ){
Canvas. drawBitmap (background, x, y, null );
}
}
Super. dispatchDraw (canvas );
}
}
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:Copy codeThe Code is 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 = "tianlong Babu"
Android: padding = "15dp"
Android: textColor = "#000000"
/>
</LinearLayout>
Finally, it can be displayed in the main activity.Copy codeThe Code is 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
};
PrivateString [] name = {
"Tianlong Babu", "soushenji", "Water Margin", "Darkness Sorrow"
};
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 = (s0000ingdrawer) findViewById (R. id. sliding );
Iv = (Button) findViewById (R. id. imageViewIcon );
Gv. setAdapter (newGridAdapter ());
Sd. setOnDrawerOpenListener (newsjavasingdrawer. OnDrawerOpenListener () // open the drawer
{
@ Override
PublicvoidonDrawerOpened (){
Iv. setText ("return ");
Iv. setBackgroundResource (R. drawable. btn_local); // responds to Drawer Opening events
// Set the image to the lower
}
});
Sd. setOnDrawerCloseListener (newsreceivingdrawer. OnDrawerCloseListener (){
@ Override
PublicvoidonDrawerClosed (){
Iv. setText ("local ");
Iv. setBackgroundResource (R. drawable. btn_local); // responds to the 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 exit? ")
. SetCancelable (false)
. SetPositiveButton ("OK ",
NewDialogInterface. OnClickListener (){
PublicvoidonClick (DialogInterfacedialog,
Intid ){
Finish ();
}
})
. SetNegativeButton ("return ",
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 (bookshelfacti.pdf. 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 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, then open the source code so that I don't need to proceed.