Android 在gridview中使用项目进行后台滚动
如何使用gridview实现后台滚动?如果听起来很模糊,我的意思是使用gridview实现书架,其中书架图像附加到gridview中的项目 我以前的回答只添加背景,但不允许它随项目滚动。你想要的是Nightwistler的回答:)Android 在gridview中使用项目进行后台滚动,android,android-layout,android-gridview,Android,Android Layout,Android Gridview,如何使用gridview实现后台滚动?如果听起来很模糊,我的意思是使用gridview实现书架,其中书架图像附加到gridview中的项目 我以前的回答只添加背景,但不允许它随项目滚动。你想要的是Nightwistler的回答:) 很抱歉误解了这个问题。我以前的回答只添加了背景,但没有让它与项目一起滚动。你想要的是Nightwistler的回答:) 很抱歉,我误解了这个问题。我花了很长时间才弄明白这一点,所以对于每个试图这样做的人,这里是我的电子书阅读器的代码 这是基于,所以他应该为原始代码赢得
很抱歉误解了这个问题。我以前的回答只添加了背景,但没有让它与项目一起滚动。你想要的是Nightwistler的回答:)
很抱歉,我误解了这个问题。我花了很长时间才弄明白这一点,所以对于每个试图这样做的人,这里是我的电子书阅读器的代码 这是基于,所以他应该为原始代码赢得荣誉
package net.nightwhistler.pageturner.view;
import net.nightwhistler.pageturner.R;
import net.nightwhistler.pageturner.library.LibraryBook;
import net.nightwhistler.pageturner.library.QueryResult;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.GridView;
public class BookCaseView extends GridView {
private Bitmap background;
private int mShelfWidth;
private int mShelfHeight;
private QueryResult<LibraryBook> result;
private LibraryBook selectedBook;
public BookCaseView(Context context, AttributeSet attributes) {
super(context, attributes);
this.setFocusableInTouchMode(true);
this.setClickable(false);
final Bitmap shelfBackground = BitmapFactory.decodeResource(context.getResources(),
R.drawable.shelf_single);
setBackground(shelfBackground);
this.setFocusable(true);
}
public void setBackground(Bitmap background) {
this.background = background;
mShelfWidth = background.getWidth();
mShelfHeight = background.getHeight();
}
protected void onClick( int bookIndex ) {
LibraryBook book = this.result.getItemAt(bookIndex);
this.selectedBook = book;
invalidate();
}
@Override
protected void dispatchDraw(Canvas canvas) {
final int count = getChildCount();
final int top = count > 0 ? getChildAt(0).getTop() : 0;
final int shelfWidth = mShelfWidth;
final int shelfHeight = mShelfHeight;
final int width = getWidth();
final int height = getHeight();
final Bitmap background = this.background;
for (int x = 0; x < width; x += shelfWidth) {
for (int y = top; y < height; y += shelfHeight) {
canvas.drawBitmap(background, x, y, null);
}
//This draws the top pixels of the shelf above the current one
Rect source = new Rect(0, mShelfHeight - top, mShelfWidth, mShelfHeight);
Rect dest = new Rect(x, 0, x + mShelfWidth, top );
canvas.drawBitmap(background, source, dest, null);
}
super.dispatchDraw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_BACK && this.selectedBook != null ) {
this.selectedBook = null;
invalidate();
return true;
}
return false;
}
}
package net.nightwistler.pageturner.view;
导入net.nightwistler.pagerner.R;
导入net.nightwistler.pageturner.library.LibraryBook;
导入net.nightwistler.pageturner.library.QueryResult;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Rect;
导入android.util.AttributeSet;
导入android.view.KeyEvent;
导入android.widget.GridView;
公共类BookCaseView扩展了GridView{
私有位图背景;
姆舍夫维兹私人酒店;
私人国际机场;
私有查询结果;
私人图书馆图书精选图书;
公共书架视图(上下文、属性集属性){
超级(上下文、属性);
此.setFocusableInTouchMode(true);
此参数为可设置可点击(false);
最终位图shelfBackground=BitmapFactory.decodeResource(context.getResources(),
R.可拉拔式货架(单件);
立根背景(shelfBackground);
此参数为.setFocusable(true);
}
公共空间背景(位图背景){
this.background=背景;
mShelfWidth=background.getWidth();
mShelfHeight=background.getHeight();
}
受保护的void onClick(int bookIndex){
LibraryBook book=this.result.getItemAt(bookIndex);
this.selectedBook=book;
使无效();
}
@凌驾
受保护的void dispatchDraw(画布){
最终整数计数=getChildCount();
final int top=count>0?getChildAt(0).getTop():0;
最终整数shelfWidth=mShelfWidth;
最终整数shelfHeight=mShelfHeight;
最终整数宽度=getWidth();
最终整数高度=getHeight();
最终位图背景=this.background;
对于(int x=0;x
我花了很长时间才弄明白这一点,所以对于每个试图这样做的人来说,这是我的电子书阅读器中的代码
这是基于,所以他应该为原始代码赢得荣誉
package net.nightwhistler.pageturner.view;
import net.nightwhistler.pageturner.R;
import net.nightwhistler.pageturner.library.LibraryBook;
import net.nightwhistler.pageturner.library.QueryResult;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.GridView;
public class BookCaseView extends GridView {
private Bitmap background;
private int mShelfWidth;
private int mShelfHeight;
private QueryResult<LibraryBook> result;
private LibraryBook selectedBook;
public BookCaseView(Context context, AttributeSet attributes) {
super(context, attributes);
this.setFocusableInTouchMode(true);
this.setClickable(false);
final Bitmap shelfBackground = BitmapFactory.decodeResource(context.getResources(),
R.drawable.shelf_single);
setBackground(shelfBackground);
this.setFocusable(true);
}
public void setBackground(Bitmap background) {
this.background = background;
mShelfWidth = background.getWidth();
mShelfHeight = background.getHeight();
}
protected void onClick( int bookIndex ) {
LibraryBook book = this.result.getItemAt(bookIndex);
this.selectedBook = book;
invalidate();
}
@Override
protected void dispatchDraw(Canvas canvas) {
final int count = getChildCount();
final int top = count > 0 ? getChildAt(0).getTop() : 0;
final int shelfWidth = mShelfWidth;
final int shelfHeight = mShelfHeight;
final int width = getWidth();
final int height = getHeight();
final Bitmap background = this.background;
for (int x = 0; x < width; x += shelfWidth) {
for (int y = top; y < height; y += shelfHeight) {
canvas.drawBitmap(background, x, y, null);
}
//This draws the top pixels of the shelf above the current one
Rect source = new Rect(0, mShelfHeight - top, mShelfWidth, mShelfHeight);
Rect dest = new Rect(x, 0, x + mShelfWidth, top );
canvas.drawBitmap(background, source, dest, null);
}
super.dispatchDraw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ( keyCode == KeyEvent.KEYCODE_BACK && this.selectedBook != null ) {
this.selectedBook = null;
invalidate();
return true;
}
return false;
}
}
package net.nightwistler.pageturner.view;
导入net.nightwistler.pagerner.R;
导入net.nightwistler.pageturner.library.LibraryBook;
导入net.nightwistler.pageturner.library.QueryResult;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.graphics.Canvas;
导入android.graphics.Rect;
导入android.util.AttributeSet;
导入android.view.KeyEvent;
导入android.widget.GridView;
公共类BookCaseView扩展了GridView{
私有位图背景;
姆舍夫维兹私人酒店;
私人国际机场;
私有查询结果;
私人图书馆图书精选图书;
公共书架视图(上下文、属性集属性){
超级(上下文、属性);
此.setFocusableInTouchMode(true);
此参数为可设置可点击(false);
最终位图shelfBackground=BitmapFactory.decodeResource(context.getResources(),
R.可拉拔式货架(单件);
立根背景(shelfBackground);
此参数为.setFocusable(true);
}
公共空间背景(位图背景){
this.background=背景;
mShelfWidth=background.getWidth();
mShelfHeight=background.getHeight();
}
受保护的void onClick(int bookIndex){
LibraryBook book=this.result.getItemAt(bookIndex);
this.selectedBook=book;
使无效();
}
@凌驾
受保护的void dispatchDraw(画布){
最终整数计数=getChildCount();
final int top=count>0?getChildAt(0).getTop():0;
最终整数shelfWidth=mShelfWidth;
最终整数shelfHeight=mShelfHeight;
最终整数宽度=getWidth();
最终整数高度=getHeight();
最终位图背景=this.background;
对于(int x=0;x