【Android -- 开源库】NiceSpinner 的基本使用

在这里插入图片描述

如今,好多 App 都有下拉选择框的需求,今天这个第三方下拉框可以满足我们大部分的需求噢!
GitHub 地址: nice-spinner

效果图

这里写图片描述

使用

1. 在 build.gradle 文件添加:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    ...
    compile 'com.github.arcadefire:nice-spinner:1.3.1'
    ...
}

2. 布局文件中使用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.gyq.nicespinner.MainActivity">

    <org.angmarch.views.NiceSpinner
        android:id="@+id/nice_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:arrowTint="@color/colorPrimary"
        app:textTint="@color/colorAccent"/>


</LinearLayout>

3. java 代码中使用:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NiceSpinner niceSpinner = (NiceSpinner)findViewById(R.id.nice_spinner);
        List<String> dataList = new ArrayList<>();
        dataList.add("android");
        dataList.add("java");
        dataList.add("ios");
        dataList.add("php");
        dataList.add("kotlin");

        niceSpinner.attachDataSource(dataList);

        niceSpinner.addOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0 :
                        ToastUtil.showToast("android");
                        break;
                    case 1 :
                        ToastUtil.showToast("java");
                        break;
                    case 2 :
                        ToastUtil.showToast("ios");
                        break;
                    case 3 :
                        ToastUtil.showToast("php");
                        break;
                    case 4 :
                        ToastUtil.showToast("kotlin");
                        break;
                }
            }
        });
    }
}

4. ToastUtil.java

/**
 * Created by gyq on 2017/12/28 11:54
 */

public class ToastUtil {

        public static void showToast(String msg){
            if(isMainThread()){
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_SHORT).show();
            }else{
                Looper.prepare();
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        }

        public static void showLongToast(String msg){
            if(isMainThread()){
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_LONG).show();
            }else{
                Looper.prepare();
                Toast.makeText(MyApplication.getContext(),msg,Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }

        public static boolean isMainThread() {
            return Looper.getMainLooper().getThread() == Thread.currentThread();
        }

}

5. MyApplication.java

/**
 * Created by gyq on 2017/12/28 11:54
 */

public class MyApplication extends Application {
    private static Context appContext;

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this.getApplicationContext();
    }

    public static Context getContext() {
        return appContext;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gyq.nicespinner">

    <application
        **android:name=".base.MyApplication"**
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值