In the forum, we often see people asking how to achieve the drag effect of UC or moji weather. In fact, most implementations refer to the workspace class in launcher. We can see that an open-source project also implements this function.
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.
Usagein your layout
<org.taptwo.android.widget.ViewFlow android:id="@+id/viewflow" app:sidebuffer="5" />
The useapp: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
More convenientapp:sidebuffer
Attribute, the application namespace must be encoded 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:
viewFlow.setAdapter(myAdapter, 8);
Although possible, you shoshould 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 wocould want to implement your ownViewFlow.ViewSwitchListener
And pass it tosetOnViewSwitchListener()
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 ofFlowIndicator
Is to present a visual representation of where in the item list focus is at. You may either implementFlowIndicator
Yourself or use
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 eachView
In 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 yourViewFlow
WithFlowIndicator
:
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 nextView
In 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 yourViewFlow
WithFlowIndicator
:
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 jar
Inandroid-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 forwardonConfigurationChanged
Event toViewFlow
From 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 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 addcloses #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 extends kerfeldt
Licensed under the Apache license, version 2.0