不断学习,做更好的自己!💪

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

效果图

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

使用

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>