使用ListView FastScroller,默认滑块和自定义滑块图片的样子:
设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可:
<ListView android:id=”@+id/listView” android:layout_width=”fill_parent”
android:layout_height=”fill_parent” android:fastScrollEnabled=”true”
android:focusable=”true” />
但是有时候会发现设置属性无效,滚动ListView并未出现滑块。原因是该属性生效有最小记录限制。当ListView记录能够在4屏以内显示(也就是说滚动4页)就不会出现滑块。可能是api设计者认为这么少的记录不需要快速滚动。
我的依据是android源代码,见FastScroller的常量声明:
// Minimum number of pages to justify showing a fast scroll thumb
private static int MIN_PAGES = 4;
以及:
// Are there enough pages to require fast scroll? Recompute only if total count changes
if (mItemCount != totalItemCount && visibleItemCount > 0) {
mItemCount = totalItemCount;
mLongList = mItemCount / visibleItemCount >= MIN_PAGES;
}
通篇查看了ListView及其超累AbsListView,都未找到自定义图片的设置接口。看来是没打算让开发者更改了。但是用户要求我们自定义这个图片。那只能用非常手段了。
经过分析发现,该图片是ListView超类AbsListView的一个成员mFastScroller对象的成员 mThumbDrawable。这里mThumbDrawable是Drawable类型的。mFastScroller是FastScroller类 型,这个类型比较麻烦,类的声明没有modifier,也就是default(package),只能供包内的类调用。
因此反射代码写的稍微麻烦一些:
try {
Field f = AbsListView.class.getDeclaredField(“mFastScroller”);
f.setAccessible(true);
Object o=f.get(listView);
f=f.getType().getDeclaredField(“mThumbDrawable”);
f.setAccessible(true);
Drawable drawable=(Drawable) f.get(o);
drawable=getResources().getDrawable(R.drawable.icon);
f.set(o,drawable);
Toast.makeText(this, f.getType().getName(), 1000).show();
} catch (Exception e) {
throw new RuntimeException(e);
}
这样就可以改变默认的滑块图片了。
源代码见:http://easymorse.googlecode.com/svn/tags/ListViewCustomerFastScroller-0.1/
http://marshal.easymorse.com/archives/3760
页效果,类似下面的样子:
在电子书应用中会很常见。这里需要两个要点:在电子书应用中会很常见。这里需要两个要点:- 翻页动画
- 手势上下轻扫(swipe)的处理
先说一下轻扫(swipe)的实现,可以参考编写简单的手势示例:Tap了解手势种类。
在viewDidLoad方法中注册了对上、下、左、右四个方向轻松的处理方法:
- (void)viewDidLoad {
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
[[self view] addGestureRecognizer:recognizer];
[recognizer release];
[super viewDidLoad];
可以看到,都是同一个方法,handleSwipeFrom。
在该方法中,再识别具体是哪个方向的轻扫手势,比如判断是向下的轻扫:
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(@"Swipe received.");
if (recognizer.direction==UISwipeGestureRecognizerDirectionDown) {
NSLog(@"swipe down");
判断是向上的轻扫:
if (recognizer.direction==UISwipeGestureRecognizerDirectionUp) {
NSLog(@"swipe up");
有关动画的处理,比如向下(往回)翻页,类似这样:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
向上(向前)翻页,只需改为:
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUpforView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
如果是电子书,还需要考虑一个问题,就是有多个页面(图形),比如50页。那么需要有一个数据结构来保存这些页面的图片路径:
- objc数据结构,比如数组
- sqlite数据库表
这样,写一套翻页代码和加载什么图形之间就可以解耦。
本文示例使用的是数组,类似这样:
pages=[[NSArray alloc] initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",
nil];
图片保存在resources下。
为了能让上页下页翻页的时候找到关联的页面,采用了如下机制:
- 将图片封装为UIImageView显示
- 可以为UIImageView设置一个tag值,值为数组下标+1
- 这样,上级view有方法能根据tag查询到UIImageView,比如:UIView *currentView=[self.view viewWithTag:currentTag];
- 设置一个成员变量currentTag保存当前的tag值
比如这样,当应用加载的时候显示第一页:
currentTag=1;
NSString *path = [[NSBundle mainBundle] pathForResource:@"pageflip1" ofType:@"mp3"];
player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
//[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:[pages objectAtIndex:(currentTag-1)]]];
[contentView setUserInteractionEnabled:YES];
contentView.tag=currentTag;
在翻页时的处理:
if (currentTag<[pages count]) {
UIView *currentView=[self.view viewWithTag:currentTag];
currentTag++;
UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[contentView setImage:[UIImage imageNamed:[pages objectAtIndex:(currentTag-1)]]];
[contentView setUserInteractionEnabled:YES];
contentView.tag=currentTag;
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:0.7f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationRepeatAutoreverses:NO];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[currentView removeFromSuperview];
[self.view addSubview:contentView];
[UIView commitAnimations];
_list=[_dataString componentsSeparatedByString:@","];