Android UI开发第一篇——android-viewflow

ViewFlow for Android


在论坛里,经常看到有人问如何实现UC或墨迹天气那样的拖动效果。其实大部分的实现都是参考了Launcher里的Workspace这个类。刚好看到有个开源项目也是实现了这种功能,地址在
http://code.google.com/p/andro-views/,希望对有需要的人有所帮助,做出很cool的应用出来。

View Flow for Android

ViewFlow is an Android UI widget providing a horizontally scrollable ViewGroup with items populated from an Adapter. Scroll down to the bottom of the page for a screen shot.

The component is a Library Project. This means that there's no need to copy-paste resources into your own project, simply add the viewflow component as a reference to any project.

Usage

In your layout

[html]   view plain copy
  1. <org.taptwo.android.widget.ViewFlow  
  2.     android:id="@+id/viewflow"  
  3.     app:sidebuffer="5"  
  4.     />  

The use of app:sidebuffer is optional. It defines the number of Views to buffer on each side of the currently shown View. The default sidebuffer is 3, making up a grand total of 7 (3 * 2 + 1) Views loaded at a time (at max). To be able to use the more convenient app:sidebuffer attribute, the application namespace must be included in the same manner as the android namespace is. Please refer to the layout main.xml in the example project for a full example. Again, note that it's the application namespace and not the viewflow namespace that must be referred likexmlns:app="http://schemas.android.com/apk/res/your.application.package.here".

In your activity

[java]   view plain copy
  1. ViewFlow viewFlow = (ViewFlow) findViewById(R.id.viewflow);  
  2. viewFlow.setAdapter(myAdapter);  

Setting a different initial position (0 being default) is as easy as:

[java]   view plain copy
  1. viewFlow.setAdapter(myAdapter, 8);  

Although possible, you should not call setSelection(...) immediately after callingsetAdapter(myAdapter) as that might load unnecessary views giving you a decrease in performance.

Listen on screen change events

If you need to listen to screen change events you would want to implement your ownViewFlow.ViewSwitchListener and pass it to thesetOnViewSwitchListener() method.

[java]   view plain copy
  1. viewFlow.setOnViewSwitchListener(new ViewSwitchListener() {  
  2.     public void onSwitched(View v, int position) {  
  3.         // Your code here  
  4.     }  
  5. });  

Flow Indicator

It is also possible to add a flow view indicator to your layout. The purpose of aFlowIndicator is to present a visual representation of where in the item list focus is at. You may either implement aFlowIndicator yourself or use an implementation provided by the View Flow library. The View Flow library currently supports the following indicators:

Circle Flow Indicator

This indicator shows a circle for each View in the adapter with a special circle representing the currently selected view (see screenshot below).

[html]   view plain copy
  1. <org.taptwo.android.widget.CircleFlowIndicator  
  2.     android:padding="10dip" android:layout_height="wrap_content"  
  3.     android:layout_width="wrap_content" android:id="@+id/viewflowindic"  
  4.     android:background="#00000000"/>  

And then you'll need to connect your  ViewFlow  with the  FlowIndicator :

[java]   view plain copy
  1. CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);  
  2. viewFlow.setFlowIndicator(indic);  

The following attributes are supported: activeColorinactiveColor,activeType (either fill or stroke), inactiveType (either fill or stroke),fadeOut (time in ms until indicator fades out, 0 = never), radius.

Title Flow Indicator

This indicator presents the title of the previous, current and next View in the adapter (see screenshot below).

[html]   view plain copy
  1. <org.taptwo.android.widget.TitleFlowIndicator  
  2.         android:id="@+id/viewflowindic" android:layout_height="wrap_content"  
  3.         android:layout_width="fill_parent"  
  4.         app:footerLineHeight="2dp"  
  5.         app:footerTriangleHeight="10dp" app:textColor="#FFFFFFFF" app:selectedColor="#FFFFC445"  
  6.         app:footerColor="#FFFFC445" app:titlePadding="10dp" app:textSize="11dp" app:selectedSize="12dp"  
  7.         android:layout_marginTop="10dip"  
  8.         app:clipPadding="5dp" />  

And then you'll need to connect your  ViewFlow  with the  FlowIndicator :

[java]   view plain copy
  1. TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic);  
  2.     indicator.setTitleProvider(myTitleProvider);  
  3.     viewFlow.setFlowIndicator(indicator);  

Building a jar file

If you rather want a jar file instead of a including the project as an android library, runant jar in the android-viewflow/viewflow folder, to build a jar file.

Caveats

The manifest states a min sdk version of 4, which is true. But in any case you want to support an api level < 8 you will have to forward anonConfigurationChanged event to the ViewFlow from your Activity. I know this isn't a very nice solution, feel free to propose better ones!

[java]   view plain copy
  1. @Override  
  2.     public void onConfigurationChanged(Configuration newConfig) {  
  3.         super.onConfigurationChanged(newConfig);  
  4.         viewFlow.onConfigurationChanged(newConfig);  
  5.     }  

Contributions

The following persons deserves a mention for their contributions:

Want to contribute?

GitHub has some great articles on how to get started with Git and GitHub and how to fork a project.

Contributers are recommended to fork the app on GitHub (but don't have too). Create a feature branch, push the branch to git hub, press Pull Request and write a simple explanation.

One fix per commit. If let's say a commit closes the open issue 12. Just add closes #12 in your commit message to close that issue automagically.

If you still feel uncomfortable contributing the project github-wise, don't hesistate to send a regular patch.

All code that is contributed must be compliant with Apache License 2.0.

License

Copyright (c) 2011 Patrik Åkerfeldt

Licensed under the Apache License, Version 2.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值