ListView getView - 如果(convertView == null)未在第一次滚动时调用

时间:2015-07-28 20:53:52

标签: android android-listview

当记录首次出现在ListView时,它们似乎正常,并且首次显示记录时正确调用getView if (convertView == null)
但是,如果我滚动ListView,即使是第一次,也不会调用if (convertView == null)。因为顶部项目中的孩子在滚动上出现的第一项中重复。根据我的理解,如果首次滚动ListView,则应调用if (convertView == null)
如果我错了,请纠正我 getView:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try {
            ViewHolder holder = null;


            int rowType = getItemViewType(position);
            if (convertView == null) {


                holder = new ViewHolder();

                switch (rowType) {
                    case TYPE_ITEM:
                        convertView = inflater.inflate(R.layout.activity_item, null);

                        activityModel = (truegroups.model.Activity) arrActivity.get(position);

                        holder.activityLayout = (RelativeLayout) convertView.findViewById(R.id.activity_item_layout);
                        holder.startDate = (TextView) convertView.findViewById(R.id.lblStartDate);
                        holder.line = (TextView) convertView.findViewById(R.id.lblLine);
                        holder.endDate = (TextView) convertView.findViewById(R.id.lblEndDate);
                        holder.groupName = (TextView) convertView.findViewById(R.id.lblGroupName);
                        holder.activity = (TextView) convertView.findViewById(R.id.lblActivity);
                        holder.location = (TextView) convertView.findViewById(R.id.lblLocation);
                        holder.topInviteeLayout = (RelativeLayout) convertView.findViewById(R.id.invitationTopPanel);

                        int iCount = 0;
                        for (Invitation invitation : activityModel.getArrInvitations()) {

                            ImageView imgTopStatus = new ImageView(activity);

                            RelativeLayout.LayoutParams imgStatusParams = new RelativeLayout.LayoutParams(40, 40);
                            imgStatusParams.setMarginStart(160);
                            imgStatusParams.setMargins(160, 20, 0, 0);

                            if (iCount > 0)
                                imgStatusParams.addRule(RelativeLayout.BELOW, holder.arrImageStatus.get(iCount - 1).getId());

                            imgTopStatus.setId(iCount * iCount + iCount + 1);
                            holder.topInviteeLayout.addView(imgTopStatus, imgStatusParams);
                            holder.arrImageStatus.add(imgTopStatus);

                            TextView topInviteeName = new TextView(activity);

                            RelativeLayout.LayoutParams topInviteeParams = new RelativeLayout.LayoutParams(300, ViewGroup.LayoutParams.WRAP_CONTENT);
                            topInviteeParams.addRule(RelativeLayout.RIGHT_OF, imgTopStatus.getId());
                            if (iCount > 0)
                                topInviteeParams.addRule(RelativeLayout.BELOW, holder.arrlblTopInviteeName.get(iCount - 1).getId());


                            topInviteeParams.setMargins(15, 5, 5, 15);
                            topInviteeName.setTextColor(Color.BLACK);
                            topInviteeName.setTextSize(14);
                            topInviteeName.setBackgroundColor(Color.WHITE);
                            topInviteeName.setId(iCount * iCount + 2 * iCount + 2);
                            //topInviteeName.setText(invitation.getChildName());
                            holder.topInviteeLayout.addView(topInviteeName, topInviteeParams);
                            holder.arrlblTopInviteeName.add(topInviteeName);

                            //topInviteeParams.addRule(RelativeLayout.ALIGN_TOP, imgTopStatus.getId());
                           // imgStatusParams.addRule(RelativeLayout.ALIGN_BASELINE, topInviteeName.getId());

                            Button decline = new Button(activity);

                            RelativeLayout.LayoutParams paramsDecline = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 80);
                            paramsDecline.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                            if (iCount > 0)
                                paramsDecline.addRule(RelativeLayout.BELOW, holder.arrDecline.get(iCount - 1).getId());

                            decline.setTextColor(Color.BLACK);
                            decline.setTextSize(9);
                            decline.setText("Decline");
                            decline.setId(iCount * iCount + 3 * iCount + 3);
                            holder.topInviteeLayout.addView(decline, paramsDecline);
                            holder.arrDecline.add(decline);

                            Button accept = new Button(activity);

                            RelativeLayout.LayoutParams paramsAccept = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 80);

                            if (iCount > 0)
                                paramsAccept.addRule(RelativeLayout.BELOW, holder.arrAccept.get(iCount - 1).getId());
                            paramsAccept.addRule(RelativeLayout.RIGHT_OF, topInviteeName.getId());

                           // paramsAccept.addRule(RelativeLayout.ALIGN_BOTTOM, decline.getId());
                            paramsAccept.setMarginStart(155);

                            accept.setTextColor(Color.BLACK);
                            accept.setTextSize(9);
                            accept.setId(iCount * iCount + 8 * iCount + 8);
                            accept.setText("Accept");
                            holder.topInviteeLayout.addView(accept, paramsAccept);
                            holder.arrAccept.add(accept);

                            paramsDecline.addRule(RelativeLayout.ALIGN_TOP, accept.getId());

                            iCount++;
                        }

                        break;
                    case TYPE_SEPARATOR:
                        convertView = inflater.inflate(R.layout.activity_header, null);

                        holder.header = (TextView) convertView.findViewById(R.id.textSeparator);
                        holder.headerWeek = (TextView) convertView.findViewById(R.id.textWeekNumber);
                        break;
                }

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (arrActivity.size() <= 0) {
                Log.d("Data", "No data found !!");

            } else {
                switch (rowType) {
                    case TYPE_ITEM:
                        activityModel = (truegroups.model.Activity) arrActivity.get(position);
//
                        holder.groupName.setText(activityModel.getGroupName());
                        holder.activity.setText(activityModel.getTitle());
                        holder.location.setText(activityModel.getLocation());
                        holder.startDate.setText(getHours(activityModel.getStartDate()));
                        holder.endDate.setText(getHours(activityModel.getEndDate()));



                        int iCount = 0;
                        for (Invitation invitation : activityModel.getArrInvitations()) {
                           ImageView imgStatus = holder.arrImageStatus.get(iCount);
                            //ImageView imgStatus = (ImageView) holder.topInviteeLayout.getChildAt(0);
                            if (invitation.getResponse().equals("3"))
                                imgStatus.setImageResource(R.drawable.notresponded);
                            else if (invitation.getResponse().equals("1"))
                                imgStatus.setImageResource(R.drawable.attending);
                            else
                                imgStatus.setImageResource(R.drawable.decline);

                           TextView inviteeName = holder.arrlblTopInviteeName.get(iCount);
                            //TextView inviteeName = (TextView) holder.topInviteeLayout.getChildAt(1);
                            inviteeName.setText(invitation.getChildName());

                            iCount++;
                        }


                        convertView.setOnClickListener(new OnItemClickListener(position));
                        break;
                    case TYPE_SEPARATOR:

                        holder.header.setText(getHeaderDate(String.valueOf(arrActivity.get(position))));

                        Calendar calendarGivenDate = Calendar.getInstance();
                        calendarGivenDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(String.valueOf(arrActivity.get(position)).split("/")[1]));
                        calendarGivenDate.set(Calendar.MONTH, Integer.parseInt(String.valueOf(arrActivity.get(position)).split("/")[0]));
                        calendarGivenDate.set(Calendar.YEAR, Integer.parseInt(String.valueOf(arrActivity.get(position)).split("/")[2]));

                        int weekNumber = calendarGivenDate.get(Calendar.WEEK_OF_YEAR);
                        holder.headerWeek.setText(StringUtils.join("WEEK ", String.valueOf(weekNumber)));

                        break;
                }

            }

            return convertView;
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(activity, e.getMessage(), Toast.LENGTH_LONG).show();
            Log.d("Exception : ", e.getMessage());
            return convertView;
        }
    }

1 个答案:

答案 0 :(得分:2)

由于ListView实现了视图回收,因此在初始化后convertView == null永远不会出现getView

当您初始化ListView时,它会在其上创建行并调用getView(),直到屏幕满了行。假设这个数字是10行,编号为0-9,第10行只是部分可见。向下滚动时,第0行将滚动到视图之外,并且新行需要显示在底部。而不是为第10行初始化新的内存块,而是采用第0行并在if (convertView == null) { convertView = new WhateverViewTypeYouNeed(); } if (convertView != SEPARATOR) { initializeValues(); } return convertView; 函数中将其提供给您。如果您不修改这些值,它将再次显示在列表的底部,并显示第0行的所有原始值。在此示例中,您唯一的时间将获得null convertView ,如果您正在使用多种类型的对象(如分隔符),并且滚动实际上需要在上一行滚动屏幕之前创建一个新行。

因此,如果converView为null,则需要首先创建一行,但需要与之后(重新)初始化视图中的所有值之后分别创建一行。

编写getView函数的最佳策略遵循以下基本结构:

fxml

您可以查看this link进一步阅读。