Android UI开发第十三篇——android-viewflow
在论坛里,经常看到有人问如何实现UC或墨迹天气那样的拖动效果。其实大部分的实现都是参考了Launcher里的Workspace这个类。刚好看到有个开源项目也是实现了这种功能,地址在http://code.google.com/p/andro-views/,希望对有需要的人有所帮助,做出很cool的应用出来。 View Flow for Android ViewFlow is an Android UI widget providing a horizontally scrollableViewGroupwith items populated from anAdapter. Scroll down to the bottom of the page for a screen shot. The component is aLibrary 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 <org.taptwo.android.widget.ViewFlow android:id="@+id/viewflow" app:sidebuffer="5" /> The use ofapp:sidebufferis 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 convenientapp:sidebufferattribute, 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 andnotthe viewflow namespace that must be referred likexmlns:app="http://schemas.android.com/apk/res/your.application.package.here". In your activity ViewFlow viewFlow = (ViewFlow) findViewById(R.id.viewflow); viewFlow.setAdapter(myAdapter); Setting a different initial position (0 being default) is as easy as: viewFlow.setAdapter(myAdapter, 8); Although possible, you should not callsetSelection(...)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.ViewSwitchListenerand pass it to thesetOnViewSwitchListener()method. viewFlow.setOnViewSwitchListener(new ViewSwitchListener() { public void onSwitched(View v, int position) { // Your code here } }); Flow Indicator It is also possible to add a flow view indicator to your layout. The purpose of aFlowIndicatoris to present a visual representation of where in the item list focus is at. You may either implement aFlowIndicatoryourself 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 eachViewin the adapter with a special circle representing the currently selected view (see screenshot below). <org.taptwo.android.widget.CircleFlowIndicator android:padding="10dip" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/viewflowindic" android:background="#00000000"/> And then you'll need to connect yourViewFlowwith theFlowIndicator: CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic); viewFlow.setFlowIndicator(indic); The following attributes are supported:activeColor,inactiveColor,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 nextViewin the adapter (see screenshot below). <org.taptwo.android.widget.TitleFlowIndicator android:id="@+id/viewflowindic" android:layout_height="wrap_content" android:layout_width="fill_parent" app:footerLineHeight="2dp" app:footerTriangleHeight="10dp" app:textColor="#FFFFFFFF" app:selectedColor="#FFFFC445" app:footerColor="#FFFFC445" app:titlePadding="10dp" app:textSize="11dp" app:selectedSize="12dp" android:layout_marginTop="10dip" app:clipPadding="5dp" /> And then you'll need to connect yourViewFlowwith theFlowIndicator: TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic); indicator.setTitleProvider(myTitleProvider); 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 jarin theandroid-viewflow/viewflowfolder, 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 anonConfigurationChangedevent to theViewFlowfrom yourActivity. I know this isn't a very nice solution, feel free to propose better ones! @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); viewFlow.onConfigurationChanged(newConfig); } Contributions The following persons deserves a mention for their contributions: Eric Taix Marc Reichelt,http://marcreichelt.blogspot.com/ Want to contribute? GitHub has some great articles onhow to get started with Git and GitHuband how tofork 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 addcloses #12in 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 withApache License 2.0. License Copyright (c) 2011Patrik Åkerfeldt Licensed under theApache License, Version 2.0 本文转自xyz_lmn51CTO博客,原文链接:http://blog.51cto.com/xyzlmn/817285,如需转载请自行联系原作者