自己写的一个popupWindow的小工具,简化一下一些简单popupWindow的创建。
ShowPopup:
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
/**
* Created by CKZ on 2017/7/10.
*/
public class ShowPopup {
private ShowPopup showPopup;
private Context context;
private LayoutInflater inflater;
private View popView;
private PopupWindow popupWindow;
/**
* 初始化
* @param context
*/
public ShowPopup (Context context){
this.context = context;
inflater = LayoutInflater.from(context);
showPopup = this;
}
/**
* 输入布局文件,设置popwindow
* @param layoutId
* @return
*/
public ShowPopup showSimplePopupWindows(int layoutId){
popView = inflater.inflate(layoutId,null);
popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(false);
ColorDrawable dw = new ColorDrawable(0x30000000);
popupWindow.setBackgroundDrawable(dw);
return showPopup;
}
/**
* 设置弹窗动画
* @param animId
* @return showPopu
*/
public ShowPopup setAnim(int animId){
if (popupWindow!=null){
popupWindow.setAnimationStyle(animId);
}
return showPopup;
}
/**
* 将弹窗设置在底部
* @param parent
* @return
*/
public ShowPopup atBottom(View parent){
if (popupWindow!=null){
popupWindow.showAtLocation(parent, Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL,0,0);
}
return showPopup;
}
/**
* 设置悬浮框位置,偏移量
* @param paren
* @param x
* @param y
* @return
*/
public ShowPopup dropDown(View paren,int x,int y){
if (popupWindow!=null){
popupWindow.showAsDropDown(paren,x,y);
}
return showPopup;
}
/**
* 设置弹窗的位置以及偏移量
* @param parent
* @param gravity
* @param x
* @param y
* @return
*/
public ShowPopup atLocation(View parent,int gravity,int x,int y){
if (popupWindow!=null){
popupWindow.showAtLocation(parent,gravity,x,y);
}
return showPopup;
}
/**
* 设置布局文件中控件的点击事件
* @param id
* @param listener
* @return
*/
public ShowPopup setClick(int id,View.OnClickListener listener){
if (popView!=null){
popView.findViewById(id).setOnClickListener(listener);
}
return showPopup;
}
/**
* 关闭弹窗的点击事件
* @param id
* @return
*/
public ShowPopup setDismissClick(int id){
if (popupWindow!=null && popView!=null){
popView.findViewById(id).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
}
return showPopup;
}
public void closePopupWindow(){
if (popupWindow!=null){
popupWindow.dismiss();
}
}
/**
* 获取view
* @return
*/
public View getView(){
if (popView!=null){
return popView;
}
return null;
}
}
ShowPopup popu = new ShowPopup(MainActivity.this);
popu.showSimplePopupWindows(R.layout.popu_windows_bottom)
.setAnim(R.style.Animation)
.atBottom(view)
.setClick(R.id.test1, new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"点击了",Toast.LENGTH_SHORT).show();
}
}).setDismissClick(R.id.test2);
final ShowPopup popu = new ShowPopup(MainActivity.this);
popu.showSimplePopupWindows(R.layout.popu_windows_bottom).dropDown(view,0,0)
.setClick(R.id.test1, new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"点击了1",Toast.LENGTH_SHORT).show();
popu.closePopupWindow();
}
}).setClick(R.id.test2, new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"点击了2",Toast.LENGTH_SHORT).show();
popu.closePopupWindow();
}
});
final ShowPopup popu = new ShowPopup(MainActivity.this);
popu.showSimplePopupWindows(R.layout.popu_girde_bottom)
.setAnim(R.style.Animation)
.atBottom(view).setDismissClick(R.id.cancel_btn);
RecyclerView recyclerView = (RecyclerView) popu.getView().findViewById(R.id.share_list);
recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3));
MyAdapter adapter = new MyAdapter(MainActivity.this,mData);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void OnItemClickListener(View view, int position) {
Toast.makeText(MainActivity.this,"点击了"+position,Toast.LENGTH_SHORT).show();
popu.closePopupWindow();
}
});
源码地址:https://github.com/c786909486/PopuTest/