Android:listview中的多个画廊

时间:2013-02-12 10:05:32

标签: android listview gallery

我正在努力在列表视图中实现多个画廊。

listview有一个适配器(BaseAdapter),用数据填充列表。 适配器包含textview(指定库的名称)和库。 在这个适配器中,我实例化了Gallery对象,并将一个新的适配器(用图像填充图库)附加到库对象。

问题如下: 每行都填充了一个名称和一个图库对象 - 这很好。 但每个图库只有一个图像而不是10个图像(测试数据)。

代码段:

用于填充列表视图的适配器

public class Adapter_GalleryList extends BaseAdapter
{
private static final String ERRORTAG = "ERROR_CHECKING --> ";   


//Components
private Context context;


//Global variables
private Object_Event[] events;


public Adapter_GalleryList(Context context, Object_Event[] events)
{
    this.context = context;
    this.events = events;
}


public int getCount()
{
    return events.length;
}


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


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


public View getView(int position, View convertView, ViewGroup parent)
{
    //Inflate the each row of the list with the gallerylist layout
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.row_gallerylist, parent , false);


    //Configure a typeface for the text headings
    Typeface quackery = Typeface.createFromAsset(context.getAssets(), "fonts/tpf_quackery.ttf");


    //Instantiate the row's components
    TextView textEventName = (TextView) rowView.findViewById(R.id.event_name);
    Gallery eventGallery = (Gallery) rowView.findViewById(R.id.gallery);


    //Populate the components with data
    textEventName.setTypeface(quackery);
    textEventName.setText(events[position].Name);


    Integer[] images = new Integer[10]; 
    images[0] = R.drawable.button_events;
    images[1] = R.drawable.button_events;
    images[2] = R.drawable.button_events;
    images[3] = R.drawable.button_events;
    images[4] = R.drawable.button_events;
    images[5] = R.drawable.button_events;
    images[6] = R.drawable.button_events;
    images[7] = R.drawable.button_events;
    images[8] = R.drawable.button_events;
    images[9] = R.drawable.button_events;

    Adapter_EventGallery adapter = new Adapter_EventGallery(context,images);
    eventGallery.setAdapter(adapter);


    //OnClickListener for images in the gallery
    eventGallery.setOnItemClickListener(new OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id)
        {
            Toast.makeText(context, "" + position, Toast.LENGTH_SHORT).show();
        }
    });



    return rowView;
}
}

用于填充独立图库的适配器

public class Adapter_EventGallery extends BaseAdapter
{
private static final String ERRORTAG = "ERROR_CHECKING --> ";

//Component variables
    private Context adapterContext;



private Integer[] images;


public Adapter_EventGallery(Context passedContext, Integer[] images)
{
    adapterContext = passedContext;
    this.images = images;
}


public int getCount()
{
    Log.i(ERRORTAG, "Length: " + images.length);
    return images.length;
}


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


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


public View getView(int position, View convertView, ViewGroup parent)
{
    Log.i(ERRORTAG, "EventGallery Position: " + position);

    ImageView galleryImage = new ImageView(adapterContext);

    galleryImage.setImageResource(images[position]);
    galleryImage.setLayoutParams(new Gallery.LayoutParams(250, 250));
    galleryImage.setScaleType(ImageView.ScaleType.FIT_XY);
    //galleryImage.setBackgroundResource(mGalleryItemBackground);

    return galleryImage;
}

}

任何想法或指示?

1 个答案:

答案 0 :(得分:0)

在KarolGusak的帮助下,我设法解决了这个问题。 问题实际上是画廊没有像Karol Gusak指出的那样跨越整个宽度。

一个菜鸟的错误......