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);
}
成功截图如下:

Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录的更多相关文章
- vue+element项目中使用el-dialog弹出Tree控件报错问题
1. 按正常的点击按钮,显示dialog弹出的Tree控件,然后把该条数据下的已经选中的checkbox , 用setCheckedNodes或者setCheckedKeys方法选择上 , 报下面这个 ...
- 关于一条定制长按Power键弹出Dialog的需求
如题,需要定制长按Power键弹出的Dialog,UI上的大致效果是:全屏,中间下拉按钮“Swipe Down To Power Off”下拉关机,底部左右两侧“Reboot”,“Cancel”按钮, ...
- Qml 写的弹出层控件
QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; heigh ...
- 2015-11-04 报表(c#部分)(Datatable 查询,弹出日期控件,输入是否整数)
using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq ...
- Qml 写的弹出层控件(13篇博客)
QML弹出窗口组件,灯箱效果.动画效果,可拖拽 核心思路:一个mask层,一个最顶层,都用rectangle,禁止事件穿透 使用 Popup { id: popup width: 200; heigh ...
- React native 无法弹出调试控件的问题
React Native 在debug模式下,可以通过摇动手机,弹出调试选项.但是今天利用了cocoapods 把react native 文件整理后,调试界面就弹不出了,其他功能正常.查了好久,发现 ...
- ActiveQt框架 禁止弹出ActiveX控件交互提示
重点重写QAxBindable的createAggregate() 参考Qt例子: qtactiveqt\activeqt\opengl\glbox.cpp
- EasyMvc入门教程-高级控件说明(18)弹出框控件
前面两节介绍了信息框与对话框,实际开发中如果我们遇到更复杂的要求,比如要求在弹出框里显示另外的网址,如下所示: 实现代码如下: @Html.Q().Popup().Text("我可以嵌套网页 ...
- C# 设置MDI子窗体只能弹出一个的方法
Windows程序设计中的MDI(Multiple Document Interface)官方解释就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Windows .0下的Mi ...
随机推荐
- iOS 中UIWebView的cookie
有关cookie是什么,大家可以自行百度,本文我获得cookie的目的是得到一个userID. 下面的是代码. - (void)getUserIDFromCookie { NSHTTPCookieSt ...
- 方差分析|残差|MSA/MSE|Completely randomized design|Randomized block design|LSD|主效应|intercept|多重比较|
符合方差分析的三个条件: 残差=实际值-预测值(其实是均值). 在原假设下,MSA的期望会等于MSE的期望:在备选假设下,MSA的期望会大于MSE的期望,所以MSA/MSE的取值范围在(1,正无穷), ...
- whatsoever|
ADV (用于名词词组后,强调否定陈述)丝毫,任何,无论什么You use whatsoever after a noun group in order to emphasize a negative ...
- practically|exclamation|defy|thrilled|
ADV 简直:几乎Practically means almost, but not completely or exactly. He'd known the old man practically ...
- 第十六届“二十一世纪的计算”学术研讨会 图灵奖获得者Butler W. Lampson主题演讲
Personal Control of Digital Data 图灵奖获得者Butler W. Lampson主题演讲" title="第十六届"二十一世纪的计算&qu ...
- 搭建harbor企业级私有registry
主机环境要求 硬件Hardware Resource Capacity Description CPU minimal 2 CPU 4 CPU is prefered Mem minimal 4GB ...
- RESTFUL Level&HATEOAS
1.什么是RESTful? REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的.翻译过来就是"表现层状态转化." Roy Thomas Fie ...
- html中的锚点介绍和使用
以下资料整理自网路 1.锚点是网页制作中超级链接的一种,又叫命名锚记.命名锚记像一个迅速定位器一样是一种页面内的超级链接,运用相当普遍. 英文名:anchor 使用命名锚记可以在文档中设置标记,这些标 ...
- 秒搭Kubernetes之使用Rancher
Rancher 在接触Docker和K8s的前阶段就耳闻目睹到Rancher,但是没有进一步接触过.直到将K8s搭建完成.才进一步了学习与实践Rancher. Rancher是简便易用的容器管理.其中 ...
- 网络编程OSI介绍
网络编程 软件开发架构 c/s架构(client/server) c:客户端 s:服务端 客户端和服务器端架构,这种架构是从用户层划分的,一般客户端就是在用户电脑上安装的应用程序,而服务端就是公司里的 ...