wheel的使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wheel的使用相关的知识,希望对你有一定的参考价值。

1、首先导入wheel库或者将wheel文件拷到项目中

主类:

package com.example.view;


import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.ViewGroup;

import android.widget.TextView;

import antistatic.spinnerwheel.AbstractWheel;

import antistatic.spinnerwheel.OnWheelClickedListener;

import antistatic.spinnerwheel.OnWheelScrollListener;

import antistatic.spinnerwheel.WheelView;

import antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter;


public class MainActivity2 extends Activity {

private String TAG = "MainActivity2";

private WheelView wheelView1;

private MyAdapter viewAdapter;

private int mChoosedIndex;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_activity2);

wheelView1 = (WheelView) findViewById(R.id.wheelView1);


String[] arrMonths = new String[7];

for (int i = 1; i < arrMonths.length + 1; i++) {

arrMonths[i - 1] = i + "个月";

}

viewAdapter = new MyAdapter(this, arrMonths);//设置适配器

wheelView1.setViewAdapter(viewAdapter);

wheelView1.setDrawLine(false);//设置是否在前景图上下画线

wheelView1.setWheelForeground(R.drawable.wifi_notify);//设置前景图片

wheelView1.setDrawShadows(false);//设置前景是否画阴影

scrollToMiddle();//默认滑到中间一栏


wheelView1.addClickingListener(click);//设置点击事件


wheelView1.addScrollingListener(scrollListener);//设置滑动事件

}


OnWheelClickedListener click = new OnWheelClickedListener() {

public void onItemClicked(WheelView wheel, int itemIndex) {

Log.e(TAG, ">>>>onItemClicked=WheelView>>"+itemIndex);

wheel.setCurrentItem(itemIndex, true);

//  viewAdapter .notifyDataChangedEvent();//刷新getItem

}


@Override

public void onItemClicked(AbstractWheel wheel, int itemIndex) {

// TODO Auto-generated method stub

Log.e(TAG, ">>>>onItemClicked=AbstractWheel>>"+itemIndex);

//  viewAdapter .notifyDataChangedEvent();//刷新getItem

}

};


OnWheelScrollListener scrollListener = new OnWheelScrollListener() {


@Override

public void onScrollingStarted(AbstractWheel wheel) {

// TODO Auto-generated method stub

Log.e(TAG, ">>>>onScrollingStarted=AbstractWheel>>");

}


@Override

public void onScrollingFinished(AbstractWheel wheel) {

// TODO Auto-generated method stub

Log.e(TAG, ">>>>onScrollingFinished=AbstractWheel>>");

//  viewAdapter .notifyDataChangedEvent();//刷新getItem

}


@Override

public void onScrollingStarted(WheelView wheel) {

// TODO Auto-generated method stub

Log.e(TAG, ">>>>onScrollingStarted=WheelView>>");


}


@Override

public void onScrollingFinished(WheelView wheel) {

// TODO Auto-generated method stub


                      //  viewAdapter .notifyDataChangedEvent();//刷新getItem

Log.e(TAG, ">>>>onScrollingFinished=WheelView>>");

}


};


/**

* @Title: scrollToLast

* @Description: 滑到滚轮中间

* @throws

*/

private void scrollToMiddle() {

wheelView1.stopScrolling();

int middleIndex = (wheelView1.getViewAdapter().getItemsCount() + 1) / 2;

mChoosedIndex = middleIndex;

Log.e("", ">>>>>" + middleIndex);

wheelView1.setCurrentItem(middleIndex);

}


private class MyAdapter extends AbstractWheelTextAdapter {

String[] arrMonths;


protected MyAdapter(Context context, String[] arrMonths) {

super(context, R.layout.wheel_item, NO_RESOURCE);

this.arrMonths = arrMonths;

}


@Override

public int getItemsCount() {

return arrMonths.length;

}


@Override

public View getItem(int index, View convertView, ViewGroup parent) {

View view = super.getItem(index, convertView, parent);

TextView tvItem = (TextView) view.findViewById(R.id.tv_item);

                        //在适配器中改变item的样式

// if (index == mChoosedIndex) {

// tvItem.setTextColor(getResources().getColor(R.color.lightblue));

// } else {

// tvItem.setTextColor(getResources().getColor(

// R.color.gray_info_text));

// }

tvItem.setText(arrMonths[index]);

return view;

}


@Override

protected CharSequence getItemText(int index) {

return arrMonths[index];

}


public void notifyDataChangedEvent() {

super.notifyDataChangedEvent();

}


@Override

public void notifyDataInvalidatedEvent() {

super.notifyDataInvalidatedEvent();

}

}

}

 wheel_item文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >


    <TextView

        android:id="@+id/tv_item"

        android:layout_width="80dp"

        android:layout_height="40dp"

        android:textColor="#ffffff"

        android:textSize="20sp"

        android:gravity="center"

        android:layout_gravity="center"

        android:background="#000000"

        android:text="tv" />


</LinearLayout>


布局文件:activity_main_activity2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity2" >


    <antistatic.spinnerwheel.WheelView

        android:id="@+id/wheelView1"

        android:layout_width="150dp"

        android:layout_height="200dp"

        android:layout_alignParentTop="true"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="174dp"

        android:background="#ff00ff" />


</RelativeLayout>


以上是关于wheel的使用的主要内容,如果未能解决你的问题,请参考以下文章

Python 小知识,wheel 文件的说明与使用

检查是否使用PAM认证模块禁止wheel组之外的用户su为root

使用 setup.py 和 wheel 安装包依赖项

使用pip命令安装库时提示Could not build wheels for six, since package 'wheel' is not installed

python pip安装使用wheel文件无法正常工作

Linux禁止非WHEEL用户使用SU命令