0

5 つのセクション (各セクションはリストの項目) を持つ PullToRefresh ListView があり、各セクションには多数の写真があります。5 つのセクションをセクションの写真で埋める BaseAdapter クラスがあります。問題は、リストビューを初めて読み込んだときに写真が正しく表示されないことですが、スクロールすると写真が正しい位置に表示されます。写真の読み込みには BitmapFun コードを使用します。

私は持っている:

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

最初の値はセクションで、2 番目の値は各セクションの写真の配列です。

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

画像を正しく表示するための提案はありますか? それらはイメージです。最初の 1 つは間違った画像です。すべての写真は紫色の花の写真でなければならず、黄色であってはなりません。2番目が正しいです。画像はセクションの一部です

ここに画像の説明を入力 ここに画像の説明を入力

4

1 に答える 1

0

まず、リスト内の位置にビューを格納するためにそれを使用している場合、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;
    }
}
于 2013-04-02T10:45:02.190 に答える