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 ...
随机推荐
- 感觉自己out了
看了公司混乱而落后的框架,想自己开发一个. 无意中到开源网站看到,开源的控件已经非常多了,基本上说应有尽有. 感叹这个知识大爆炸的年代. 自己现在是坐在井底的蛤蟆?
- Facebook要做约会服务,国内社交眼红吗?
看看现在的各种相亲趣事就能深深感悟到,中国还是以家庭为重的国家.在传统文化的浸染下,国人始终是将家庭摆在第一位.而对于欧美等发达国家来说,他们固然也以家庭为重,但更注重的是男女之间的关系定位--恋爱也 ...
- CentOS换yum源和epel源为国内源
CentOS换源 YUM源 备份原来的repo文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bac ...
- [LC] 56. Merge Intervals
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- highcharts 柱状图在柱子顶部显示y轴数据
var plotOptions={ column:{ //borderColor: "#CCCC66",//边框 shadow: true, //阴影 dataLabels:{ / ...
- 详解js面向对象编程
转自:http://segmentfault.com/a/1190000000713346 基本概念 ECMA关于对象的定义是:”无序属性的集合,其属性可以包含基本值.对象或者函数.“对象的每个属性或 ...
- <JZOJ5943>树
一开始t了五个点我就一脸懵逼 然后 发现高级操作... 就是那个tor的数组2333 可以让一些不需要改的不再去改啦 位运算果然是神奇的东西XD 魔性哈哈哈 #include<cstdio> ...
- Widgets学习
ListView ListView RecyclerView RecyclerView ExpandableListView 关闭箭头 elvMsg.setGroupIndicator(null); ...
- 吴裕雄--天生自然 R语言开发学习:功效分析(续一)
#----------------------------------------# # R in Action (2nd ed): Chapter 10 # # Power analysis # # ...
- 是谁收购了被谷歌私藏7年之久的Zagat?
国内餐饮行业的风口似乎总是执着于"价格"上,无论是天天搞特价的外卖,还是优惠不停歇的团购网站,都将"价格"当做了竞争的杀手锏.即使是网红奶茶.甜品之类的,也是将 ...