Android爱读app开发记录之二---书架界面

一、基本思路

书架界面实际上就是一个自定义的GridView ,然后给相应的控件设置相应的事件就可以了。比如给书籍(item)设置点击和长按事件,导入按钮设置点击事件...

二、自定义GridView

MyGradiView.java
package com.xiaoli.util;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.GridView;

import com.hck.LoverReader.R;

public class MyGridView extends GridView {

   public static Bitmap background;

   public MyGridView(Context context, AttributeSet attrs) {
      super(context, attrs);
      background = BitmapFactory.decodeResource(getResources(),
            R.drawable.shelf_background2);
   }
   public void setBackground(int drawable){
      background= BitmapFactory.decodeResource(getResources(),
            drawable);
   }
   @Override
   protected void dispatchDraw(Canvas 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();
      //Bitmap bitmap=Bitmap.createScaledBitmap(background, MyTool.getWidth(), MyTool.getHight()/8, true);
      for (int y = top; y < height; y += backgroundHeight) {
         for (int x = 0; x < width; x += backgroundWidth) {
            canvas.drawBitmap(background, x, y, null);
         }
      }

      super.dispatchDraw(canvas);
   }

}
设配器
class ShlefAdapter extends BaseAdapter{

			@Override
			public int getCount() {
				return listItem.size();
			}

			@Override
			public Object getItem(int arg0) {
				return arg0;
			}

			@Override
			public long getItemId(int arg0) {
				return arg0;
			}

			@Override
			public View getView(int position, View contentView, ViewGroup arg2) {
				
				contentView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item1, null);
				//文字
				TextView view=(TextView) contentView.findViewById(R.id.imageView1);
				view.setText(listItem.get(position).get("BookName").toString());
				//图片
				ImageView image1 =(ImageView)contentView.findViewById(R.id.image);
				if(position==0){
					image1.setImageResource(R.drawable.cover_txt0);
				} else if(position==1){
					image1.setImageResource(R.drawable.cover_txt1);
				}else{
					image1.setImageResource(R.drawable.cover);
				}
				return contentView;
			}
	    }
每一项显示的布局item1.xml



    

    
效果图
Android爱读app开发记录之二---书架界面_第1张图片

然后是主布局文件main.xml



  
    
  
效果图
Android爱读app开发记录之二---书架界面_第2张图片
最后在网格视图中添加数据库存储的数据,就可以有书籍的效果了
相关代码
public void getDate() {
   SQLiteDatabase db = localbook.getReadableDatabase();
   String col[] = { "path" };
   Cursor cur = db.query(FinalDate.DATABASE_TABKE, col, "type=1", null, null, null, null);
   Cursor cur1 = db.query(FinalDate.DATABASE_TABKE, col, "type=2", null, null, null, null);
   Integer num = cur.getCount();
   Integer num1 = cur1.getCount();
   Log.i("hck", "booklistActivity2 :"+num);
   Log.i("hck", "booklistActivity2 :"+num1);
   ArrayList arraylist = new ArrayList();
   while (cur1.moveToNext()) {
      String s = cur1.getString(cur1.getColumnIndex("path"));
      arraylist.add(s);
   }
   while (cur.moveToNext()) {
      String s = cur.getString(cur.getColumnIndex("path"));
      arraylist.add(s);
   }
   db.close();
   cur.close();
   cur1.close();
   if (listItem == null)
      listItem = new ArrayList>();
   listItem.clear();
   String[] bookids = getResources().getStringArray(R.array.bookid);
   String[] booknames = getResources().getStringArray(R.array.bookname);
   String[] bookauthors = getResources().getStringArray(R.array.bookauthor);
   Map maps = new HashMap();
   for (int i = 0; i < bookids.length; i++) {
      String[] value = new String[2];
      value[0] = booknames[i];
      value[1] = bookauthors[i];
      maps.put(bookids[i], value);
   }
   for (int i = 0; i < num + num1; i++) {
      if (i < num1) {
         File file1 = new File(arraylist.get(i));
         String m = file1.getName().substring(0, file1.getName().length() - 4);
         if (m.length() > 8) {
            m = m.substring(0, 8) + "...";
         }
         String id = arraylist.get(i).substring(arraylist.get(i).lastIndexOf("/") + 1);
         String[] array = maps.get(id);
         String auther = array != null && array[1] == null ? "未知" : array[1];
         String name = array[0] == null ? m : array[0];
         map = new HashMap();

         if (i == 0) {
            map.put("itemback", R.drawable.itemback);
         } else if ((i % 2) == 0) {
            map.put("itemback", R.drawable.itemback);
         }
         map.put("ItemImage", map2 != null ? map2.get(file1.getName())[0] : R.drawable.cover);
         map.put("BookName", name == null ? m : name);
         map.put("ItemTitle1", "作者:" + auther);
         map.put("LastImage", "推荐书目");
         map.put("path", file1.getPath());
         map.put("com", 0 + file1.getName());// 单独用于排序
         listItem.add(map);
         Log.i("hck","本地"+ listItem.size()+"size");
      } else {
         map = new HashMap();

         File file1 = new File(arraylist.get(i));
         String m = file1.getName().substring(0, file1.getName().length() - 4);
         if (m.length() > 8) {
            m = m.substring(0, 8) + "...";
         }
         if (i == 0) {
            map.put("itemback", R.drawable.itemback);
         } else if ((i % 2) == 0) {
            map.put("itemback", R.drawable.itemback);
         }
         map.put("ItemImage", R.drawable.cover);
         map.put("BookName", m);
         map.put("ItemTitle", m);
         map.put("ItemTitle1", "作者:未知");
         map.put("LastImage", "本地导入");
         map.put("path", file1.getPath());
         map.put("com", "1");
         listItem.add(map);
         
      }
   }
   Collections.sort(listItem, new PinyinListComparator());
// if (adapter == null) {
      adapter = new ShlefAdapter();
      toolbarGrid.setAdapter(adapter);
// }
// adapter.notifyDataSetChanged();
}
最后效果图


底部的功能是弹出了一个网格菜单,然后在菜单的每一个Item里面实现点击事件就行了,
说明:这些代码基于网上的蝌蚪听书神器改写
本文完!

你可能感兴趣的:(Android开发)