Android Open Source - android-wheel Cities Activity






From Project

Back to project page android-wheel.

License

The source code is released under:

Apache License

If you think the Android project android-wheel 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 kankan.wheel.demo;
/*w w w .j  a v a  2s .  c  om*/
import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;
import kankan.wheel.widget.adapters.ArrayWheelAdapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class CitiesActivity extends Activity {
    // Scrolling flag
    private boolean scrolling = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.cities_layout);

        final WheelView country = (WheelView) findViewById(kankan.wheel.demo.R.id.country);
        country.setVisibleItems(3);
        country.setViewAdapter(new CountryAdapter(this));

        final String cities[][] = new String[][] {
            new String[] {"New York", "Washington", "Chicago", "Atlanta", "Orlando"},
            new String[] {"Ottawa", "Vancouver", "Toronto", "Windsor", "Montreal"},
            new String[] {"Kiev", "Dnipro", "Lviv", "Kharkiv"},
            new String[] {"Paris", "Bordeaux"},
            };

        final WheelView city = (WheelView) findViewById(R.id.city);
        city.setVisibleItems(5);

        country.addChangingListener(new OnWheelChangedListener() {
      public void onChanged(WheelView wheel, int oldValue, int newValue) {
          if (!scrolling) {
              updateCities(city, cities, newValue);
          }
      }
    });

        country.addScrollingListener( new OnWheelScrollListener() {
            public void onScrollingStarted(WheelView wheel) {
                scrolling = true;
            }
            public void onScrollingFinished(WheelView wheel) {
                scrolling = false;
                updateCities(city, cities, country.getCurrentItem());
            }
        });

        country.setCurrentItem(1);
    }

    /**
     * Updates the city wheel
     */
    private void updateCities(WheelView city, String cities[][], int index) {
        ArrayWheelAdapter<String> adapter =
            new ArrayWheelAdapter<String>(this, cities[index]);
        adapter.setTextSize(18);
        city.setViewAdapter(adapter);
        city.setCurrentItem(cities[index].length / 2);
    }

    /**
     * Adapter for countries
     */
    private class CountryAdapter extends AbstractWheelTextAdapter {
        // Countries names
        private String countries[] =
            new String[] {"USA", "Canada", "Ukraine", "France"};
        // Countries flags
        private int flags[] =
            new int[] {R.drawable.usa, R.drawable.canada, R.drawable.ukraine, R.drawable.france};

        /**
         * Constructor
         */
        protected CountryAdapter(Context context) {
            super(context, R.layout.country_layout, NO_RESOURCE);

            setItemTextResource(R.id.country_name);
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            ImageView img = (ImageView) view.findViewById(R.id.flag);
            img.setImageResource(flags[index]);
            return view;
        }

        @Override
        public int getItemsCount() {
            return countries.length;
        }

        @Override
        protected CharSequence getItemText(int index) {
            return countries[index];
        }
    }
}




Java Source Code List

kankan.wheel.demo.CitiesActivity.java
kankan.wheel.demo.DateActivity.java
kankan.wheel.demo.PasswActivity.java
kankan.wheel.demo.SlotMachineActivity.java
kankan.wheel.demo.SpeedActivity.java
kankan.wheel.demo.Time2Activity.java
kankan.wheel.demo.TimeActivity.java
kankan.wheel.demo.WheelDemo.java
kankan.wheel.widget.ItemsRange.java
kankan.wheel.widget.OnWheelChangedListener.java
kankan.wheel.widget.OnWheelClickedListener.java
kankan.wheel.widget.OnWheelScrollListener.java
kankan.wheel.widget.WheelAdapter.java
kankan.wheel.widget.WheelRecycle.java
kankan.wheel.widget.WheelScroller.java
kankan.wheel.widget.WheelView.java
kankan.wheel.widget.adapters.AbstractWheelAdapter.java
kankan.wheel.widget.adapters.AbstractWheelTextAdapter.java
kankan.wheel.widget.adapters.AdapterWheel.java
kankan.wheel.widget.adapters.ArrayWheelAdapter.java
kankan.wheel.widget.adapters.NumericWheelAdapter.java
kankan.wheel.widget.adapters.WheelViewAdapter.java