安卓右上角的弹框 自定义PopWindow实现

import android.app.Activity;
import android.content.Context;
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.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ylt.gxjkz.youliantong.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hutao on 2018/3/27.
 * 右上角的弹框  如:扫一扫等
 */
public class ShowPopRightTop extends PopupWindow {

    private final View contentView;

    public interface OnItemClick {
        void OnClick(int position);
    }

    public ShowPopRightTop(final Activity context, List<String> mData, final OnItemClick click) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.pop_window_right_top, null);

        int height = context.getWindowManager().getDefaultDisplay().getHeight();
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        //设置SelectPicPopupWindowview
        this.setContentView(contentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(width / 3);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明   0000000000
//        ColorDrawable dw = new ColorDrawable(context.getResources().getColor(R.color.color_ff8300));
        // back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ,设置其他控件变化等操作
        this.setBackgroundDrawable(new ColorDrawable());
//        this.setAnimationStyle(android.R.style.Animation_Dialog);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimationPreview);
        RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerView);

        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        ShowPopRightTopAdapter myAdapter = new ShowPopRightTopAdapter(context, mData);
        recyclerView.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new ShowPopRightTopAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                click.OnClick(position);
                ShowPopRightTop.this.dismiss();
            }
        });
    }

    public static class ShowPopRightTopAdapter extends RecyclerView.Adapter<ShowPopRightTopAdapter.ViewHolder> {
        private Context context;
        private List<String> mData = new ArrayList<>();

        public ShowPopRightTopAdapter(Context context, List<String> mData) {
            this.context = context;
            this.mData = mData;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_show_pop_right_top, parent, false));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            holder.desc.setText(mData.get(position));
            holder.item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.OnItemClick(position);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            LinearLayout item;
            TextView desc;

            public ViewHolder(View itemView) {
                super(itemView);
                item = (LinearLayout) itemView.findViewById(R.id.item_layout);
                desc = (TextView) itemView.findViewById(R.id.desc);
            }
        }

        private ShowPopRightTopAdapter.OnItemClickListener listener;

        public void setOnItemClickListener(OnItemClickListener listener) {
            this.listener = listener;
        }

        public interface OnItemClickListener {
            void OnItemClick(int position);
        }
    }


    /**
     * 显示popupWindow
     *
     * @param parent
     */
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            // 以下拉方式显示popupwindow
            this.showAsDropDown(parent, parent.getLayoutParams().width / 2 - 10, 0);
        } else {
            this.dismiss();
        }
    }

}

        R.layout.pop_window_right_top  布局文件:
<?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:background="@color/color_7F7F7F"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
        R.style.AnimationPreview    动画:
<!--右上角弹框的动画   扫一扫的那个弹框-->
<style name="AnimationPreview">
    <item name="android:windowEnterAnimation">@anim/pop_right_top_fade_in</item>
    <item name="android:windowExitAnimation">@anim/pop_right_top_fade_out</item>
</style>
pop_right_top_fade_in   :
<?xml version="1.0" encoding="utf-8"?><!-- 左上角扩大-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromXScale="0.001"
    android:fromYScale="0.001"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="100%"
    android:pivotY="10%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

pop_right_top_fade_out  :
<?xml version="1.0" encoding="utf-8"?><!-- 左上角缩小 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="100%"
    android:pivotY="10%"
    android:toXScale="0.001"
    android:toYScale="0.001" />
R.layout.item_show_pop_right_top  布局文件:
<?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:orientation="vertical">

    <LinearLayout
        android:id="@+id/item_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:gravity="center"
            android:src="@mipmap/icon_delete"
            android:visibility="gone" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginLeft="5dp"
            android:gravity="center"
            android:text="扫一扫"
            android:textColor="@color/color_ffffff"
            android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>

使用  :
List<String> popList = new ArrayList<>();
popList.add("第一条");
popList.add("第二条");
ShowPopRightTop showPopRightTop = new ShowPopRightTop(MasterActivity.this, popList, new ShowPopRightTop.OnItemClick() {
    @Override
    public void OnClick(int position) {
        if (position == 0) {
            //第一条
        } else if (position == 1) {
            //第二条
        }
    }
});
showPopRightTop.showPopupWindow(mRlShowPop); //是点击的按钮

阅读更多

没有更多推荐了,返回首页