BVB Source Codes

android-viewflow Show AsyncAdapter.java Source code

Return Download android-viewflow: download AsyncAdapter.java Source code - Download android-viewflow Source code - Type:.java
  1. package org.taptwo.android.widget.viewflow.example;
  2.  
  3. import java.text.DateFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Calendar;
  6. import java.util.Date;
  7.  
  8. import org.taptwo.android.widget.TitleProvider;
  9. import org.taptwo.android.widget.viewflow.example.R;
  10.  
  11. import android.content.Context;
  12. import android.os.AsyncTask;
  13. import android.view.LayoutInflater;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.BaseAdapter;
  17. import android.widget.ProgressBar;
  18. import android.widget.TextView;
  19.  
  20.  
  21. public class AsyncAdapter extends BaseAdapter implements TitleProvider {
  22.  
  23.         private LayoutInflater mInflater;
  24.        
  25.         private static final DateFormat dfTitle = new SimpleDateFormat("E, dd MMM");
  26.        
  27.         private static final int daysDepth = 10;
  28.         private static final int daysSize = daysDepth * 2 + 1;
  29.        
  30.         private static Date[] dates = new Date[ daysSize ];
  31.         private static String[] content = new String[ daysSize ];
  32.        
  33.        
  34.         private class ViewHolder {
  35.                 ProgressBar mProgressBar;
  36.                 View mContent;
  37.                 TextView mDate;
  38.         }
  39.        
  40.        
  41.         public AsyncAdapter(Context context) {
  42.                 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  43.                 prepareDates();
  44.         }
  45.        
  46.         @Override
  47.         public String getItem(int position) {
  48.                 return content[position];
  49.         }
  50.  
  51.         @Override
  52.         public long getItemId(int position) {
  53.                 return position;
  54.         }
  55.  
  56.         @Override
  57.         public View getView(int position, View convertView, ViewGroup parent) {
  58.                 return drawView(position, convertView);
  59.         }
  60.  
  61.         private View drawView(int position, View view) {
  62.                 ViewHolder holder = null;
  63.                
  64.                 if(view == null) {
  65.                         view = mInflater.inflate(R.layout.day_view, null);
  66.                        
  67.                         holder = new ViewHolder();
  68.  
  69.                         holder.mProgressBar = (ProgressBar) view.findViewById(R.id.progress);
  70.                         holder.mDate = (TextView) view.findViewById(R.id.date);
  71.                         holder.mContent = (View) view.findViewById(R.id.content);
  72.                        
  73.                         view.setTag(holder);
  74.                 } else {
  75.                         holder = (ViewHolder) view.getTag();
  76.                 }              
  77.  
  78.  
  79.                 final String o = getItem(position);
  80.                 if (o != null) {
  81.                         holder.mProgressBar.setVisibility(View.GONE);
  82.                         holder.mDate.setText(o);
  83.                         holder.mContent.setVisibility(View.VISIBLE);
  84.                 }
  85.                 else {
  86.                         new LoadContentTask().execute(position, view);
  87.  
  88.                         holder.mContent.setVisibility(View.GONE);
  89.                         holder.mProgressBar.setVisibility(View.VISIBLE);
  90.                 }
  91.        
  92.                 return view;
  93.         }
  94.  
  95.         @Override
  96.         public String getTitle(int position) {
  97.                 return dfTitle.format( dates[position] );
  98.         }
  99.  
  100.         @Override
  101.         public int getCount() {
  102.                 return dates.length;
  103.         }
  104.  
  105.         public int getTodayId() {
  106.                 return daysDepth;
  107.         }
  108.  
  109.         public Date getTodayDate() {
  110.                 return dates[daysDepth];
  111.         }
  112.        
  113.         /**
  114.          * Prepare dates for navigation, to past and to future
  115.          */
  116.         private void prepareDates() {
  117.                 Date today = new Date();
  118.  
  119.                 Calendar calPast = Calendar.getInstance();
  120.                 Calendar calFuture = Calendar.getInstance();
  121.  
  122.                 calPast.setTime(today);
  123.                 calFuture.setTime(today);
  124.  
  125.                 dates[ daysDepth ] = calPast.getTime();
  126.                 for (int i = 1; i <= daysDepth; i++) {
  127.                         calPast.add( Calendar.DATE, -1 );
  128.                         dates[ daysDepth - i ] = calPast.getTime();
  129.  
  130.                         calFuture.add( Calendar.DATE, 1 );
  131.                         dates[ daysDepth + i ] = calFuture.getTime();
  132.                 }
  133.         }
  134.        
  135.        
  136.         private class LoadContentTask extends AsyncTask<Object, Object, Object> {
  137.                
  138.                 private Integer position;
  139.                 private View view;
  140.                
  141.                 @Override
  142.                 protected Object doInBackground(Object... arg) {
  143.                         position = (Integer) arg[0];
  144.                         view = (View) arg[1];
  145.  
  146. // long-term task is here                      
  147.                         try {
  148.                                 Thread.sleep(3000); // do nothing for 3000 miliseconds (3 second)
  149.                         } catch (InterruptedException e) {
  150.                                 e.printStackTrace();
  151.                         }
  152.                        
  153.                         return getTitle(position);
  154.                 }
  155.  
  156.                 protected void onPostExecute(Object result) {
  157. // process result        
  158.                         content[position] = (String) result;
  159.                        
  160.                 drawView(position, view);
  161.  
  162.                 view.postInvalidate();
  163.              }
  164.  
  165.         }      
  166.  
  167. }
  168.  
downloadAsyncAdapter.java Source code - Download android-viewflow Source code
Related Source Codes/Software:
onionshare - Securely and anonymously share a file of any size ... 2017-04-16
Android-ItemTouchHelper-Demo - Basic example of using ItemTouchHelper to add drag... 2017-04-16
later - A javascript library for defining recurring schedu... 2017-04-16
x64dbg - An open-source x64/x32 debugger for windows. ... 2017-04-16
pencil - Multiplatform GUI Prototyping/Wireframing 2017-04-16
css-in-js - React: CSS in JS techniques comparison. 2017-04-16
st2 - StackStorm (aka IFTTT for Ops) is event-driven aut... 2017-04-16
WNXHuntForCity - City find By Objective - C 2017-04-16
tmate - Instant Terminal Sharing ht... 2017-04-16
dat.gui - dat.gui is a lightweight controller library for Ja... 2017-04-16
EventStore - The open-source, functional database with Complex ... 2017-04-23
screencat - 2017-04-23
viz.js - A hack to put Graphviz on the web. ... 2017-04-23
JKeyboardPanelSwitch - For resolve the layout conflict when keybord & amp... 2017-04-23
mama2 - Mother plan - all firewood high flame 2017-04-23
BlurEffectForAndroidDesign - Sample to show how to implement blur graphical tri... 2017-04-23
sphinx_rtd_theme - Sphinx theme for readthedocs.org 2017-04-23
rouge - A pure-ruby code highlighter that is compatible wi... 2017-04-23
spring-security-oauth - Support for adding OAuth1(a) and OAuth2 features (... 2017-04-23
Toucan - Fabulous Image Processing in Swift 2017-04-23

 Back to top