import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.orhanobut.logger.Logger;
import java.util.ArrayList;
import jiayin.com.jiayin.R;
import jiayin.com.jiayin.adapter.PopupAdapter;
/**
* Created by Administrator on 2016/12/30.
*/
public class TopMiddlePopup extends PopupWindow{
private Context myContext;
private ListView myLv;
private AdapterView.OnItemClickListener myOnItemClickListener;
private ArrayList<String> myItems;
private int myWidth;
private int myHeight;
private int myType;
// 判断是否需要添加或更新列表子类项
private boolean myIsDirty = true;
private LayoutInflater inflater = null;
private View myMenuView;
private LinearLayout popupLL;
private PopupAdapter adapter;
public TopMiddlePopup(Context context) {
// TODO Auto-generated constructor stub
}
public TopMiddlePopup(Context context, int width, int height,ArrayList<String> items,
AdapterView.OnItemClickListener onItemClickListener,
int type) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myMenuView = inflater.inflate(R.layout.top_popup, null);
this.myContext = context;
this.myOnItemClickListener = onItemClickListener;
this.myType = type;
this.myItems = items;
this.myWidth = width;
this.myHeight = height;
Logger.e("--myWidth--:" + myWidth + "--myHeight--:"
+ myHeight+"******数据***"+items.toString());
initWidget();
setPopup();
}
public void setDatas(ArrayList<String> items){
this.myItems = items;
}
/**
* 初始化控件
*/
private void initWidget() {
myLv = (ListView) myMenuView.findViewById(R.id.popup_lv);
popupLL = (LinearLayout) myMenuView.findViewById(R.id.popup_layout);
myLv.setOnItemClickListener(myOnItemClickListener);
if (myType == 1) {
android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL
.getLayoutParams();
lpPopup.width = (int) (myWidth * 1.0 / 4);
lpPopup.setMargins(0, 0, (int) (myWidth * 3.0 / 4), 0);
popupLL.setLayoutParams(lpPopup);
} else if (myType == 2) {
android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL
.getLayoutParams();
lpPopup.width = (int) (myWidth * 1.0 / 4);
lpPopup.setMargins((int) (myWidth * 3.0 / 4), 0, 0, 0);
popupLL.setLayoutParams(lpPopup);
}
}
/**
* 设置popup的样式
*/
private void setPopup() {
// 设置AccessoryPopup的view
this.setContentView(myMenuView);
// 设置AccessoryPopup弹出窗体的宽度
this.setWidth(LayoutParams.MATCH_PARENT);
// 设置AccessoryPopup弹出窗体的高度
this.setHeight(LayoutParams.MATCH_PARENT);
// 设置AccessoryPopup弹出窗体可点击
this.setFocusable(true);
// 设置AccessoryPopup弹出窗体的动画效果
if (myType == 1) {
this.setAnimationStyle(R.style.AnimTopLeft);
} else if (myType == 2) {
this.setAnimationStyle(R.style.AnimTopRight);
} else {
//this.setAnimationStyle(R.style.AnimTop);
this.setAnimationStyle(R.style.AnimTopMiddle);
}
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0x33000000);
// 设置SelectPicPopupWindow弹出窗体的背景
this.setBackgroundDrawable(dw);
myMenuView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int height = popupLL.getBottom();
int left = popupLL.getLeft();
int right = popupLL.getRight();
System.out.println("--popupLL.getBottom()--:"
+ popupLL.getBottom());
int y = (int) event.getY();
int x = (int) event.getX();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y > height || x < left || x > right) {
System.out.println("---点击位置在列表下方--");
dismiss();
}
}
return true;
}
});
}
/**
* 显示弹窗界面
*
* @param view
*/
public void show(View view) {
if (myIsDirty) {
myIsDirty = false;
adapter = new PopupAdapter(myContext, myItems, myType);
myLv.setAdapter(adapter);
}
showAsDropDown(view, 0, 0);
}
public String getItem(int position){
return adapter.getItem(position);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/popup_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#ffffff"
android:orientation="vertical" >
<ListView
android:id="@+id/popup_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="0.5dp" >
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dp"
/>
</LinearLayout>
</RelativeLayout>
动画
<!--动画-->
<style name="AnimTop" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/push_top_in</item>
<item name="android:windowExitAnimation">@anim/push_top_out</item>
</style>
<style name="AnimTopRight" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/top_right_in</item>
<item name="android:windowExitAnimation">@anim/top_right_out</item>
</style>
<style name="AnimTopLeft" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/top_left_in</item>
<item name="android:windowExitAnimation">@anim/top_left_out</item>
</style>
<style name="AnimTopMiddle" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/top_middle_in</item>
<item name="android:windowExitAnimation">@anim/top_middle_out</item>
</style>
push_top_in
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="-100%p"
android:toYDelta="0" />
<alpha
android:duration="500"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
push_top_out
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="0"
android:toYDelta="-100%p" />
<alpha
android:duration="500"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
top_right_in
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="100%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
top_right_out
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="100%"
android:pivotY="0%"
android:toXScale="0.0"
android:toYScale="0.0" />
</set>
top_left_in
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="0%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
top_left_out
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="0%"
android:pivotY="0%"
android:toXScale="0.0"
android:toYScale="0.0" />
</set>
top_middle_in
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
top_middle_out
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fillAfter="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="0%"
android:toXScale="0.0"
android:toYScale="0.0" />
</set>
调用
/**
* 设置弹窗
*
* @param type
*/
private void setPopup(int type) {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list.add("第" + i + "单元");
}
middlePopup = new TopMiddlePopup(TaskAnalyesActivity.this, screenW, screenH,list,
onItemClickListener, type);
}
/**
* 弹窗点击事件
*/
private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = middlePopup.getItem(position);
dytv.setText(item);
middlePopup.dismiss();
}
};
setPopup(2);
middlePopup.show(actionbar);