android——下拉选择框(自定义spinner)

相信大家都有遇到下拉选择这个功能,接下来我把我学习的给大家分享一下。

一、导入依赖

    implementation 'com.github.arcadefire:nice-spinner:1.4.4'

二、具体应用

1、接来下就是创建一个xml布局了fragment_restaurant,我个人比较喜欢用constraintlayout约束性布局,我觉得还挺好用的。

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintTop_toTopOf="parent">

        <org.angmarch.views.NiceSpinner
            android:id="@+id/spinner_fruit"
            android:layout_width="260dp"
            android:layout_height="80dp"
            android:textColor="@color/black"
            android:textSize="36sp"
            app:layout_constraintRight_toLeftOf="@id/spinner_food"
            app:layout_constraintTop_toTopOf="parent" />

        <org.angmarch.views.NiceSpinner
            android:id="@+id/spinner_food"
            android:layout_width="420dp"
            android:layout_height="80dp"
            android:layout_marginLeft="40dp"
            android:textColor="@color/black"
            android:textSize="36sp"
            app:layout_constraintLeft_toRightOf="@id/spinner_fruit"
            app:layout_constraintTop_toTopOf="@id/spinner_fruit" />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

二、布局文件写完,就该写具体的应用逻辑了,我们新建一个activity或者一个fragment,这看你自己的使用场景。

我这里就以创建fragment为例了,创建一个RestaurantFragment。具体使用就是将你的列表清单即一个list绑定到控件中,没错,就这么easy,嘻嘻!

public class RestaurantFragment extends Fragment{

    private List<String> fruitlist = new ArrayList<>();
    private List<String> foodlist = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_restaurant, container, false);
        fruitlist.add("苹果");
        fruitlist.add("香蕉");
        fruitlist.add("西瓜");
        fruitlist.add("葡萄");
        
        foodlist.add("红烧肉");
        foodlist.add("可乐鸡翅");
        foodlist.add("奥尔良烤鸡");


        //找到控件
        NiceSpinner spinnerFruit = (NiceSpinner) view.findViewById(R.id.spinner_fruit);
        NiceSpinner spinnerFood = (NiceSpinner) view.findViewById(R.id.spinner_food);

        //设置背景,自定义的背景
        spinnerFood.setBackgroundResource(R.drawable.style_item);
        spinnerFruit.setBackgroundResource(R.drawable.style_item);

        //将数据绑定
        spinnerFruit.attachDataSource(fruitlist);
        spinnerFood.attachDataSource(foodlist);

        spinnerFruit.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "你选择标签" + fruitlist.get(position), Toast.LENGTH_SHORT).show();
            }
        });

        spinnerFood.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "你选择标签" + foodlist.get(position), Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }
}

三、第三部就是在drawable里面新建一个xml样式了style_item

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="300dp" android:height="72dp">
        <shape android:shape="rectangle">
            <solid android:color="#ffffffff" />
            <corners android:radius="6dp" />
        </shape>
    </item>
</selector>

ok,这样大功告成了,运行一下看一下吧!注意,里面的list换成你自己的实际数据就行,布局样式也可以换成你自己的。

上一篇:2021-05-09


下一篇:VUE:图片上传