底部弹出PopupWindow+地址三级联动,多问题全面分析,详细注释
【摘要】
主要功能:从底部弹出的 PopupWindow,带动画,带灰色背景,在PopupWindow中实现省市区三级联动,并填充textview
先看效果图
1.PopupWindow
<span style="font-size:18px;">public class PopHelper { p...
主要功能:从底部弹出的 PopupWindow,带动画,带灰色背景,在PopupWindow中实现省市区三级联动,并填充textview
先看效果图
1.PopupWindow
-
<span style="font-size:18px;">public class PopHelper {
-
-
private Context mContext;
-
private PopupWindow pw;
-
private CityPicker cityPicker;
-
-
public PopHelper(Context context) {
-
mContext = context;
-
}
-
-
/**
-
* @param view
-
* @param itemsOnClick 抛出一个OnClickListener接口
-
*/
-
public void showAddressPop(View view, final Activity activity, View.OnClickListener itemsOnClick) {
-
// 设置pw里的点击事件
-
LinearLayout addressView = (LinearLayout) View.inflate(mContext, R.layout.pop_address, null);
-
Button btnPopAddressCencel = (Button) addressView.findViewById(R.id.btn_pop_address_cencel);
-
btnPopAddressCencel.setOnClickListener(itemsOnClick);
-
Button btnPopAddressEnter = (Button) addressView.findViewById(R.id.btn_pop_address_enter);
-
btnPopAddressEnter.setOnClickListener(itemsOnClick);
-
cityPicker = (CityPicker) addressView.findViewById(R.id.citypicker);
-
//-2是包裹自己,-1是填充父容器
-
pw = new PopupWindow(addressView, -1, -2);
-
//先关闭
-
colsePopupwindow();
-
//必须要设置一个背景
-
pw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
-
//点击外边可关闭pw
-
pw.setOutsideTouchable(true);
-
//pw内可获取焦点
-
pw.setFocusable(true);
-
// 设置背景颜色变暗
-
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
-
lp.alpha = 0.7f;
-
activity.getWindow().setAttributes(lp);
-
//退出时恢复透明度
-
pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
-
-
@Override
-
public void onDismiss() {
-
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
-
lp.alpha = 1f;
-
activity.getWindow().setAttributes(lp);
-
}
-
});
-
//设置动画
-
pw.setAnimationStyle(R.style.take_photo_anim);
-
//显示。y轴距离底部100是因为部分手机有虚拟按键,所以实际项目中要动态设置
-
pw.showAtLocation(view, Gravity.BOTTOM, 0, 100);
-
}
-
-
/**
-
*
-
* @return 获取控件中的地址数据
-
*/
-
public String getAddressData() {
-
String cData = cityPicker.getCity_string();
-
return cData;
-
}
-
-
/**
-
* 关闭pw
-
*/
-
public void colsePopupwindow() {
-
if (pw != null && pw.isShowing()) {
-
pw.dismiss();
-
pw = null;
-
}
-
}
-
-
}</span>
2.pop的弹出退出动画
pop_enter_anim.xml
-
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
-
<set xmlns:android="http://schemas.android.com/apk/res/android">
-
-
<translate
-
android:duration="200"
-
android:fromYDelta="100%p"
-
android:toYDelta="0" />
-
<alpha
-
android:duration="200"
-
android:fromAlpha="0.0"
-
android:toAlpha="1.0" />
-
</set></span>
pop_exit_anim.xml
-
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
-
<set xmlns:android="http://schemas.android.com/apk/res/android">
-
<translate
-
android:duration="200"
-
android:fromYDelta="0"
-
android:toYDelta="50%p" />
-
<alpha
-
android:duration="200"
-
android:fromAlpha="1.0"
-
android:toAlpha="0.0" />
-
</set> </span>
记得copy styles文件中的style
3.在MainActivity中的调用
-
<span style="font-size:18px;">public class MainActivity extends AppCompatActivity implements View.OnClickListener {
-
-
private TextView textView1, textView2;
-
private Button button1, button2;
-
private PopHelper popHelper;//pop
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
textView1 = (TextView) findViewById(R.id.textView1);
-
textView2 = (TextView) findViewById(R.id.textView2);
-
button1 = (Button) findViewById(R.id.button1);
-
button1.setOnClickListener(this);
-
button2 = (Button) findViewById(R.id.button2);
-
button2.setOnClickListener(this);
-
}
-
-
/**
-
* 实现popHelper中的item接口
-
*/
-
private View.OnClickListener onClickListener = new View.OnClickListener() {
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()) {
-
case R.id.btn_pop_address_cencel:
-
popHelper.colsePopupwindow();
-
break;
-
case R.id.btn_pop_address_enter:
-
String addressData = popHelper.getAddressData();
-
if (flag) {
-
textView1.setText(addressData);
-
} else {
-
textView2.setText(addressData);
-
}
-
popHelper.colsePopupwindow();
-
break;
-
}
-
}
-
};
-
-
boolean flag;//用于pop标记显示,避免多个地方显示却只填充一个textview的情况
-
-
@Override
-
public void onClick(View v) {
-
switch (v.getId()) {
-
case R.id.button1:
-
popHelper = new PopHelper(getApplicationContext());
-
popHelper.showAddressPop(v, MainActivity.this, onClickListener);
-
flag = true;
-
break;
-
case R.id.button2:
-
popHelper = new PopHelper(getApplicationContext());
-
popHelper.showAddressPop(v, MainActivity.this, onClickListener);
-
flag = false;
-
break;
-
}
-
}
-
}</span>
4.地址三级联动
首先,在main目录下创建assets文件,并copy城市json数据包
其次,在lib中加入gson包,记得As a lib,或直接
<span style="font-size:18px;">compile 'com.google.code.gson:gson:2.6.2'</span>
然后,修改布局中的控件名称换成自己的包名+自定义类名
最后在values文件夹中copy-attrs.xml文件
然后就完美实现了。
代码中都有详细注释
源码下载地址:https://github.com/yechaoa/PopAddress
文章来源: blog.csdn.net,作者:yechaoa,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/yechaoa/article/details/52818287
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)