This repository has been archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 695
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#32 Add the example of asynchronous data loading into ViewFlow
- Loading branch information
1 parent
b4063cd
commit 063b333
Showing
6 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<ProgressBar android:id="@+id/progress" | ||
android:layout_width="wrap_content" android:layout_height="wrap_content"></ProgressBar> | ||
|
||
<LinearLayout android:layout_height="wrap_content" | ||
android:id="@+id/content" android:layout_width="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<TextView android:id="@+id/text" android:text="Content loaded." | ||
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> | ||
|
||
<TextView android:id="@+id/date" android:text="This is content." | ||
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> | ||
|
||
</LinearLayout> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
viewflow-example/src/org/taptwo/android/widget/viewflow/example/AsyncAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package org.taptwo.android.widget.viewflow.example; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
import org.taptwo.android.widget.TitleProvider; | ||
import org.taptwo.android.widget.viewflow.example.R; | ||
|
||
import android.content.Context; | ||
import android.os.AsyncTask; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.ProgressBar; | ||
import android.widget.TextView; | ||
|
||
|
||
public class AsyncAdapter extends BaseAdapter implements TitleProvider { | ||
|
||
private LayoutInflater mInflater; | ||
|
||
private static final DateFormat dfTitle = new SimpleDateFormat("E, dd MMM"); | ||
|
||
private static final int daysDepth = 10; | ||
private static final int daysSize = daysDepth * 2 + 1; | ||
|
||
private static Date[] dates = new Date[ daysSize ]; | ||
private static String[] content = new String[ daysSize ]; | ||
|
||
|
||
private class ViewHolder { | ||
ProgressBar mProgressBar; | ||
View mContent; | ||
TextView mDate; | ||
} | ||
|
||
|
||
public AsyncAdapter(Context context) { | ||
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
prepareDates(); | ||
} | ||
|
||
@Override | ||
public String getItem(int position) { | ||
return content[position]; | ||
} | ||
|
||
@Override | ||
public long getItemId(int position) { | ||
return position; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup parent) { | ||
return drawView(position, convertView); | ||
} | ||
|
||
private View drawView(int position, View view) { | ||
ViewHolder holder = null; | ||
|
||
if(view == null) { | ||
view = mInflater.inflate(R.layout.day_view, null); | ||
|
||
holder = new ViewHolder(); | ||
|
||
holder.mProgressBar = (ProgressBar) view.findViewById(R.id.progress); | ||
holder.mDate = (TextView) view.findViewById(R.id.date); | ||
holder.mContent = (View) view.findViewById(R.id.content); | ||
|
||
view.setTag(holder); | ||
} else { | ||
holder = (ViewHolder) view.getTag(); | ||
} | ||
|
||
|
||
final String o = getItem(position); | ||
if (o != null) { | ||
holder.mProgressBar.setVisibility(View.GONE); | ||
holder.mDate.setText(o); | ||
holder.mContent.setVisibility(View.VISIBLE); | ||
} | ||
else { | ||
new LoadContentTask().execute(position, view); | ||
|
||
holder.mContent.setVisibility(View.GONE); | ||
holder.mProgressBar.setVisibility(View.VISIBLE); | ||
} | ||
|
||
return view; | ||
} | ||
|
||
@Override | ||
public String getTitle(int position) { | ||
return dfTitle.format( dates[position] ); | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return dates.length; | ||
} | ||
|
||
public int getTodayId() { | ||
return daysDepth; | ||
} | ||
|
||
public Date getTodayDate() { | ||
return dates[daysDepth]; | ||
} | ||
|
||
/** | ||
* Prepare dates for navigation, to past and to future | ||
*/ | ||
private void prepareDates() { | ||
Date today = new Date(); | ||
|
||
Calendar calPast = Calendar.getInstance(); | ||
Calendar calFuture = Calendar.getInstance(); | ||
|
||
calPast.setTime(today); | ||
calFuture.setTime(today); | ||
|
||
dates[ daysDepth ] = calPast.getTime(); | ||
for (int i = 1; i <= daysDepth; i++) { | ||
calPast.add( Calendar.DATE, -1 ); | ||
dates[ daysDepth - i ] = calPast.getTime(); | ||
|
||
calFuture.add( Calendar.DATE, 1 ); | ||
dates[ daysDepth + i ] = calFuture.getTime(); | ||
} | ||
} | ||
|
||
|
||
private class LoadContentTask extends AsyncTask<Object, Object, Object> { | ||
|
||
private Integer position; | ||
private View view; | ||
|
||
@Override | ||
protected Object doInBackground(Object... arg) { | ||
position = (Integer) arg[0]; | ||
view = (View) arg[1]; | ||
|
||
// long-term task is here | ||
try { | ||
Thread.sleep(3000); // do nothing for 3000 miliseconds (3 second) | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return getTitle(position); | ||
} | ||
|
||
protected void onPostExecute(Object result) { | ||
// process result | ||
content[position] = (String) result; | ||
|
||
drawView(position, view); | ||
|
||
view.postInvalidate(); | ||
} | ||
|
||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
viewflow-example/src/org/taptwo/android/widget/viewflow/example/AsyncDataFlowExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.taptwo.android.widget.viewflow.example; | ||
|
||
import org.taptwo.android.widget.TitleFlowIndicator; | ||
import org.taptwo.android.widget.ViewFlow; | ||
|
||
import android.app.Activity; | ||
import android.os.Bundle; | ||
|
||
public class AsyncDataFlowExample extends Activity { | ||
|
||
private ViewFlow viewFlow; | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setTitle(R.string.async_title); | ||
setContentView(R.layout.title_layout); | ||
|
||
viewFlow = (ViewFlow) findViewById(R.id.viewflow); | ||
AsyncAdapter adapter = new AsyncAdapter(this); | ||
viewFlow.setAdapter(adapter, adapter.getTodayId()); | ||
|
||
TitleFlowIndicator indicator = (TitleFlowIndicator) findViewById(R.id.viewflowindic); | ||
indicator.setTitleProvider(adapter); | ||
|
||
viewFlow.setFlowIndicator(indicator); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters