美文网首页
PopupWindow

PopupWindow

作者: wasdzy111 | 来源:发表于2021-06-29 11:17 被阅读0次
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<CallPersonRes.Person>(), 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<CallPersonRes.Person> 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

<?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="wrap_content"
    android:layout_marginBottom="10dp"
    android:gravity="center"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/sanjiao"
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:layout_gravity="center"
            android:src="@drawable/sanjiaoxing" />
    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="-3dp"
        android:background="@drawable/background_pop"
        android:scrollbars="vertical" />
</LinearLayout>

background_pop_w.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#bfbfbf" />
    <!-- 设置圆角
    注意: bottomRightRadius是左下角而不是右下角  bottomLeftRadius右下角-->
    <corners android:radius="10dp"/>
</shape>

图片


sanjiaoxing.png sanjiaoxing_w.png

相关文章

网友评论

      本文标题:PopupWindow

      本文链接:https://www.haomeiwen.com/subject/mqihultx.html