Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

android - How to make a Wheel picker

I am trying to make a wheel picker like this I have tried downloading this project but the only files included in the .zip download are wheel-demo.apk and notes.txt. Notes.txt does not have any instructions on how to use this file with android studio. I found a post that suggested using ListViews to create the same effect. Rather than making my own I spent another day searching the internet and I found source code here but when I imported the files into my project the IDE showed dozens of errors. Through trial and error I managed to fix all but 3 errors. Pretty sure I have put the relevant code below
MainActivity.java:

OnWheelScrollListener scrolledListener = new OnWheelScrollListener()
{
    public void onScrollingStarted(WheelView wheel)
    {
        wheelScrolled = true;// "Cannot resolve symbol wheelScrolled
    }

    public void onScrollingFinished(WheelView wheel)
    {
        wheelScrolled = false;// "Cannot resolve symbol wheelScrolled
        updateStatus();
    }
};

// Wheel changed listener
private final OnWheelChangedListener changedListener = new OnWheelChangedListener()
{
    public void onChanged(WheelView wheel, int oldValue, int newValue)
    {
        if (!wheelScrolled)// "Cannot resolve symbol wheelScrolled
        {
            updateStatus();
        }
    }
};
private void initWheel1(int id)
{
    WheelView wheel = (WheelView) findViewById(id);
    wheel.setAdapter(new ArrayWheelAdapter<String>(wheelMenu1)); //cannot resolve method 'setAdapter(com.projectname.ArrayWheelAdapter<Java.lang.String>)
    wheel.setVisibleItems(2);
    wheel.setCurrentItem(0);
    wheel.addChangingListener(changedListener);
    wheel.addScrollingListener(scrolledListener);
}

OnWheelScrollListener.java:

public interface OnWheelScrollListener {
/**
 * Callback method to be invoked when scrolling started.
 * @param wheel the wheel view whose state has changed.
 */
void onScrollingStarted(WheelView wheel);

/**
 * Callback method to be invoked when scrolling ended.
 * @param wheel the wheel view whose state has changed.
 */
void onScrollingFinished(WheelView wheel);}

OnWheelChangedListener.java:

    public interface OnWheelChangedListener {
    /**
     * Callback method to be invoked when current item changed
     * @param wheel the wheel view whose state has changed
     * @param oldValue the old value of current item
     * @param newValue the new value of current item
     */
    void onChanged(WheelView wheel, int oldValue, int newValue);
}

ArrayWheelAdapter.java

    public class ArrayWheelAdapter<T> extends AbstractWheelTextAdapter {

    // items
    private T items[];

    /**
     * Constructor
     * @param context the current context
     * @param items the items
     */
    public ArrayWheelAdapter(Context context, T items[]) {
        super(context);

        //setEmptyItemResource(TEXT_VIEW_ITEM_RESOURCE);
        this.items = items;
    }

    @Override
    public CharSequence getItemText(int index) {
        if (index >= 0 && index < items.length) {
            T item = items[index];
            if (item instanceof CharSequence) {
                return (CharSequence) item;
            }
            return item.toString();
        }
        return null;
    }

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

All 3 of the .java files have been added to the import list in MainActivity thinking it might solve the issue, it didn't. Thank you for the advice so far.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can just use the default NumberPicker included in Android since API 11. You can display any Strings you want by using NumberPicker#setDisplayedValues.

XML:

<NumberPicker
        android:id="@+id/number_picker"
        android:layout_width="match_parent"
        android:layout_height="180dp" />

Java:

    String[] data = new String[]{"Berlin", "Moscow", "Tokyo", "Paris"};
    picker.setMinValue(0);
    picker.setMaxValue(data.length-1);
    picker.setDisplayedValues(data);

Screenshot:

NumberPicker displaying Cities


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...