一、问题描述

    记录一下最近在安卓的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. Anaconda 安装、TensorFlow 安装、pytorch 安装

    问题 1 假设已经安装完Anaconda Navigator,按照 网址:https://www.zhihu.com/question/55577586 的教程操作,设置镜像源地址,安装. 问题 2: ...

  2. [LC] 122. Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. 发现个很有意思的angularjs +grunt 复习项目

    最近作运维工作 docker 接触到一个开源webui dockerui 原项目地址 https://github.com/crosbymichael/dockerui 用angular框架实现,项目 ...

  4. Zabbix调用外部脚本发送邮件:python编写脚本

    Zabbix调用外部脚本发送邮件的时候,会在命令行传入两个参数,第一个参数就是要发送给哪个邮箱地址,第二个参数就是邮件信息,为了保证可以传入多个参数,所以假设有多个参数传入 #!/usr/bin/en ...

  5. 为何印度打车软件Ola,也难逃“资本合并”命运?

    从全球市场来看,共享经济已经引发了多场具有颠覆性的风暴.尤其是在与大众关系紧密的衣食住行方面,诞生了具有强势影响力的独角兽企业.如,共享打车企业Uber.共享房屋出租企业Airbnb等.而鉴于每个国家 ...

  6. 如何手动添加jar包到本地maven仓库

    环境 win10    idea工具 1.确认已经安装好 mvn环境   MAVEN_HOME   D:\Tool\apache-maven-3.5.2 Path %MAVEN_HOME%\bin 2 ...

  7. 吴裕雄--python学习笔记:通过sqlite3 进行文字界面学生管理

    import sqlite3 conn = sqlite3.connect('E:\\student.db') print("Opened database successfully&quo ...

  8. TCP 的三次握手和四次挥手

    参考资料: 1.TCP的三次握手与四次挥手理解及面试题: 2.Http协议三次握手和四次挥手: 3.TCP通信的三次握手和四次撒手的详细流程(顿悟) 前置: 序号(也称序列号) - Sequence ...

  9. 从又一家外卖被Uber收购,看美团打车未来

    别以为Uber在中国失利,连优步中国都被滴滴收购了,就认为Uber已经不行了.其实从全球范围内来看,Uber还是相当强势的--创始人的那些破事儿不算在内.此外,更重要的是Uber已经不单单是在打车业务 ...

  10. 关于CLOSE BY CLIENT STACK TRACE

    关于CLOSE BY CLIENT STACK TRACE 程序正常运行,数据库连接可以获取,一些列操作都可以实现,可在debug信息中总会一段时间就报如下错误: java.lang.Exceptio ...