PopupWindow

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import java.util.ArrayList;
import java.util.List;

public class PopupWindowUtil {
    private PopupWindow popupWindow;
    private RecyclerView content;
    private PopAdapter popAdapter;

    /**
     * @param activity
     * @param dayOrNight true 白天  false 黑夜
     * @return
     */
    public PopupWindowUtil init(final Activity activity, boolean dayOrNight) {
        popupWindow = new PopupWindow(activity);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(-00000));
        View view = LayoutInflater.from(activity).inflate(R.layout.popup_down, null);
        content = view.findViewById(R.id.recyclerView);
        ImageView sanjiao = view.findViewById(R.id.sanjiao);
        if (dayOrNight) {
            sanjiao.setImageDrawable(Res.getDrawableRes(R.drawable.sanjiaoxing_w, activity));
            content.setBackground(Res.getDrawableRes(R.drawable.background_pop_w, activity));
        } else {
            sanjiao.setImageDrawable(Res.getDrawableRes(R.drawable.sanjiaoxing, activity));
            content.setBackground(Res.getDrawableRes(R.drawable.background_pop, activity));
        }

        popAdapter = new PopAdapter(activity, new ArrayList(), new PopAdapter.OnClick() {
            @Override
            public void onClick(CallPersonRes.Person p) {
                try {
                    if (null != popupWindow && !activity.isFinishing()) {
                        popupWindow.dismiss();
                    }
                } catch (Exception e) {
                    //e.printStackTrace();
                }
                //CallPhoneUtil.callPhone(activity, p.getStaffTelephone());
            }
        });
        content.setLayoutManager(new LinearLayoutManager(activity));
        content.setAdapter(popAdapter);

        popupWindow.setContentView(view);
        //popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        return this;
    }

    private int offX = 0;
    private int offY = 0;

    public PopupWindowUtil offset(int x, int y) {
        this.offX = x;
        this.offY = y;
        return this;
    }

    public PopupWindowUtil show(View view) {
        popupWindow.showAsDropDown(view, offX, offY);
        return this;
    }

    public PopupWindowUtil setVal(List datas) {
        if (null != content) {
            popAdapter.updateData(datas);
        }
        return this;
    }

    public void dismiss() {
        try {
            if (null != popupWindow)
                popupWindow.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

popup_down.xml




    

        
    

    

background_pop_w.xml



    
    
    

图片


sanjiaoxing.png
sanjiaoxing_w.png

你可能感兴趣的:(PopupWindow)