用popupwindow实现在某空间正下方滑动弹出,并用SharedPreferences记录选择当前项的位置。
activity的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bg_work">
<TextView
android:id="@+id/tv_pop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="名称排序"
android:textSize="16sp"
android:drawableRight="@drawable/back"
android:padding="10dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"/>
</LinearLayout>
popupwindow的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#345435">
<RelativeLayout
android:id="@+id/rl_01"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="名称排序"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/back"
android:visibility="gone" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<RelativeLayout
android:id="@+id/rl_02"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="从高到低排序"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/back"
android:visibility="gone" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
<RelativeLayout
android:id="@+id/rl_03"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/tv_03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="从低到高排序"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="10dp"
android:src="@drawable/back"
android:visibility="gone" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>
动画样式需要在styles.xml中定义:
<style name="style_pop_animation">
<item name="android:windowEnterAnimation">@anim/anim_pop_in</item>
<item name="android:windowExitAnimation">@anim/anim_pop_out</item>
</style>
动画须在anim文件夹中定义:
弹出动画anim_pop_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="300"
android:fromXScale="1.0"
android:fromYScale="0"
android:pivotX="100%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0"/>
</set>
隐藏动画anim_pop_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="300"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="100%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="0.0"/>
</set>
activity代码
public class PopUpWindowActivity extends Activity implements OnClickListener {
private TextView tv_pop;
private PopupWindow popupWindow;
private SharedPreferences sp;
private ImageView iv_01, iv_02, iv_03;
private TextView tv_01, tv_02, tv_03;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_popupwindow);
sp = getSharedPreferences("state", MODE_PRIVATE);
// 初始化时清除保存的状态数据
Editor edit = sp.edit();
edit.clear();
edit.commit();
initView();
}
private void initView() {
tv_pop = (TextView) findViewById(R.id.tv_pop);
tv_pop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_pop:
showPopWindow(getView());
break;
default:
break;
}
}
private void showPopWindow(View view) {
try {
popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
ColorDrawable cd = new ColorDrawable(0x00ffffff);// 背景颜色全透明
popupWindow.setBackgroundDrawable(cd);
int[] location = new int[2];
tv_pop.getLocationOnScreen(location);
popupWindow.setAnimationStyle(R.style.style_pop_animation);// 动画效果必须放在showAsDropDown()方法上边,否则无效
backgroundAlpha(0.5f);// 设置背景半透明
popupWindow.showAsDropDown(tv_pop);
//popupWindow.showAtLocation(tv_pop, Gravity.NO_GRAVITY, location[0]+tv_pop.getWidth(),location[1]);
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
popupWindow = null;// 当点击屏幕时,使popupWindow消失
backgroundAlpha(1.0f);// 当点击屏幕时,使半透明效果取消
}
});
if (sp.getInt("flag", 1) == 1) {
iv_01.setVisibility(View.VISIBLE);
tv_01.setTextColor(Color.RED);
} else if (sp.getInt("flag", 2) == 2) {
iv_02.setVisibility(View.VISIBLE);
tv_02.setTextColor(Color.RED);
} else if (sp.getInt("flag", 3) == 3) {
iv_03.setVisibility(View.VISIBLE);
tv_03.setTextColor(Color.RED);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 设置popupWindow背景半透明
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha;// 0.0-1.0
getWindow().setAttributes(lp);
}
/*
* 得到popupwindow的View
*/
private View getView() {
View view = LayoutInflater.from(PopUpWindowActivity.this).inflate(
R.layout.layout_pop_item, null);
iv_01 = (ImageView) view.findViewById(R.id.iv_01);
iv_02 = (ImageView) view.findViewById(R.id.iv_02);
iv_03 = (ImageView) view.findViewById(R.id.iv_03);
tv_01 = (TextView) view.findViewById(R.id.tv_01);
tv_02 = (TextView) view.findViewById(R.id.tv_02);
tv_03 = (TextView) view.findViewById(R.id.tv_03);
RelativeLayout rl_01 = (RelativeLayout) view.findViewById(R.id.rl_01);
RelativeLayout rl_02 = (RelativeLayout) view.findViewById(R.id.rl_02);
RelativeLayout rl_03 = (RelativeLayout) view.findViewById(R.id.rl_03);
rl_01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
tv_pop.setText("名称排序");
saveCurrentState(1);
}
});
rl_02.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
tv_pop.setText("从高到低排序");
saveCurrentState(2);
}
});
rl_03.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iv_03.setVisibility(View.VISIBLE);
popupWindow.dismiss();
tv_pop.setText("从低到高排序");
saveCurrentState(3);
}
});
return view;
}
/*
* 保存当前状态 1-->名称排序 2-->从高到低 3-->从低到高
*/
private void saveCurrentState(int i) {
Editor editor = sp.edit();
editor.putInt("flag", i);
editor.commit();
}
}
基本就能实现了。