我想在android中使用gridview显示不同的图像? Xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
Android [Java代码]:
public class Backgroundimage extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter
{
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public int getCount()
{
return mThumbIds.length;
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(6, 6, 6, 6);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(R.drawable.image1);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4,
};
}
}
但是image1出现在所有位置,就像这个图像出现在行和列的网格视图中一样。但是没有显示其他图像。如何解决这个问题?
答案 0 :(得分:1)
尝试更改您的代码,如下所示: - (标有/* */
的更改)
public class Backgroundimage extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c){
mContext = c;
}
public int getCount(){
return mThumbIds.length;
}
public Object getItem(int position){
return null;
}
public long getItemId(int position){
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView imageView;
if (convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(6, 6, 6, 6);
}else{
imageView = (ImageView) convertView;
}
/* Change Here in your code */
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.image1, R.drawable.image2,
R.drawable.image3, R.drawable.image4};
}
}
答案 1 :(得分:0)
更改此行
imageView.setImageResource(R.drawable.image1);
添加此项而非
imageView.setImageResource(mThumbIds[position]);
答案 2 :(得分:0)
它简单的兄弟......
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(6, 6, 6, 6);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(R.drawable.image1);
return imageView;
}
在上面的代码更改
imageView.setImageResource(R.drawable.image1);
到
imageView.setImageResource(mThumbIds[position]);
并确保在mThumbs数组中添加您想要Gridview的子元素
的元素数量第二个疑问 在这一行之后 GridView gridview =(GridView)findViewById(R.id.gridview); 使用这个
gritview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View imageview, int pos,
long arg3) {
// TODO Auto-generated method stub
}
});