应用图片来自http://imgur.com/6A92Yl6
所以我设置了一个图库,通过点击,用户可以从视图中选择一张图片,然后用它来查看图片及其相应的烤面包。
问题是弹出的图像只有三个。我不知道为什么,并且没有限制画廊的变量,所以我不知所措。
我的主要人物:
public class MainActivity<View> extends Activity
{
ImageView selectedImage;
private Integer[] mImageIds = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
R.drawable.image7,
R.drawable.image8,
R.drawable.image9,
R.drawable.image10
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
selectedImage=(ImageView)findViewById(R.id.imageView1);
gallery.setSpacing(1);
gallery.setAdapter(new GalleryImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "THIS TEXT SHOULD NEVER SHOW = " + position, Toast.LENGTH_SHORT).show();
// show the selected Image
selectedImage.setImageResource(mImageIds[position]);
}
@Override
public void onItemClick(AdapterView<?> parent, android.view.View v, int position, long id) {
Toast.makeText(MainActivity.this, "Picture Number: " + position, Toast.LENGTH_SHORT).show();
// show the selected Image
selectedImage.setImageResource(mImageIds[position]);
}
});
}
}
MY 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"
android:orientation="vertical" >
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:layout_weight="0.13"
android:src="@drawable/image1" />
</LinearLayout>
答案 0 :(得分:1)
我猜你在创建新的GalleryImageAdapter时忘了用mImageIds填充适配器。
答案 1 :(得分:1)
您需要使用适配器:
将适配器调用更改为:
gallery.setAdapter(new GalleryImageAdapter(this,mImageIds));
添加/更新 GalleryImageAdapter.java:
import java.util.List;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryImageAdapter extends BaseAdapter {
private Activity context;
private static ImageView imageView;
private List<Drawable> plotsImages;
private static ViewHolder holder;
public GalleryImageAdapter(Activity context, List<Drawable> plotsImages) {
this.context = context;
this.plotsImages = plotsImages;
}
@Override
public int getCount() {
return plotsImages.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
imageView = new ImageView(this.context);
imageView.setPadding(3, 3, 3, 3);
convertView = imageView;
holder.imageView = imageView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageDrawable(plotsImages.get(position));
holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
holder.imageView.setLayoutParams(new Gallery.LayoutParams(150, 90));
return imageView;
}
private static class ViewHolder {
ImageView imageView;
}
}