Android Open Source - android-weather-demo-application View Pager Indicator View






From Project

Back to project page android-weather-demo-application.

License

The source code is released under:

GNU General Public License

If you think the Android project android-weather-demo-application listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.openweathermap.view;
/*  w  w w. j  a  v  a 2s.  c o  m*/
import java.util.ArrayList;

import org.openweathermap.sql.model.WeatherModel;

import android.content.Context;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.LinearLayout;

/**
 * 
 * @author samkirton
 */
public class ViewPagerIndicatorView extends LinearLayout implements OnPageChangeListener, OnClickListener, AnimationListener {
  private WeatherModel[] mWeatherModelArray;
  private Context mContext;
  private ArrayList<DateDisplayView> mDateDisplayViewCollection;
  private PagerPositionCallback mPagerPositionCallback;
  private boolean mFirstLoad = true;
  
  public interface PagerPositionCallback {
    public void onSetPosition(int position);
  }
  
  public void setPagerPositionCallback(PagerPositionCallback pagerPositionCallback) {
    mPagerPositionCallback = pagerPositionCallback;
  }
  
  public ViewPagerIndicatorView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    setVisibility(GONE);
  }
  
  /**
   * Populate the indicator view with a list of day data
   * @param  weatherModelArray  The data to populate the indicator with
   */
  public void init(WeatherModel[] weatherModelArray) {
    mWeatherModelArray = weatherModelArray;
    mDateDisplayViewCollection = new ArrayList<DateDisplayView>();
    
    this.removeAllViews();
    
    for (int i = 0; i < mWeatherModelArray.length; i++) {
      WeatherModel weatherModel = mWeatherModelArray[i];
      long timestamp = weatherModel.getTimestamp();
      
      DateDisplayView dateDisplayView = new DateDisplayView(mContext);
      dateDisplayView.init(timestamp,i);
      dateDisplayView.setOnClickListener(this);
      this.addView(dateDisplayView);
      
      // add a reference of the view to the collection so its selection state
      // can be changed onPageSelected()
      mDateDisplayViewCollection.add(dateDisplayView);
      
      // select the first date as selected
      if (i == 0) 
        dateDisplayView.selected();
    }
    
    if (mFirstLoad) {
      mFirstLoad = false;
      showView();
    } else {
      hideView();
    }
  }
  
  /**
   * Fade the layout in
   */
  public void showView() {
    AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(500);
    animation.setRepeatCount(0);
    this.startAnimation(animation);
    this.setVisibility(VISIBLE);
  }
  
  /**
   * Hides the view and when the animation ends it shows the view
   */
  public void hideView() {
    AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
    animation.setDuration(500);
    animation.setRepeatCount(0);
    animation.setAnimationListener(this);
    this.startAnimation(animation);
  }
  
  @Override
  public void onClick(View v) {
    DateDisplayView dateDisplayView = (DateDisplayView)v;
    int position = dateDisplayView.getPosition();
    mPagerPositionCallback.onSetPosition(position);
    onPageSelected(position);
  }

  @Override
  public void onPageSelected(int position) {
    // unselect all views
    for (DateDisplayView dateDisplayView : mDateDisplayViewCollection) 
      dateDisplayView.unSelected();
    
    // select the current selected view
    mDateDisplayViewCollection.get(position).selected();
  }
  
  @Override
  public void onPageScrollStateChanged(int arg0) { }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) { }

  @Override
  public void onAnimationEnd(Animation animation) {
    showView();
  }

  @Override
  public void onAnimationRepeat(Animation animation) { }

  @Override
  public void onAnimationStart(Animation animation) { }
}




Java Source Code List

org.openweathermap.WeatherApplication.java
org.openweathermap.activity.MainActivity.java
org.openweathermap.activity.base.BaseActivity.java
org.openweathermap.adapter.CityAdapter.java
org.openweathermap.adapter.ForecastAdapter.java
org.openweathermap.asynctask.GetCitiesAsyncTask.java
org.openweathermap.asynctask.GetLastCityAsyncTask.java
org.openweathermap.asynctask.SaveForecastAsyncTask.java
org.openweathermap.asynctask.SearchCitiesAsyncTask.java
org.openweathermap.asynctask.base.BaseAsyncTask.java
org.openweathermap.asynctask.base.BaseResponse.java
org.openweathermap.asynctask.base.IParam.java
org.openweathermap.asynctask.param.SaveForecastParam.java
org.openweathermap.asynctask.param.SearchCitiesParam.java
org.openweathermap.asynctask.response.GetCitiesResponse.java
org.openweathermap.asynctask.response.GetLastCityResponse.java
org.openweathermap.asynctask.response.SaveForecastResponse.java
org.openweathermap.dto.CityDTO.java
org.openweathermap.dto.CoordDTO.java
org.openweathermap.dto.DayDTO.java
org.openweathermap.dto.ResultDTO.java
org.openweathermap.dto.TempDTO.java
org.openweathermap.dto.WeatherDTO.java
org.openweathermap.dto.base.IDTO.java
org.openweathermap.fragment.ForecastFragment.java
org.openweathermap.fragment.ModalDialogFragment.java
org.openweathermap.fragment.SelectCountryFragment.java
org.openweathermap.sql.SQLInitProvider.java
org.openweathermap.sql.model.CityModel.java
org.openweathermap.sql.model.IconModel.java
org.openweathermap.sql.model.WeatherModel.java
org.openweathermap.utils.DateHelper.java
org.openweathermap.utils.RESTProvider.java
org.openweathermap.utils.ReflectionHelper.java
org.openweathermap.utils.ResourceHelper.java
org.openweathermap.utils.StringHelper.java
org.openweathermap.view.CityListItemView.java
org.openweathermap.view.DateDisplayView.java
org.openweathermap.view.ViewPagerFade.java
org.openweathermap.view.ViewPagerIndicatorView.java
org.openweathermap.view.WeatherDataRowView.java
org.openweathermap.volley.callback.VolleyResponseCallback.java
org.openweathermap.volley.provider.VolleyImageLoader.java
org.openweathermap.volley.provider.VolleyRequest.java