在ListView中使用2个布局

时间:2014-12-02 05:44:56

标签: java android listview android-listview bitmap

我一直在尝试实现一个ListView,它根据行使用2个单独的Layouts。第一个布局有1个图像视图,第二个布局有2个图像视图。

我想这样做,以便我可以让ListView中的每一行包含交替的布局,但也能够使用ArrayList bitmapArray中的内容;

因此,例如,第一行将包含前2个位图图像,第二行将包含1个大位图图像,第3行将是并排2个位图图像......依此类推。

更新

我有点工作,问题仍然是图像没有正确分离。它只是重复使用了4张不同的图片并且奇怪地将它们分开......我将尝试在下面进行说明

每个数字代表BitmapArray中的一张图片(总共有20张图片,总数为0 - 19)

需要

[   0   ]
[ 1 , 2 ]
[   3   ]
[ 4,  5 ]
[   6   ]
[ 7,  8 ]
[   9   ]
[10 , 11]
[  12   ]
[13,  14]
[  15   ]
[16,  17]
[  18   ]
[ 19, --]

DER GOLEM的更新模式

[   0   ]
[ 1 , 2 ]
[   2   ]
[ 3 , 4 ]
REPEAT

** BitmapArray中总共存储了20个图像。我发现的另一个问题是,当我在后面的行上按OnItemClick(比如说10)时,它会带我到另一个放大位图的活动(按照设计)。奇怪的是它将是一个完全不同的图像,一个可能与数组中的位置对应的一个。 **

Here is the code that I have been working with.

    public class FeedAdapter extends ArrayAdapter<Bitmap> {

        private static final int TYPE_BIG_ITEM = 0;
        private static final int TYPE_SMALL_ITEM = 1;

        public FeedAdapter(Context context, ArrayList<Bitmap> bitmapArray) {
            super(context, R.layout.feed_listview_big);
        }

        private class ViewHolder {
            ImageView ivPictureSmallOne;
            ImageView ivPictureSmallTwo;
            ImageView ivPictureBig;
        }

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

        @Override
        public int getItemViewType(int position) {
            return position % 2;
        }

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

            Bitmap bmp = getItem(position);
            ViewHolder viewHolder;


            if (convertView == null) {
                viewHolder = new ViewHolder();
                LayoutInflater inflater = LayoutInflater.from(getContext());
                if (getItemViewType(position) == 0) {
                    convertView = inflater.inflate(R.layout.feed_listview_small,
                            null);
                    viewHolder.ivPictureSmallOne = (ImageView) convertView
                            .findViewById(R.id.ivSmall1);
                    viewHolder.ivPictureSmallTwo = (ImageView) convertView
                            .findViewById(R.id.ivSmall2);
                    viewHolder.ivPictureSmallOne.setImageBitmap(bmp);
                    viewHolder.ivPictureSmallTwo.setImageBitmap(getItem(position + 1));
                } else {
                    convertView = inflater.inflate(R.layout.feed_listview_big,
                            null);
                    viewHolder.ivPictureBig = (ImageView) convertView
                            .findViewById(R.id.ivBig);
                    viewHolder.ivPictureBig.setImageBitmap(bmp);
                }
            }
            // Return the completed view to render on screen
            return convertView;

        }
    }

2 个答案:

答案 0 :(得分:1)

尝试更改此

if (convertView == null) {
    viewHolder = new ViewHolder();
    LayoutInflater inflater = LayoutInflater.from(getContext());
    if (getItemViewType(position) == 0) {
        convertView = inflater.inflate(R.layout.feed_listview_small,
                null);
        viewHolder.ivPictureSmallOne = (ImageView) convertView
                .findViewById(R.id.ivSmall1);
        viewHolder.ivPictureSmallTwo = (ImageView) convertView
                .findViewById(R.id.ivSmall2);
        viewHolder.ivPictureSmallOne.setImageBitmap(bmp);
        viewHolder.ivPictureSmallTwo.setImageBitmap(getItem(position + 1));
    } else {
        convertView = inflater.inflate(R.layout.feed_listview_big,
                null);
        viewHolder.ivPictureBig = (ImageView) convertView
                .findViewById(R.id.ivBig);
        viewHolder.ivPictureBig.setImageBitmap(bmp);
    }
}
// Return the completed view to render on screen
return convertView;

if (convertView == null)
{
    viewHolder = new ViewHolder();
    LayoutInflater inflater = LayoutInflater.from(getContext());
    // Every 3rd image, load the "big" one
    if ((getItemViewType(position) % 3) == 0)
    {
        convertView = inflater.inflate(R.layout.feed_listview_big,
                null);
        viewHolder.ivPictureBig = (ImageView) convertView
                .findViewById(R.id.ivBig);
        viewHolder.ivPictureBig.setImageBitmap(bmp);
    }
    // If not dividable by 3, load the 2 "small" ones
    else
    {
        convertView = inflater.inflate(R.layout.feed_listview_small,
                null);
        viewHolder.ivPictureSmallOne = (ImageView) convertView
                .findViewById(R.id.ivSmall1);
        viewHolder.ivPictureSmallTwo = (ImageView) convertView
                .findViewById(R.id.ivSmall2);
        viewHolder.ivPictureSmallOne.setImageBitmap(bmp);
        viewHolder.ivPictureSmallTwo.setImageBitmap(getItem(position + 1));
    }
}
// Return the completed view to render on screen
return convertView;

答案 1 :(得分:0)

将此添加到您的获取视图中。

 position = MakeNewPosition(position);

make new position方法将为您提供新的更正位置。

 private int MakeNewPosition(int position)
    {
        return position+(position/2);
    }
希望这有效。试试