我根据一些例子将它拼凑在一起,结果是乱七八糟的混乱。图像/文本相互叠加,像单个列表视图项一样来回移动: 没有像图像列表。我的目标是在其下方显示文本视图的图像。这就是全部了。请注意:我也尝试从适配器中的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 :(得分: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适配器的信息。