BaseAdapter中没有图像的好位置

时间:2013-04-02 10:09:39

标签: android image baseadapter

我有一个包含5个部分的PullToRefresh ListView(每个部分是列表中的一个项目),每个部分都有许多照片。我有一个BaseAdapter类,用截面照片填充5个部分。问题是当我第一次加载列表视图时,照片没有正确显示,但是当我滚动时,照片会显示在正确的位置。 我使用BitmapFun代码加载照片。

我有:

private ArrayList<Pair<Integer, ArrayList<String>>> section_photos_url;

第一个值是部分,第二个是每个部分的照片数组。

getView方法:

public View getView(int position, View convertView, ViewGroup container) {

    if (position < 5) {

        ItemViewHolder viewHolder;

        // Si la vista de dicha posicion ha sido inflada
        if (view_array[position] == null) { 

            // Inflate the view
            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            switch (section_photos_url.get(position).first) {
            case ConstantsTypeSection.SECTION1:
                view_array[position] = li.inflate(R.layout.timeline_section_today_trending, container, false);
                break;
            case ConstantsTypeSection.SECTION2:
                view_array[position] = li.inflate(R.layout.timeline_section_trendy_smiler, container, false);
                break;
            case ConstantsTypeSection.SECTION3:
                view_array[position] = li.inflate(R.layout.timeline_section_around_me, container, false);
                break;
            case ConstantsTypeSection.SECTION4:
                view_array[position] = li.inflate(R.layout.timeline_section_trending_hashtag, container, false);
                break;
            case ConstantsTypeSection.SECTION5:
                view_array[position] = li.inflate(R.layout.timeline_section_last_content, container, false);
                break;
            }

            viewHolder = new ItemViewHolder();

            viewHolder.sectionName = (TextView) view_array[position].findViewById(R.id.section_name);
            viewHolder.userName = (TextView) view_array[position].findViewById(R.id.user_name);
            viewHolder.hashTag = (TextView) view_array[position].findViewById(R.id.hash_tag_name);

            viewHolder.userImage = (ImageView) view_array[position].findViewById(R.id.user_photo);

            viewHolder.item1 = (RecyclingImageView) view_array[position].findViewById(R.id.item_1);
            viewHolder.item2 = (RecyclingImageView) view_array[position].findViewById(R.id.item_2);
            viewHolder.item3 = (RecyclingImageView) view_array[position].findViewById(R.id.item_3);
            viewHolder.item4 = (RecyclingImageView) view_array[position].findViewById(R.id.item_4);
            viewHolder.item5 = (RecyclingImageView) view_array[position].findViewById(R.id.item_5);
            viewHolder.item6 = (RecyclingImageView) view_array[position].findViewById(R.id.item_6);
            viewHolder.item7 = (RecyclingImageView) view_array[position].findViewById(R.id.item_7);
            viewHolder.photos_layout = (LinearLayout) view_array[position].findViewById(R.id.photo_section);

            view_array[position].setTag(viewHolder);
        } else {
            viewHolder =  (ItemViewHolder) view_array[position].getTag();
        }

        switch (section_photos_url.get(position).first) {
        case ConstantsTypeSection.SECTION1:
                 viewHolder.sectionName.setText(context.getResources().getString(R.string.today_trending_section));

          LinearLayout.LayoutParams lp = new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,    LinearLayout.LayoutParams.WRAP_CONTENT);
        viewHolder.photos_layout.addView(getPhotoView(section_photos_url.get(0).second), lp);

            break;
        case ConstantsTypeSection.SECTION2:
            viewHolder.sectionName.setText(context.getResources().getString(R.string.trendy_smiler_section));

                          LinearLayout.LayoutParams lp = new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,    LinearLayout.LayoutParams.WRAP_CONTENT);
        viewHolder.photos_layout.addView(getPhotoView(section_photos_url.get(1).second), lp);

            break;
        case ConstantsTypeSection.SECTION3:
            viewHolder.sectionName.setText(context.getResources().getString(R.string.around_me_section));

                          LinearLayout.LayoutParams lp = new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,    LinearLayout.LayoutParams.WRAP_CONTENT);
        viewHolder.photos_layout.addView(getPhotoView(section_photos_url.get(2).second), lp);
            break;
        case ConstantsTypeSection.SECTION4:
            viewHolder.sectionName.setText(context.getResources().getString(R.string.trending_hashtag_section));

                          LinearLayout.LayoutParams lp = new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,    LinearLayout.LayoutParams.WRAP_CONTENT);
        viewHolder.photos_layout.addView(getPhotoView(section_photos_url.get(3).second), lp);
            break;
        case ConstantsTypeSection.SECTION5:
            viewHolder.sectionName.setText(context.getResources().getString(R.string.last_content_section));


                          LinearLayout.LayoutParams lp = new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,    LinearLayout.LayoutParams.WRAP_CONTENT);
        viewHolder.photos_layout.addView(getPhotoView(section_photos_url.get(0).second), lp);
            break;
        }

        return view_array[position];

    } else {
        return null;
    }
}

private class ItemViewHolder {
    TextView sectionName;
    TextView userName;
    TextView hashTag;
    ImageView userImage;

    RecyclingImageView item1;
    RecyclingImageView item2;
    RecyclingImageView item3;
    RecyclingImageView item4;
    RecyclingImageView item5;
    RecyclingImageView item6;
    RecyclingImageView item7;

    LinearLayout photos_layout;

}


  private View getPhotoView(ArrayList<String> photos) {

    View view = null;

    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    switch (photos.size()) {
    case 1:
        break;
    case 3:
        view = li.inflate(R.layout.timeline_item_three, null, false);
        break;
    case 5:
        if (Math.random() * 2 < 1) {
            view = li.inflate(R.layout.timeline_item_three_two, null, false);
        } else {
            view = li.inflate(R.layout.timeline_item_two_three, null, false);
        }
        break;
    case 7:
    default:
        view = li.inflate(R.layout.timeline_item_seven, null, false);
        break;
    }

    RecyclingImageView image = null;
    for (int i = 0; i < photos.size(); i++) {
        switch (i) {
        case 0:
              image =  (RecyclingImageView) view.findViewById(R.id.item_1);
            break;
        case 1:
            image =  (RecyclingImageView) view.findViewById(R.id.item_2);
            break;
        case 2:
            image =  (RecyclingImageView) view.findViewById(R.id.item_3);
            break;
        case 3:
            image =  (RecyclingImageView) view.findViewById(R.id.item_4);
            break;
        case 4:
            image =  (RecyclingImageView) view.findViewById(R.id.item_5);
            break;
        case 5:
            image =  (RecyclingImageView) view.findViewById(R.id.item_6);
            break;
        case 6:
            image =  (RecyclingImageView) view.findViewById(R.id.item_7);
            break;
        }

        CacheLoaderImagesSingleton.getInstance().getImageFetcher().loadImage(photos.get(i), image);
    }

    return view;
}

有关正确显示图像的建议吗?那是图像。第一个是错误的图像,因为所有照片都必须是紫色的花朵照片,而不是黄色。第二个是正确的。图像是部分的一部分

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

首先,如果您使用view_array将视图存储在列表中的位置,那么什么是view_array。那就错了。实现该列表以重用视图。

其次,对于列表视图具有不同的布局,可以使用getItemViewType()和getViewTypeCount()。

见下面的例子,

public class Adapter extends BaseAdapter {

    private static final int SECTION1 = 0;
    private static final int SECTION2 = 1;
    private static final int SECTION3 = 2;
    private static final int SECTION4 = 3;
    private static final int SECTION5 = 4;

    ...

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

        View view;

        int type = getItemViewType(position);

        if (convertView == null)
        {
            switch (type) {
            case SECTION1:
                view = inflater.inflate(layout_id1, parent, false);
                break;
            case SECTION2:
                view = inflater.inflate(layout_id2, parent, false);
                break;
            case SECTION3:
                view = inflater.inflate(layout_id3, parent, false);
                break;
            case SECTION4:
                view = inflater.inflate(layout_id4, parent, false);
                break;
            case SECTION5:
                view = inflater.inflate(layout_id5, parent, false);
                break;
            }
        }
        else
        {
            // Based on value of getItemViewType the ListView will pass the 
            // corresponding layout which was inflated before by getView so no need to inflate again
            view = convertView;
        }

        ...

        return view;
    }

    // getItemViewType should return value from 0 to 4 since the count is 5
    // See doc for more info
    @Override
    public int getItemViewType(int position) {
        // return the section type which should be in range 0 to 4
    }

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