Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
#32 Add the example of asynchronous data loading into ViewFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
Voldemar123 committed Aug 18, 2011
1 parent b4063cd commit 063b333
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 1 deletion.
4 changes: 4 additions & 0 deletions viewflow-example/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
android:configChanges="orientation">

</activity>

<activity android:label="@string/app_name"
android:configChanges="orientation" android:name=".AsyncDataFlowExample">
</activity>

<activity android:name="ViewFlowExample" android:label="@string/app_name">
<intent-filter>
Expand Down
21 changes: 21 additions & 0 deletions viewflow-example/res/layout/day_view.xml
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>
1 change: 1 addition & 0 deletions viewflow-example/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<string name="title_title">TitleFlowIndicator</string>
<string name="circle_title">CircleFlowIndicator</string>
<string name="diff_title">DifferentViewFlow</string>
<string name="async_title">AsyncDataLoading</string>
</resources>
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();
}

}

}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.menu);
String[] listeStrings = { "Circle indicator...", "Title indicator...", "Different Views..." };
String[] listeStrings = { "Circle indicator...", "Title indicator...", "Different Views...", "Async Data Loading..." };
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listeStrings));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
Expand All @@ -48,6 +48,10 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
break;
case 2:
startActivity(new Intent(ViewFlowExample.this, DiffViewFlowExample.class));
break;
case 3:
startActivity(new Intent(ViewFlowExample.this, AsyncDataFlowExample.class));
break;
}
}
});
Expand Down

0 comments on commit 063b333

Please sign in to comment.