java - Android Gridview item 0 onlickLister 不会注册
问题描述
我有一个gridview,它工作得很好,除了第一个项目上的onlcick没有注册,如果我向下滚动一段时间然后再回来的话,如果有人有任何想法,我真的很感激...... .
Gridadapter: - 主要构造函数(getPosition 等)都是标准的
public View setFriendGridItem(View view,Friend friend) {
if(view == null) view = layoutInflator.inflate(R.layout.basic_friend_item, null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
view.setLayoutParams(layoutParams);
TextView nameView = view.findViewById(R.id.basic_friend_display_name);
TextView statusView = view.findViewById(R.id.basic_friend_status);
if(statusView.hasOnClickListeners())statusView.setOnClickListener(null);
String status = friend.id%5 == 0 ? "outgoing" : (friend.id%3 == 0 ? "incoming" : friend.id%2==0?"confirmed": "");
setFriendStatus(statusView,status,friend.id);
nameView.setText(friend.displayName);
return view;
}
id 没有做任何事情,它只是传递给跟踪日志 - Util.Log 只是写出一个日志,因为我厌倦了写 String.valueof....
public void setFriendStatus(TextView view, String status,final int id) {
Util.logInt("66",id);
switch(status.toLowerCase()) {
case "outgoing":
view.setText("Request sent");
view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Util.logStr("gridadapter 73","cancel request");Util.logInt("69",id);
}
});
view.setBackgroundColor(context.getResources().getColor(R.color.playdateLightGreen));
break;
case "incoming":
view.setText("Wants to be friends");
view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Util.logStr("gridadapter 73","respond to request");Util.logInt("69",id);
}
});
view.setBackgroundColor(context.getResources().getColor(R.color.playdateLightGreen));
break;
case "confirmed":
view.setText("Confirmed");
view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Util.logStr("gridadapter 84","remove friend");Util.logInt("69",id);
}
});
view.setBackgroundColor(context.getResources().getColor(R.color.playdateMainGreen));
break;
default:
view.setText("Connect");
view.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Util.logStr("gridadapter 94","send request");Util.logInt("69",id);
}
});
view.setBackgroundColor(context.getResources().getColor(R.color.playdateMainRed));
break;
}
}
目前数据只是我正在硬编码的样本数据(在设计阶段)它是一个基本项目,我不知道它是否有助于在正确加载等时看到它,但只要这么说,我可以添加它在
对于那些喜欢 logcat 的人(在第一个 LOGI 之前,我点击了几次文本视图,当我最终返回第 1 项注册时,您可以在底部看到):
2019-03-07 18:13:46.982 2815-2815/com.localplaydatesandroid I/LOGI 66: 1
2019-03-07 18:13:46.995 2815-2815/com.localplaydatesandroid I/LOGI 66: 1
2019-03-07 18:13:47.007 2815-2815/com.localplaydatesandroid I/LOGI 66: 2
2019-03-07 18:13:47.023 2815-2815/com.localplaydatesandroid I/LOGI 66: 3
2019-03-07 18:13:47.039 2815-2815/com.localplaydatesandroid I/LOGI 66: 4
2019-03-07 18:13:47.051 2815-2815/com.localplaydatesandroid I/LOGI 66: 5
2019-03-07 18:13:47.065 2815-2815/com.localplaydatesandroid I/LOGI 66: 6
2019-03-07 18:13:47.079 2815-2815/com.localplaydatesandroid I/LOGI 66: 7
2019-03-07 18:13:47.096 2815-2815/com.localplaydatesandroid I/LOGI 66: 8
2019-03-07 18:14:49.675 2815-2815/com.localplaydatesandroid I/LOGI gridadapter 84: remove friend
2019-03-07 18:14:49.675 2815-2815/com.localplaydatesandroid I/LOGI 69: 4
2019-03-07 18:14:53.695 2815-2815/com.localplaydatesandroid I/LOGI gridadapter 84: remove friend
2019-03-07 18:14:53.695 2815-2815/com.localplaydatesandroid I/LOGI 69: 2
2019-03-07 18:14:55.677 2815-2815/com.localplaydatesandroid I/LOGI gridadapter 73: respond to request
2019-03-07 18:14:55.677 2815-2815/com.localplaydatesandroid I/LOGI 69: 3
2019-03-07 18:14:58.733 2815-2815/com.localplaydatesandroid I/LOGI gridadapter 84: remove friend
2019-03-07 18:14:58.733 2815-2815/com.localplaydatesandroid I/LOGI 69: 4
2019-03-07 18:15:00.847 2815-2815/com.localplaydatesandroid I/LOGI 66: 9
2019-03-07 18:15:00.851 2815-2815/com.localplaydatesandroid I/LOGI 66: 10
2019-03-07 18:15:00.879 2815-2815/com.localplaydatesandroid I/LOGI 66: 11
2019-03-07 18:15:00.882 2815-2815/com.localplaydatesandroid I/LOGI 66: 12
2019-03-07 18:15:00.913 2815-2815/com.localplaydatesandroid I/LOGI 66: 13
2019-03-07 18:15:00.916 2815-2815/com.localplaydatesandroid I/LOGI 66: 14
2019-03-07 18:15:00.947 2815-2815/com.localplaydatesandroid I/LOGI 66: 15
2019-03-07 18:15:00.949 2815-2815/com.localplaydatesandroid I/LOGI 66: 16
2019-03-07 18:15:00.979 2815-2815/com.localplaydatesandroid I/LOGI 66: 17
2019-03-07 18:15:00.982 2815-2815/com.localplaydatesandroid I/LOGI 66: 18
2019-03-07 18:15:00.996 2815-2815/com.localplaydatesandroid I/LOGI 66: 19
2019-03-07 18:15:00.999 2815-2815/com.localplaydatesandroid I/LOGI 66: 20
2019-03-07 18:15:01.030 2815-2815/com.localplaydatesandroid I/LOGI 66: 21
2019-03-07 18:15:01.033 2815-2815/com.localplaydatesandroid I/LOGI 66: 22
2019-03-07 18:15:01.063 2815-2815/com.localplaydatesandroid I/LOGI 66: 23
2019-03-07 18:15:01.066 2815-2815/com.localplaydatesandroid I/LOGI 66: 24
2019-03-07 18:15:01.097 2815-2815/com.localplaydatesandroid I/LOGI 66: 25
2019-03-07 18:15:01.099 2815-2815/com.localplaydatesandroid I/LOGI 66: 26
2019-03-07 18:15:01.131 2815-2815/com.localplaydatesandroid I/LOGI 66: 27
2019-03-07 18:15:01.133 2815-2815/com.localplaydatesandroid I/LOGI 66: 28
2019-03-07 18:15:01.165 2815-2815/com.localplaydatesandroid I/LOGI 66: 29
2019-03-07 18:15:01.167 2815-2815/com.localplaydatesandroid I/LOGI 66: 30
2019-03-07 18:15:01.198 2815-2815/com.localplaydatesandroid I/LOGI 66: 31
2019-03-07 18:15:01.201 2815-2815/com.localplaydatesandroid I/LOGI 66: 32
2019-03-07 18:15:01.249 2815-2815/com.localplaydatesandroid I/LOGI 66: 33
2019-03-07 18:15:01.251 2815-2815/com.localplaydatesandroid I/LOGI 66: 34
2019-03-07 18:15:01.282 2815-2815/com.localplaydatesandroid I/LOGI 66: 35
2019-03-07 18:15:01.285 2815-2815/com.localplaydatesandroid I/LOGI 66: 36
2019-03-07 18:15:01.332 2815-2815/com.localplaydatesandroid I/LOGI 66: 37
2019-03-07 18:15:01.335 2815-2815/com.localplaydatesandroid I/LOGI 66: 38
2019-03-07 18:15:01.366 2815-2815/com.localplaydatesandroid I/LOGI 66: 39
2019-03-07 18:15:01.368 2815-2815/com.localplaydatesandroid I/LOGI 66: 40
2019-03-07 18:15:01.416 2815-2815/com.localplaydatesandroid I/LOGI 66: 41
2019-03-07 18:15:01.419 2815-2815/com.localplaydatesandroid I/LOGI 66: 42
2019-03-07 18:15:01.475 2815-2815/com.localplaydatesandroid I/LOGI 66: 43
2019-03-07 18:15:01.490 2815-2815/com.localplaydatesandroid I/LOGI 66: 44
2019-03-07 18:15:01.534 2815-2815/com.localplaydatesandroid I/LOGI 66: 45
2019-03-07 18:15:01.536 2815-2815/com.localplaydatesandroid I/LOGI 66: 46
2019-03-07 18:15:01.601 2815-2815/com.localplaydatesandroid I/LOGI 66: 47
2019-03-07 18:15:01.603 2815-2815/com.localplaydatesandroid I/LOGI 66: 48
2019-03-07 18:15:01.668 2815-2815/com.localplaydatesandroid I/LOGI 66: 49
2019-03-07 18:15:01.670 2815-2815/com.localplaydatesandroid I/LOGI 66: 50
2019-03-07 18:15:01.753 2815-2815/com.localplaydatesandroid I/LOGI 66: 51
2019-03-07 18:15:01.756 2815-2815/com.localplaydatesandroid I/LOGI 66: 52
2019-03-07 18:15:01.836 2815-2815/com.localplaydatesandroid I/LOGI 66: 53
2019-03-07 18:15:01.840 2815-2815/com.localplaydatesandroid I/LOGI 66: 54
2019-03-07 18:15:01.954 2815-2815/com.localplaydatesandroid I/LOGI 66: 55
2019-03-07 18:15:01.957 2815-2815/com.localplaydatesandroid I/LOGI 66: 56
2019-03-07 18:15:02.088 2815-2815/com.localplaydatesandroid I/LOGI 66: 57
2019-03-07 18:15:02.091 2815-2815/com.localplaydatesandroid I/LOGI 66: 58
2019-03-07 18:15:02.256 2815-2815/com.localplaydatesandroid I/LOGI 66: 59
2019-03-07 18:15:02.259 2815-2815/com.localplaydatesandroid I/LOGI 66: 60
2019-03-07 18:15:02.508 2815-2815/com.localplaydatesandroid I/LOGI 66: 61
2019-03-07 18:15:02.511 2815-2815/com.localplaydatesandroid I/LOGI 66: 62
2019-03-07 18:15:03.966 2815-2815/com.localplaydatesandroid I/LOGI gridadapter 73: cancel request
2019-03-07 18:15:03.967 2815-2815/com.localplaydatesandroid I/LOGI 69: 60
2019-03-07 18:15:04.993 2815-2815/com.localplaydatesandroid I/LOGI 66: 53
2019-03-07 18:15:04.995 2815-2815/com.localplaydatesandroid I/LOGI 66: 54
2019-03-07 18:15:05.043 2815-2815/com.localplaydatesandroid I/LOGI 66: 51
2019-03-07 18:15:05.045 2815-2815/com.localplaydatesandroid I/LOGI 66: 52
2019-03-07 18:15:05.077 2815-2815/com.localplaydatesandroid I/LOGI 66: 49
2019-03-07 18:15:05.078 2815-2815/com.localplaydatesandroid I/LOGI 66: 50
2019-03-07 18:15:05.110 2815-2815/com.localplaydatesandroid I/LOGI 66: 47
2019-03-07 18:15:05.112 2815-2815/com.localplaydatesandroid I/LOGI 66: 48
2019-03-07 18:15:05.144 2815-2815/com.localplaydatesandroid I/LOGI 66: 45
2019-03-07 18:15:05.146 2815-2815/com.localplaydatesandroid I/LOGI 66: 46
2019-03-07 18:15:05.177 2815-2815/com.localplaydatesandroid I/LOGI 66: 43
2019-03-07 18:15:05.179 2815-2815/com.localplaydatesandroid I/LOGI 66: 44
2019-03-07 18:15:05.228 2815-2815/com.localplaydatesandroid I/LOGI 66: 41
2019-03-07 18:15:05.231 2815-2815/com.localplaydatesandroid I/LOGI 66: 42
2019-03-07 18:15:05.263 2815-2815/com.localplaydatesandroid I/LOGI 66: 39
2019-03-07 18:15:05.266 2815-2815/com.localplaydatesandroid I/LOGI 66: 40
2019-03-07 18:15:05.295 2815-2815/com.localplaydatesandroid I/LOGI 66: 37
2019-03-07 18:15:05.298 2815-2815/com.localplaydatesandroid I/LOGI 66: 38
2019-03-07 18:15:05.328 2815-2815/com.localplaydatesandroid I/LOGI 66: 35
2019-03-07 18:15:05.332 2815-2815/com.localplaydatesandroid I/LOGI 66: 36
2019-03-07 18:15:05.379 2815-2815/com.localplaydatesandroid I/LOGI 66: 33
2019-03-07 18:15:05.381 2815-2815/com.localplaydatesandroid I/LOGI 66: 34
2019-03-07 18:15:05.430 2815-2815/com.localplaydatesandroid I/LOGI 66: 31
2019-03-07 18:15:05.432 2815-2815/com.localplaydatesandroid I/LOGI 66: 32
2019-03-07 18:15:05.479 2815-2815/com.localplaydatesandroid I/LOGI 66: 29
2019-03-07 18:15:05.482 2815-2815/com.localplaydatesandroid I/LOGI 66: 30
2019-03-07 18:15:05.530 2815-2815/com.localplaydatesandroid I/LOGI 66: 27
2019-03-07 18:15:05.532 2815-2815/com.localplaydatesandroid I/LOGI 66: 28
2019-03-07 18:15:05.597 2815-2815/com.localplaydatesandroid I/LOGI 66: 25
2019-03-07 18:15:05.599 2815-2815/com.localplaydatesandroid I/LOGI 66: 26
2019-03-07 18:15:05.647 2815-2815/com.localplaydatesandroid I/LOGI 66: 23
2019-03-07 18:15:05.651 2815-2815/com.localplaydatesandroid I/LOGI 66: 24
2019-03-07 18:15:05.731 2815-2815/com.localplaydatesandroid I/LOGI 66: 21
2019-03-07 18:15:05.734 2815-2815/com.localplaydatesandroid I/LOGI 66: 22
2019-03-07 18:15:05.815 2815-2815/com.localplaydatesandroid I/LOGI 66: 19
2019-03-07 18:15:05.818 2815-2815/com.localplaydatesandroid I/LOGI 66: 20
2019-03-07 18:15:05.916 2815-2815/com.localplaydatesandroid I/LOGI 66: 17
2019-03-07 18:15:05.918 2815-2815/com.localplaydatesandroid I/LOGI 66: 18
2019-03-07 18:15:06.033 2815-2815/com.localplaydatesandroid I/LOGI 66: 15
2019-03-07 18:15:06.036 2815-2815/com.localplaydatesandroid I/LOGI 66: 16
2019-03-07 18:15:06.202 2815-2815/com.localplaydatesandroid I/LOGI 66: 13
2019-03-07 18:15:06.205 2815-2815/com.localplaydatesandroid I/LOGI 66: 14
2019-03-07 18:15:06.419 2815-2815/com.localplaydatesandroid I/LOGI 66: 11
2019-03-07 18:15:06.422 2815-2815/com.localplaydatesandroid I/LOGI 66: 12
2019-03-07 18:15:06.790 2815-2815/com.localplaydatesandroid I/LOGI 66: 9
2019-03-07 18:15:06.792 2815-2815/com.localplaydatesandroid I/LOGI 66: 10
2019-03-07 18:15:06.841 2815-2815/com.localplaydatesandroid I/LOGI 66: 7
2019-03-07 18:15:06.842 2815-2815/com.localplaydatesandroid I/LOGI 66: 8
2019-03-07 18:15:06.890 2815-2815/com.localplaydatesandroid I/LOGI 66: 5
2019-03-07 18:15:06.891 2815-2815/com.localplaydatesandroid I/LOGI 66: 6
2019-03-07 18:15:06.940 2815-2815/com.localplaydatesandroid I/LOGI 66: 3
2019-03-07 18:15:06.943 2815-2815/com.localplaydatesandroid I/LOGI 66: 4
2019-03-07 18:15:06.973 2815-2815/com.localplaydatesandroid I/LOGI 66: 1
2019-03-07 18:15:06.976 2815-2815/com.localplaydatesandroid I/LOGI 66: 2
2019-03-07 18:15:07.024 2815-2815/com.localplaydatesandroid I/LOGI 66: 7
2019-03-07 18:15:07.026 2815-2815/com.localplaydatesandroid I/LOGI 66: 8
2019-03-07 18:15:08.083 2815-2815/com.localplaydatesandroid I/LOGI gridadapter 94: send request
2019-03-07 18:15:08.084 2815-2815/com.localplaydatesandroid I/LOGI 69: 1
解决方案
如果其他人遇到此问题,这是我修复它的方法:
@Override
public View getView(int i, View basicGrid, ViewGroup viewGroup) {
if(items.get(i) instanceof Friend) return setFriendGridItem((basicGrid == null), (Friend) items.get(i));
return basicGrid;
}
所以我们检查当前位置的视图是否为空(即它是否仍然保存在内存中或已被删除)并将该布尔值传递给 setFriendGridItem 方法
然后在方法中我们说:
public View setFriendGridItem(Boolean setClicks,final Friend friend) {
View view = layoutInflator.inflate(R.layout.basic_friend_item, null);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,height);
view.setLayoutParams(layoutParams);
LinearLayout basic_profile = view.findViewById(R.id.basic_friend_profile);
if(!setClicks) {
basic_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), FriendActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("friend", friend);
view.getContext().startActivity(intent);
}
});
}
……
在这里,我们检查 setClicks 是否为 false(即视图为空),如果是,那么我们可以分配尚未设置的点击(因为没有视图可以设置它们)。
这现在适用于从 0 到无穷大(甚至更远!)的所有视图。
我认为正在发生的是,对于位置 0,视图被多次绘制(如日志所示),因此在 onclicks 注册之前创建视图然后重新创建,然后在这些重新绘制的某个地方,onclicks 被取消注册和视图不为空,因此它们不会被放回。
这对我来说不是 100% 有意义,它可能只是由于 Android 的生命周期而发生的异常之一——但如果其他人有这个问题,那么这里有一个解决方案。如果其他对此有更深入了解的人可以向我解释发生了什么,那就太棒了。
推荐阅读
- angularjs - HTTP 状态 405 – 不允许的方法 不支持请求方法“GET”
- asynchronous - AWS Lambda Nodejs async.waterfall 不执行 MQTT 函数
- c++ - 如何在 arduino 程序中存储 JSON 数组?
- php - 从字符串中提取数据
- c# - 无法使用 Selenium webdrive (c#) 选择复选框
- javascript - 如何在圆形图像中使用 CSS 制作彩虹边框?
- c# - 在 Excel 互操作对象的合并行中自动调整文本。C#
- c# - ASP.NET Core MVC 路由到带有参数的 url
- arrays - 多维数组上的 Fortran 操作 - 求和示例
- java - Spring-Cloud-Stream Kafka Azure - 获取数据时出现意外错误代码 13