这两天项目中有用到一个国家的滚动选择器的效果,我就研究了下。效果图如下
首先用到的第三方控件,地址:https://github.com/weidongjian/androidWheelView/,这个第三方控件有个限制,传入的数据的数量必须在9条以上,不然就会报错。还有这个项目默认自带能记住上次的选择项。
首先,把项目下下来,然后找到androidWheelView-master\app\src\main\java\com\weidongjian\meitu\wheelviewdemo\view,在自己项目中新建一个文件夹,把这些文件都拷贝过去,
然后我们首先先建一个布局:代码如下:
activity_wheel
<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:id="@+id/ll_main"
>
<LinearLayout
android:id="@+id/ll_country"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:background="@drawable/rectangle_stroke6_solid1_corner5"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_country"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:paddingLeft="16dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:textColor="#000000"
android:textSize="15sp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:src="@drawable/ic_choose_address"/>
</LinearLayout>
</LinearLayout>
用到的背景文件
rectangle_stroke6_solid1_corner5
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#e94737"/>
<corners android:radius="5dp"/>
<solid android:color="#FFFFFF"/>
</shape>
Activty文件
public class WheelCountryActivity extends Activity {
ChooseCountryPopWindow ChooseCountryPop;
private ArrayList<String> list;
private TextView tvCountry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wheel);
tvCountry = (TextView) findViewById(R.id.tv_country);
final LinearLayout llMain = (LinearLayout) findViewById(R.id.ll_main);
initData();
if (list.size() < 9) {//数据必须9条以上,
return;
}
if (tvCountry.getText().length() == 0) {//没有国家时,默认第一个
flushCountry(0);
}
findViewById(R.id.ll_country).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ChooseCountryPop == null) {
ChooseCountryPop = new ChooseCountryPopWindow(WheelCountryActivity.this);
}
ChooseCountryPop.loadData(list, new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
flushCountry(index);
}
}, new View.OnClickListener() {
@Override
public void onClick(View v) {
ChooseCountryPop.dismiss();
}
});
ChooseCountryPop.showAtLocation(llMain, Gravity.BOTTOM, 0, 0);//在布局底部
}
});
}
private void initData() {
list = new ArrayList<>();
list.add("美国");
list.add("俄国");
list.add("中国");
list.add("泰国");
list.add("日本");
list.add("越南");
list.add("老挝");
list.add("柬埔寨");
list.add("英国");
list.add("西班牙");
list.add("英国");
}
/**
* 刷新国家
*
* @param county
*/
private void flushCountry(int county) {
tvCountry.setText(list.get(county));
}
}
自定义的国家选择器的popupWindow.
public class ChooseCountryPopWindow extends PopupWindow {
LoopView loopView;
TextView tvDone;
public ChooseCountryPopWindow(Context context) {
super(LayoutInflater.from(context).inflate(R.layout.dialog_choose_country, null), ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
loopView = (LoopView) getContentView().findViewById(R.id.loop_view);
tvDone = (TextView) getContentView().findViewById(R.id.tv_done);
setBackgroundDrawable(new ColorDrawable());//设置背景半透明
setFocusable(true);
setTouchable(true);// 设置PopupWindow可触摸
setOutsideTouchable(true);// 设置允许在外点击消失
getContentView().setFocusableInTouchMode(true);//调用View的setFocusableInTouchMode(true)可以使View在Touch Mode模式之下仍然可获得焦点
update();
}
public void loadData(ArrayList<String> list, OnItemSelectedListener onClickLis,View.OnClickListener onClickListener) {
loopView.setListener(onClickLis);
tvDone.setOnClickListener(onClickListener);
//设置原始数据
loopView.setItems(list);
//设置初始位置
loopView.setInitPosition(0);
//设置字体大小
loopView.setTextSize(12);
loopView.setViewPadding(70, 0, 0, 0);
loopView.setNotLoop();
}
}
poopupWindow的布局
dialog_choose_country.xml
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#787878"
android:gravity="right">
<TextView
android:id="@+id/tv_done"
android:text="Done"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:paddingRight="15dp"
android:paddingLeft="10dp"
android:textColor="#FFFFFF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<test.my.com.hellochina.widget.wheelcountry.LoopView
android:id="@+id/loop_view"
android:layout_width="match_parent"
android:paddingLeft="30dp"
android:layout_height="wrap_content"/>
</LinearLayout>
<test.my.com.hellochina.widget.wheelcountry.LoopView
android:id="@+id/loop_view"
android:layout_width="match_parent"
android:paddingLeft="30dp"
android:layout_height="wrap_content"/>
这个是导入第三方控件的地方。