我有一个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;
}
所以我们检查当前位置的视图是否为null(即它是保存在内存中还是已被删除)并将该布尔值传递给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是否为假(即视图为空),如果是,那么我们可以分配尚未设置的点击(因为没有视图将它们设置为)。
这现在适用于从0到无穷大(以及更远!)的所有视图。
我认为正在发生的是,对于位置0,视图被多次绘制(如日志所示),因此视图被创建,然后在onclicks注册之前重新创建,然后在那些重新绘制onclicks的某个地方获得未注册和视图不为null,因此不会放回。
它对我来说没有任何意义,它可能只是由于Android生命周期而发生的异常之一 - 但如果其他人有这个问题,那么这是一个解决方案。如果对此有更深入了解的其他人可以向我解释这是多么美妙的事情。