安卓底部弹出popupWindow,传入List集合

①、
public class MinePopupWindowChooseFunction {

    private Activity activity;

    private Dialog photoDialog;

    public MinePopupWindowChooseFunction(Activity activity) {
        this.activity = activity;
    }

    public void showDialog(final List list) {

        if (list == null && list.size() == 0) {
            AppUtils.getUtils().toastContent("发生错误");
            return;
        }

        photoDialog = new Dialog(activity, R.style.MyDialog2);
        Window window = photoDialog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.mystyle3); // 添加动画
        View view = View.inflate(activity, R.layout.popup_function_list, null);

        ListView listView = view.findViewById(R.id.lv_popup_function_list);
        OnlyOneTextviewStringAdapter adapter = new OnlyOneTextviewStringAdapter(activity, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                if (!activity.isFinishing()) {
                    onItemChooseFunction(position ,list.get(position));
                    photoDialog.dismiss();
                }

            }
        });

        TextView tvCacel = view.findViewById(R.id.tv_popup_quxiao);
        tvCacel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                photoDialog.dismiss();
            }
        });

        photoDialog.setContentView(view);
        photoDialog.setCanceledOnTouchOutside(true);
        photoDialog.show();
        WindowManager.LayoutParams lp = photoDialog.getWindow().getAttributes();
        lp.width = (YBTApplication.display.getWidth());// 设置宽度
        photoDialog.getWindow().setAttributes(lp);
    }

    /**
     * 列表的条目点击事件
     *
     * @param position
     * @param s
     */
    public void onItemChooseFunction(int position, String s) {

    }
}

②、styles.xml中,添加样式




③、布局文件如下:




    

    

④、列表adapter如下:

public class OnlyOneTextviewStringAdapter extends MyBaseAdapter {

    private Activity mActivity;

    public OnlyOneTextviewStringAdapter(Activity activity , List dataSets) {
        super(dataSets);
        this.list = dataSets;
        mActivity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        OnlyOneTextviewViewholder holder = null;
        if (convertView == null) {
            holder = new OnlyOneTextviewViewholder();
            convertView = View.inflate(mActivity, R.layout.item_only_one_string_textview, null);
            holder.textView = convertView.findViewById(R.id.tv_item_only_one_string_textview);
            convertView.setTag(holder);
        } else  {
            holder = (OnlyOneTextviewViewholder) convertView.getTag();
        }

        final String function = list.get(position);

        holder.textView.setText(function);

        return convertView;
    }

    private class OnlyOneTextviewViewholder{
        TextView textView;
    }
}

⑤、adapter的布局如下:




    

    

⑥、使用如下:

List list = new ArrayList<>();

list.add("分享");

MinePopupWindowChooseFunction popupWindowChooseFunction = new MinePopupWindowChooseFunction(this) {

    /**
     * 列表的条目点击事件
     *
     * @param position
     * @param s
     */
    @Override
    public void onItemChooseFunction(int position, String s) {
        try {
            SelfKaoqinShujuTongjiBean tongjiBean1 = tongjiBeanList.get(position);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};
popupWindowChooseFunction.showDialog(list);

 

你可能感兴趣的:(安卓底部弹出popupWindow,传入List集合)