Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

一、问题描述

????记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!!

????在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout longSetting = (LinearLayout) view.findViewById(R.id.testlong);,然后对变量longSetting调用setOnLongClickListener函数实现注册长按事件,但是在一个fragment控件中如此设置长按无法弹出popupwindows控件。在fragment控件无法实现长按事件弹出popwindows控件的代码如下:

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/LongSet"
    android:orientation="vertical"
    android:layout_height="match_parent">
        <ListView
            android:id="@+id/chat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
</LinearLayout>

java代码:

View view = inflater.inflate(R.layout.chat,container,false);
LinearLayout longSetting = (LinearLayout) view.findViewById(R.id.LongSet);
longSetting.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showPop();
            return true;
        }
    });

二、解决方法

????解决方法也很迷!!!经过实验发现在根布局中再镶嵌一个布局把所有的控件装进去,然后将这个镶嵌的控件实例化,然后对该布局设置长按事件弹出popupwindows控件即可!!!相关代码如下:

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="@+id/LongSet"
android:orientation="vertical"
android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/testlong"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/chat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

java代码:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.chat,container,false);
    LinearLayout longSetting = (LinearLayout) view.findViewById(R.id.testlong);
    longSetting.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            showPop();
            return true;
        }
    });
    ListView lv = (ListView)view.findViewById(R.id.chat);
    mdata = new LinkedList<RecvData>();
    recvItem = new RecvItem(mdata,getActivity());
    lv.setAdapter(recvItem);
    RecvMsgFromServer recvMsgFromServer = new RecvMsgFromServer(handler);
    recvMsgFromServer.start();
    return view;
}

public void showPop()
{
    View contentView = getActivity().getLayoutInflater().inflate(R.layout.pop,null);
    mPopWindow = new PopupWindow(contentView,ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT, true);
    mPopWindow.setContentView(contentView);
    TextView btn_send = (TextView)contentView.findViewById(R.id.send);
    TextView btn_cancle = (TextView)contentView.findViewById(R.id.exit);
    btn_cancle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPopWindow.dismiss();
        }
    });
    btn_send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mPopWindow.dismiss();
            alertDialog = new AlertSendMessage(getActivity(),handler).getAlert();
            alertDialog.show();
        }
    });
    ColorDrawable colorDrawable = new ColorDrawable(0xb0000000);
    mPopWindow.setBackgroundDrawable(colorDrawable);
    mPopWindow.setAnimationStyle(R.style.PopDh);
    View rootview = getActivity().getLayoutInflater().inflate(R.layout.chat,null);
    mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
}

成功截图如下:

原文地址:https://www.cnblogs.com/aWxvdmVseXc0/p/12113136.html

时间: 2024-08-25 18:34:38

Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录的相关文章

设置点击文本框或图片弹出日历控件

<input id="startDate" value="" readonly="true" type="text" style="cursor: pointer" class="user_datepicker "><input class="canlderImg" data-tag="start" type="imag

关于一条定制长按Power键弹出Dialog的需求

如题,需要定制长按Power键弹出的Dialog,UI上的大致效果是:全屏,中间下拉按钮“Swipe Down To Power Off”下拉关机,底部左右两侧“Reboot”,“Cancel”按钮,分别是重启,取消操作.并要求弹出Dialog的同时,背景渐变模糊,操作控件有相应动画效果,执行相应操作有同步动画,退出界面背景渐变至透明消失.设计效果酱紫: 具体控件动画要求就不再详述.主要两件事:1.关机流程,更准确的说应该是对长按Power键的处理:2.定制Dialog. 1.长按Power键,

Android学习----------长按列表项弹出菜单,给菜单项添加事件,获取上下文

这里是先显示一个listview,长按listview的一个列表项,弹出一个菜单来,菜单有两个条目, "更新该条"和"删除该条",并且这两条都有监听事件,整体就像微信好友对话操作一样. 其中菜单项有一个获取上下文的操作:AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 这句也是用于列表项的. 具体代码如下: protected void onCreate(Bundle

使用设置自定义对话框的大小,位置,样式以及设置在安卓桌面上弹出对话框

第一步:创建对话框 MyDialog mydlg=new MyDialog(context); 第二步:获取对话框的window实例,通过window实例来控制对话框出现的位置,大小和弹出时候的场景 Window dlgwindow=mydly.getWindow(); 设置对话框最终显示位置 dlgWindow.setGravity(Gravity.TOP); 参数可以是Gravity.TOP(顶部),Gravity.BUTTON(底部),Gravity.LEFT(左边),Gravity.RI

android 长按 ListView 无法弹出 ContextMeun

可能的原因: onItemLongClick 消费了长按事件 mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { mApSelect = position; Log.d(TAG, &quo

前台动态创建控件设置ID,事件等实例

代码中包括:动态生成各个控件(textbox,div等),动态设置样式,属性,id,前台map创建,dom元素操作等. //自定义前台Map function Map() { this.elements = new Array(); // 获取MAP元素个数 this.size = function() { return this.elements.length; }; // 判断MAP是否为空 this.isEmpty = function() { return (this.elements.

Qml 写的弹出层控件(13篇博客)

QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; height: 300 x: 200; y:100 z: 101 opacity: 0.8 visible: false; radius: 5 ... } popup.showMask = chk.checked; popup.animationType = 'size'; popup.show(); 注意

React native 无法弹出调试控件的问题

React Native 在debug模式下,可以通过摇动手机,弹出调试选项.但是今天利用了cocoapods 把react native 文件整理后,调试界面就弹不出了,其他功能正常.查了好久,发现是少在pods的 spec里写了DevSupport 这个模块. 下面发一份pods 的 spec 文件作为参考: # Uncomment the next line to define a global platform for your project platform :ios, '8.0'

Qml 写的弹出层控件

QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; height: 300 x: 200; y:100 z: 101 opacity: 0.8 visible: false; radius: 5 ... } popup.showMask = chk.checked; popup.animationType = 'size'; popup.show(); 注意