一、问题描述

    记录一下最近在安卓的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);
}

成功截图如下:

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

  1. vue+element项目中使用el-dialog弹出Tree控件报错问题

    1. 按正常的点击按钮,显示dialog弹出的Tree控件,然后把该条数据下的已经选中的checkbox , 用setCheckedNodes或者setCheckedKeys方法选择上 , 报下面这个 ...

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

    如题,需要定制长按Power键弹出的Dialog,UI上的大致效果是:全屏,中间下拉按钮“Swipe Down To Power Off”下拉关机,底部左右两侧“Reboot”,“Cancel”按钮, ...

  3. Qml 写的弹出层控件

    QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; heigh ...

  4. 2015-11-04 报表(c#部分)(Datatable 查询,弹出日期控件,输入是否整数)

    using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq ...

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

    QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; heigh ...

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

    React Native 在debug模式下,可以通过摇动手机,弹出调试选项.但是今天利用了cocoapods 把react native 文件整理后,调试界面就弹不出了,其他功能正常.查了好久,发现 ...

  7. ActiveQt框架 禁止弹出ActiveX控件交互提示

    重点重写QAxBindable的createAggregate() 参考Qt例子: qtactiveqt\activeqt\opengl\glbox.cpp

  8. EasyMvc入门教程-高级控件说明(18)弹出框控件

    前面两节介绍了信息框与对话框,实际开发中如果我们遇到更复杂的要求,比如要求在弹出框里显示另外的网址,如下所示: 实现代码如下: @Html.Q().Popup().Text("我可以嵌套网页 ...

  9. C# 设置MDI子窗体只能弹出一个的方法

    Windows程序设计中的MDI(Multiple Document Interface)官方解释就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Windows .0下的Mi ...

随机推荐

  1. OfficeCommandbarViewer20171005.rar

    OfficeCommandbarViewer用于查看Office各组件工具栏和控件信息的一款软件. 采用了本地XML文件的方式,所以使用本工具不需要提前打开任何Office组件. 动态图: 下载地址: ...

  2. input之按键输入

    通过直接操作驱动来监控键盘,只要程序一旦在后台启动,无论在任何页面都可以监控到按键的数值. 步骤如下: 1.找到键盘挂在点:有两种方法 方法一:在   /dev/input路径下通过  cat eve ...

  3. Linux命令:ldd

    1.ldd不是一个可执行程序,而是一个shell脚本. zlf@ubuntu:~/$ which ldd /usr/bin/ldd zlf@ubuntu:~/$ file /usr/bin/ldd / ...

  4. Linux shell Script初识

    shell secript: 执行方式的差异: ./ sh执行都是在创建一个子程序来执行,只会继承环境变量, 其中的变量如果export声明,子程序的子程序会继承,不会升级为环境变量 source 的 ...

  5. 一次完整的HTTP请求响应过程(很详细)

    一. HTTP请求和响应步骤   图片来自:理解Http请求与响应 以上完整表示了HTTP请求和响应的7个步骤,下面从TCP/IP协议模型的角度来理解HTTP请求和响应如何传递的. 二.TCP/IP协 ...

  6. [LC] 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  7. 网络TCP/IP分层、子网掩码等基本概念

    一.TCP/IP分层: OSI七层网络模型 TCP/IP四层概念模型 对应网络协议 应用层(Application) 应用层 HTTP.TFTP, FTP, NFS, WAIS.SMTP 表示层(Pr ...

  8. FFT与NTT专题

    先不管旋转操作,考虑化简这个差异值 $$begin{aligned}sum_{i=1}^n(x_i-y_i-c)^2&=sum_{i=1}^n(x_i-y_i)^2+nc^2-2csum_{i ...

  9. id0-rsa WP合集

    忙里偷闲做做题wwwwwwwwwwwww Intro to Hashing Intro to PGP Hello PGP Hello OpenSSL Intro to RSA Caesar Hello ...

  10. 18.09.22模拟赛T2 历史

    网上基本上找不到这道题,何况LJJ还稍微改了一下...... 原题:传送门 题目描述 ljj 被S 国数不清的漂亮小姐姐所吸引,为了搞清楚为什么S 国有如此多的漂亮小姐姐,他决定研究S 国的历史. 根 ...