PopupWindow封装在控件下方弹出下拉弹窗

PopupWindow封装公共弹窗,在控件下方弹出下拉弹窗,加载列表数据。样式如下:

package com.android.sdlc.checkterminal.utils;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.sdlc.checkterminal.R;
import com.android.sdlc.checkterminal.utils.adapter.PopupWindowCommonAdapter;

import java.util.List;

/**
 * Created by WJY.
 * Date: 2021-09-09
 * Time: 10:43
 * Description:公用弹窗  加载列表数据,在控件下方弹出下拉弹窗
 */
public class PopupWindowUtils {

    private static PopupWindow popupWindow;
    private static PopupWindowCommonAdapter adapter;
    private static OnSelectListener onSelectListener;

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    public static void showPopupWindow(Context context, TextView textView, List<String> stringList){
        //创建PopupWindow
        popupWindow = new PopupWindow(context);
        //一定要设置宽和高
        popupWindow.setWidth(textView.getWidth());
        popupWindow.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        //加载布局文件设置给popupWindow
        View view = View.inflate(context, R.layout.popupwindow_common, null);
        RecyclerView recyclerPopupwindow = view.findViewById(R.id.recycler_popupwindow);

        LinearLayoutManager MeasureItemLayoutManager = new LinearLayoutManager(context);
        MeasureItemLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerPopupwindow.setLayoutManager(MeasureItemLayoutManager);
        recyclerPopupwindow.addItemDecoration(new SpacesItemDecoration(UIUtils.dip2Px(1), LinearLayoutManager.VERTICAL));
        adapter = new PopupWindowCommonAdapter(context,stringList);
        recyclerPopupwindow.setAdapter(adapter);

        //将布局文件添加到popupWindow
        popupWindow.setContentView(view);
        //popupWindow.setFocusable(true);
        //设置点击任意popupWindow以外位置关闭popupWindow显示
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.style_pop_animation);

        adapter.setOnItemSelectListener(new PopupWindowCommonAdapter.OnItemSelectListener() {
            @Override
            public void onItemClick(int position, String itemValue) {
                onSelectListener.onSelected(position,itemValue);
                popupWindow.dismiss();
            }
        });

        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        } else {
            popupWindow.showAsDropDown(textView, 0, 0, Gravity.CENTER_HORIZONTAL);
        }
    }

    public interface OnSelectListener{
        void onSelected(int position,String itemValue);
    }
}

在arrays.xml文件里设置数组数值

    <string-array name="samplingRate">
        <item>8000</item>
        <item>12000</item>
        <item>16000</item>
    </string-array>

展示列表数据用到的适配器PopupWindowCommonAdapter

package com.android.sdlc.checkterminal.utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.sdlc.checkterminal.R;

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

/**
 * Created by WJY.
 * Date: 2021-09-09
 * Time: 11:20
 * Description:
 */
public class PopupWindowCommonAdapter extends RecyclerView.Adapter<PopupWindowCommonAdapter.TextHolder> {

    private Context context;
    private List<String> strList = new ArrayList<>();
    private OnItemSelectListener onItemSelectListener;

    public void setOnItemSelectListener(OnItemSelectListener onItemSelectListener) {
        this.onItemSelectListener = onItemSelectListener;
    }

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

    @Override
    public TextHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_popupwindow_common, parent, false);
        return new TextHolder(view);
    }

    @Override
    public void onBindViewHolder(TextHolder holder, int position) {
        holder.tv.setText(strList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemSelectListener.onItemClick(position,strList.get(position));
            }
        });
    }

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

    public class TextHolder extends RecyclerView.ViewHolder {
        private final TextView tv;

        public TextHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.textView);
        }
    }

    public interface OnItemSelectListener{
        void onItemClick(int position,String itemValue);
    }
}

item_popupwindow_common布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_30dp"
        android:ellipsize="end"
        android:gravity="center"
        android:singleLine="true"
        android:text="5"
        android:textColor="@color/white"
        android:textSize="@dimen/dimen_14dp"/>

</RelativeLayout>

在页面中使用

private PopupWindowUtils popupWindowUtils;
private List<String> samplingRateList = new ArrayList<>();



popupWindowUtils = new PopupWindowUtils();
samplingRateList = Arrays.asList(getResources().getStringArray(R.array.samplingRate));


//选择
        tvSamplingRate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindowUtils.showPopupWindow(AuscultationActivity.this, tvSamplingRate, samplingRateList);
                popupWindowUtils.setOnSelectListener(new PopupWindowUtils.OnSelectListener() {
                    @Override
                    public void onSelected(int position, String itemValue) {
                        tvSamplingRate.setText(itemValue);
                        switch (position){
                            case 0://8000Hz (配置下标为11)
                                ……
                                break;
                            case 1://12000Hz (配置下标为9)
                                ……
                                break;
                            case 2://16000Hz (配置下标为8)
                                ……
                                break;
                            case 3://44100Hz (配置下标为4)
                                ……
                                break;
                        }
                    }
                });
            }
        });

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代新人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值