ListView第一项重叠

时间:2015-08-04 11:17:09

标签: android listview android-listview

我的ListView在第一个项目中显示其他内容作为ListView

的其余部分

问题是,它取代了第一个项目而不是自己的位置。

示例如何:

  

A(与其他人不同)

     

B

     

     

它是怎么回事:

  

A(B覆盖A)

     

     

如您所见,一个B缺失,因为它被A覆盖。

这是守则。我写了自己的CustomAdapter:

@Override
public int getCount() {
    if (taskItems.size() + 1 >= 0)
        return taskItems.size();
    return 0;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

public static class ViewHolder {
    public TextView taskTitleTextView;
    public TextView taskObjectTextView;
    public TextView taskLocationTextView;
    public ImageView taskImageImageView;

}

public static class ViewHolderProjects {
    public TextView projectTitelTextView;
    public TextView projectInfoTextView;
    public TextView projectDeadline;
    public ImageView projectImageImageView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolderProjects holderProjects;
    if (position == 0) {
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_item_projects, null);
            holderProjects = new ViewHolderProjects();

            holderProjects.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
            holderProjects.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
            holderProjects.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
            holderProjects.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);

            vi.setTag(holderProjects);
        } else {
            holderProjects = (ViewHolderProjects) vi.getTag();
        }
        if (projectMap.size() <= 0) {
            holderProjects.projectInfoTextView.setText("Keine Infos");
            holderProjects.projectTitelTextView.setText("Kein Titel");
        } else {
            if (projectMap.get(position).containsKey("project_company_image")) {
                if (projectMap.get(position).get("project_company_image").getBytes().length == 0) {
                    holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
                    holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
                    return vi;
                }
                Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(projectMap.get(position).get("project_company_image")));
                holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
                holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
                holderProjects.projectImageImageView.setImageBitmap(bm);
            }
        }
    } else {

        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_item_tasks, null);

            holder = new ViewHolder();
            holder.taskTitleTextView = (TextView) vi.findViewById(R.id.taskTitle);
            holder.taskObjectTextView = (TextView) vi.findViewById(R.id.taskObject);
            holder.taskLocationTextView = (TextView) vi.findViewById(R.id.taskLocation);
            holder.taskImageImageView = (ImageView) vi.findViewById(R.id.taskImage);

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

        if (taskItems.size() <= 0) {
            holder.taskTitleTextView.setText("Kein Titel");
            holder.taskObjectTextView.setText("Kein Objekt");
            holder.taskLocationTextView.setText("Kein Standort");
        } else {
            if (taskItems.get(position).containsKey("object_image")) {
                if (taskItems.get(position).get("object_image").getBytes().length == 0) {
                    holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
                    holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
                    holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));

                    return vi;
                }
            }
            Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(taskItems.get(position).get("object_image")));
            holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
            holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
            holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));
            holder.taskImageImageView.setImageBitmap(bm);
        }
    }
    return vi;

}

3 个答案:

答案 0 :(得分:1)

  1. 首先将您的行布局扩展到视图中:

    LayoutInflater inflater = getLayoutInflater();

    ViewGroup header = (ViewGroup)inflater.inflate(<R.layout.header>, myListView, false);

  2. 现在,您可以获取视图的每个组件,并可以将值分配给它们..eg:

    TextView tv = header.findViewById(<R.id.text>); tv.setText("Something");

  3. 现在只需将该标题传递给列表视图。

    myListView.addHeaderView(header, null, false);

  4. 向该标头添加侦听器。只需将listnener传递给该视图组:

    header.setOnClickListener(<pass a new OnClickListener Instance>);

  5. 之后,您将代码添加到列表视图中。

    myListView.setAdapter(<your adapter instance>);

答案 1 :(得分:0)

如果在CustomAdapter的getView中存在膨胀视图中的条件,那么您必须 覆盖额外的两种方法..

声明一个名为&#34的整数;输入&#34;全局适配器类

int type;

覆盖这两个功能

@Override
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    if(position == 0){
       return 0;
    }else{
        return 1;
    }
    return super.getItemViewType(position);
}

对getView函数稍作更改,如以下代码所示

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolderProjects holderProjects;
    type = getItemViewType(position);
    if (type == 0) {
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_item_projects, null);
            holderProjects = new ViewHolderProjects();

            holderProjects.projectTitelTextView = (TextView) vi.findViewById(R.id.projectTitle);
            holderProjects.projectInfoTextView = (TextView) vi.findViewById(R.id.projectInfo);
            holderProjects.projectImageImageView = (ImageView) vi.findViewById(R.id.projectImage);
            holderProjects.projectDeadline = (TextView) vi.findViewById(R.id.projectdeadline);

            vi.setTag(holderProjects);
        } else {
            holderProjects = (ViewHolderProjects) vi.getTag();
        }
        if (projectMap.size() <= 0) {
            holderProjects.projectInfoTextView.setText("Keine Infos");
            holderProjects.projectTitelTextView.setText("Kein Titel");
        } else {
            if (projectMap.get(position).containsKey("project_company_image")) {
                if (projectMap.get(position).get("project_company_image").getBytes().length == 0) {
                    holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
                    holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
                    return vi;
                }
                Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(projectMap.get(position).get("project_company_image")));
                holderProjects.projectTitelTextView.setText(jobMap.get(position).get("job_subject"));
                holderProjects.projectDeadline.setText(jobMap.get(position).get("job_deadline"));
                holderProjects.projectImageImageView.setImageBitmap(bm);
            }
        }
    } else {

        ViewHolder holder;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.list_item_tasks, null);

            holder = new ViewHolder();
            holder.taskTitleTextView = (TextView) vi.findViewById(R.id.taskTitle);
            holder.taskObjectTextView = (TextView) vi.findViewById(R.id.taskObject);
            holder.taskLocationTextView = (TextView) vi.findViewById(R.id.taskLocation);
            holder.taskImageImageView = (ImageView) vi.findViewById(R.id.taskImage);

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

        if (taskItems.size() <= 0) {
            holder.taskTitleTextView.setText("Kein Titel");
            holder.taskObjectTextView.setText("Kein Objekt");
            holder.taskLocationTextView.setText("Kein Standort");
        } else {
            if (taskItems.get(position).containsKey("object_image")) {
                if (taskItems.get(position).get("object_image").getBytes().length == 0) {
                    holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
                    holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
                    holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));

                    return vi;
                }
            }
            Bitmap bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(taskItems.get(position).get("object_image")));
            holder.taskTitleTextView.setText(taskItems.get(position).get("task_headline"));
            holder.taskObjectTextView.setText(taskItems.get(position).get("object_name"));
            holder.taskLocationTextView.setText(taskItems.get(position).get("object_location"));
            holder.taskImageImageView.setImageBitmap(bm);
        }
    }
    return vi;

}

答案 2 :(得分:0)

您可以像此一样将标题设置为ListView

View headerView = getLayoutInflater().inflate(R.layout.header_view, null, false);
listview.addHeaderView(headerView);

注意:您必须在设置适配器

之前设置标题视图