2012-11-22 36 views
0

我根据一些例子拼凑在一起,结果是混乱的混乱。有图像/文字相互叠加,像单个列表查看项目一样来回移动: 没有任何图像列表。我的目标是在其下方显示文字视图的图像。就这些。请注意:我也尝试从适配器中的getView()返回一个夸大的视图,结果大致相同。我开始怀疑Gallery是否真的可以支持ImageView作为类型。图库项目视图集群不能正确显示

package com.example.elgallery; 

import java.util.ArrayList; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

private GalleryAdapter mGalleryAdapter; 
private ArrayList<GalleryItem> mGalleryItems; 
private Gallery mGallery; 

public class GalleryItem{ 
    int imageId=R.drawable.ic_launcher; 
    String caption; 
    public int getImageId() { 
    return imageId;  } 

public String getCaption() { 
    return caption; 
} 
    public GalleryItem(int i,String s) { 
    imageId=i; 
    caption=s; 
    } 
} 




    int[] resourceImages =  {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher, 
     R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher}; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
GalleryItem[] item = new GalleryItem[6]; 
mGalleryItems = new ArrayList<GalleryItem>(); 
    //initialising all items, change member variables according to needs 
    for(int i=0;i<6;i++){ 
     mGalleryItems.add(new GalleryItem(resourceImages[i], "pic no" +(i+1)));   
    } 

mGallery = (Gallery)findViewById(R.id.gallery); 
mGalleryAdapter = new GalleryAdapter(this); 
mGalleryAdapter.setGalleryItems(mGalleryItems); 
mGallery.setAdapter(mGalleryAdapter); 

mGalleryAdapter.notifyDataSetChanged(); 

} 

private class GalleryAdapter extends BaseAdapter { 

    private Context mContext; 
    mArrayList<GalleryItem> galleryItems; 

    public ArrayList<GalleryItem> getGalleryItems() { 
    return galleryItems; 
} 
public void setGalleryItems(ArrayList<GalleryItem> galleryItems) { 
    this.galleryItems = galleryItems; 
} 
public GalleryAdapter (Context context) 
{ 
    mContext = context; 
} 
@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return galleryItems.size(); 
} 

@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return galleryItems.get(position); 
} 

@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LinearLayout ll = new LinearLayout(mContext); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    ImageView i = new ImageView(ll.getContext()); 
    i.setTag("someTage"); //i.setImageURI(mUrls[position]); 
    i.setImageResource(R.drawable.ic_launcher); 
    i.setScaleType(ImageView.ScaleType.FIT_XY); 
    i.setLayoutParams(new Gallery.LayoutParams(48, 48)); 
    TextView tv = new TextView(ll.getContext()); 
    tv.setTag("someTag2"); tv.setText("someText"); 
    tv.setLayoutParams(new Gallery.LayoutParams(48, 48)); 
    ll.addView(tv); 
    return ll; 
} 
    } 

} 

这里是XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<Gallery 
    android:id="@+id/gallery" 
    android:layout_width="fill_parent" 
    android:layout_height="200dip" 
    /> 
</LinearLayout> 

结果:一个混乱的烂摊子。看起来像三个图像和文字视图在彼此的顶部。

+0

为什么这个工作,但我的没有:http://stackoverflow.com/questions/6448107/android-gallery-with-caption –

+0

这就是我称之为有用的答案:http://stackoverflow.com/ questions/6448107/android-gallery-with-caption关于这个例子的一切工作。但只是想知道为什么我的表现不正确。它是ViewHolder吗? –

回答

0

我的目标是一张带有文字视图的图像。

您目前的getView方法不会这样做,因为您有一些错误。首先我没有看到任何地方你将ImageView添加到LinearLayout包装。其次,这些视图的LayoutParams的类型不是Gallery.LayoutParams,因为视图是LinearLayout的子项。检查下面的代码:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LinearLayout ll = new LinearLayout(mContext); 
     ll.setLayoutParams(new Gallery.LayoutParams(
       Gallery.LayoutParams.MATCH_PARENT, 
       Gallery.LayoutParams.MATCH_PARENT)); 
     ll.setOrientation(LinearLayout.VERTICAL); 
     ImageView i = new ImageView(ll.getContext()); 
     i.setTag("someTage"); // i.setImageURI(mUrls[position]); 
     i.setImageResource(R.drawable.ic_launcher); 
     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     i.setLayoutParams(new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT)); 
     ll.addView(i); 
     TextView tv = new TextView(ll.getContext()); 
     tv.setTag("someTag2"); 
     tv.setText("someText"); 
     tv.setLayoutParams(new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT)); 
     ll.addView(tv);   
     return ll; 
    } 

我也建议你阅读有关优化Android适配器。