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;
}

因此,我们检查当前位置的视图是否为null(即它仍然保存在存储器中还是已被删除),并将该值传递给SetFriendfriditem方法

然后在我们说的方法中:

 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(即视图为null),如果是尚未设置单击(因为尚无视图可以将它们设置为),我们可以分配点击。

现在,这在从0到无限的所有视图上都起作用(及以后!)。

我认为发生的事情是,对于位置0,视图不止一次(如日志所示),因此在注册onclicks注册之前创建并重新创建视图,然后在这些重新绘制的某个地方进行onclicks。没有注册,视图不是空的,所以它们不会放回去。

这对我来说并没有100%的意义,这可能只是由于Android中的生命周期而发生的那些异常之一 - 但是如果其他人遇到了这个问题,那么这就是解决方案。如果其他对此有更深入了解的人可以向我解释发生的事情。

最新更新