WheelView

文章参考
文章参考

依赖

//wheelView
implementation 'com.contrarywind:wheelview:4.0.9'
//ArrayWheelAdapter
implementation 'com.contrarywind:Android-PickerView:4.1.9'

1、一级demo

在这里插入图片描述
layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <com.contrarywind.view.WheelView
        android:id="@+id/id_wheel_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Activity

public class WheelViewActivity extends AppCompatActivity {
    public static final String TAG = "TAG_ZLZ";

    @BindView(R.id.id_wheel_view)
    WheelView wheelView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_wheel_view);
        ButterKnife.bind(this);

        initWheelView();
    }


    /**
     * 示例 1
     */
    private void initWheelView() {

        //初始化数据
        final List<String> countryList = new ArrayList<>();
        countryList.add("中国");
        countryList.add("美国");
        countryList.add("俄罗斯");
        countryList.add("日本");
        countryList.add("印度");
        countryList.add("英国");
        countryList.add("法国");
        countryList.add("德国");

        //配置wheelView属性
        ArrayWheelAdapter adapter = new ArrayWheelAdapter(countryList);
        wheelView.setAdapter(adapter);
        wheelView.setCyclic(false); //取消循环显示数据
        wheelView.setCurrentItem(0); //当前显示第一条
        wheelView.setItemsVisibleCount(4); //可见范围为4个
        wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                Log.i(TAG, countryList.get(index));
            }
        });
    }
}

2、二级demo

在这里插入图片描述
layout

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@android:color/black"
    tools:context=".wheelview.WheelViewActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="@android:color/white"
        android:orientation="horizontal"
        android:paddingTop="30dp">

        <com.contrarywind.view.WheelView
            android:id="@+id/id_wheel_view_country"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <com.contrarywind.view.WheelView
            android:id="@+id/id_wheel_view_city"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />


    </LinearLayout>


    <TextView
        android:id="@+id/id_tv_res"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:gravity="center_horizontal"
        android:text="res"
        android:textColor="@android:color/white"
        android:textSize="30dp" />

</RelativeLayout>

Activity

public class WheelViewActivity extends AppCompatActivity {


    @BindView(R.id.id_wheel_view_country)
    WheelView wheelViewCountry;
    @BindView(R.id.id_wheel_view_city)
    WheelView wheelViewCity;
    @BindView(R.id.id_tv_res)
    TextView tvResult;

    private List<String> countryList = null;
    private List<List<String>> cityList = null;
    private ArrayWheelAdapter countryAdapter = null;
    private ArrayWheelAdapter cityAdapter = null;

    private int countryNum = 0;
    private int cityNum = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_wheel_views);
        ButterKnife.bind(this);

        //国家
        initCountryWheelView();
        //城市
        initCityWheelView();
    }

    /**
     * 国家
     */
    private void initCountryWheelView() {
        countryList = new ArrayList<>();
        countryList.add("中国");
        countryList.add("美国");
        countryList.add("日本");


        countryAdapter = new ArrayWheelAdapter(countryList);
        wheelViewCountry.setAdapter(countryAdapter);
        wheelViewCountry.setCyclic(false); //取消循环显示数据
        wheelViewCountry.setCurrentItem(0); //当前显示第一条
        wheelViewCountry.setItemsVisibleCount(2); //可见范围为2个
        wheelViewCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onItemSelected(int index) {
                countryNum = index;
                if (wheelViewCity != null) {
                    cityAdapter = new ArrayWheelAdapter(cityList.get(index));
                    wheelViewCity.setAdapter(cityAdapter);
                    wheelViewCity.setCurrentItem(0);
                    cityNum = 0;
                    tvResult.setText("country : " + countryNum + ", " + "city : " + cityNum);
                }
            }
        });
    }


    /**
     * 城市
     */
    private void initCityWheelView() {
        List<String> cnCityList = new ArrayList<>();
        cnCityList.add("北京");
        cnCityList.add("上海");
        cnCityList.add("广州");
        cnCityList.add("深圳");

        List<String> usCityList = new ArrayList<>();
        usCityList.add("华盛顿");
        usCityList.add("纽约");
        usCityList.add("底特律");

        List<String> jpCityList = new ArrayList<>();
        jpCityList.add("东京");
        jpCityList.add("秋叶原");
        jpCityList.add("横滨");
        jpCityList.add("大版");

        cityList = new ArrayList<>();
        cityList.add(cnCityList);
        cityList.add(usCityList);
        cityList.add(jpCityList);


        cityAdapter = new ArrayWheelAdapter(cityList.get(0));
        wheelViewCity.setAdapter(cityAdapter);
        wheelViewCity.setCyclic(false); //取消循环显示数据
        wheelViewCity.setCurrentItem(0); //当前显示第一条
        wheelViewCity.setItemsVisibleCount(2); //可见范围为2个
        wheelViewCity.setOnItemSelectedListener(new OnItemSelectedListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onItemSelected(int index) {
                cityNum = index;
                tvResult.setText("country : " + countryNum + ", " + "city : " + cityNum);
            }
        });
    }
}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

KillerNoBlood

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

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

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

打赏作者

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

抵扣说明:

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

余额充值