简体   繁体   English

GridView显示不正确

[英]GridView isn't being displayed correctly

I took a screenshot of it. 我截图了。 Its displaying some weird way: 它显示了一些奇怪的方式: 在此处输入图片说明

This is the code. 这是代码。

The GridAdapter: GridAdapter:

public class GridViewAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<Uri> mUrls;  
// references to our images

public GridViewAdapter(Context c, ArrayList<Uri> images) {
    mContext = c;
    this.mUrls = images;
}

public int getCount() {
    return mUrls.size();
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    ImageView inflatedImageView = (ImageView) mInflater.inflate(R.layout.imageview_amb_background, null);

    //ImageView imageView = new ImageView(mContext);

    inflatedImageView.setImageURI(mUrls.get(position));

    inflatedImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

    return inflatedImageView;
}

And the inflatedImageView is a layout inflate, like this: inflatedImageView是一个布局膨胀,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:background="@drawable/bgimages" 
    android:maxWidth="120dp"
    android:padding="5dp">

</ImageView>

On the other hand, I've a gridView in a xml file: 另一方面,我在xml文件中有一个gridView:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="250dp"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:verticalSpacing="5dp" >
</GridView>

So, I inflate this gridView, I add several URI's (3 exactly) in a loop. 因此,我将这个gridView充气起来,在一个循环中添加了几个URI(精确地是3个)。 Once added, I set the adapter to the gridview: 添加后,将适配器设置为gridview:

ArrayList<Uri> uriFotos = new ArrayList<Uri>();
HashMap<Integer, ListItem> items = xmlList.getItems();
for (int i = 0; i<items.size();i++){
     ListItem itemActual = items.get(i);
     itemActual.getLogoSrc();
     uriFotos.add(Uri.parse(Environment.getExternalStorageDirectory().toString()+rutaFinsCarpetaClient+itemActual.getLogoSrc()));
 }
 gridViewInflated.setAdapter(new GridViewAdapter(this,uriFotos));
variableContent.addView(gridViewInflated);

The images are "linked" correctly. 图像已正确“链接”。

variableContent is a LinearLayout inside a ScrollView, so the grid should be scrollable. variableContent是ScrollView内部的LinearLayout,因此网格应该是可滚动的。 But as you can see few things are happening: 但是正如您所看到的,正在发生的事情很少:

  • Height is soo big. 身高太大了。 Shouldn't it be like the inflatedImageView says? 不应该像inflatedImageView所说的那样吗?
  • Scroll isnt working. 滚动不起作用。 Well, its working but i've to move the finger around and tap several times until it works. 好吧,它的工作原理,但我必须四处移动手指并轻击几次,直到它起作用为止。 If i stop scrolling I've to repeat the same proces until it reacts again. 如果我停止滚动,则必须重复相同的过程,直到再次发生反应为止。 (SOLVED) (解决了)

Hope you guys can help me. 希望你们能帮助我。 I've changed lots of layouts, changed widths, heights, and the same thing is happening. 我改变了很多布局,改变了宽度,高度,并且发生了同样的事情。

Note that the image you see that is in the gridView, is something like 1200x800px. 请注意,您在gridView中看到的图像大约是1200x800px。

Edit The same code with smaller images: 用较小的图像编辑相同的代码:

在此处输入图片说明

I would try to set the Image view size to wrap_content : 我会尝试将图像视图的大小设置为wrap_content:

android:layout_height="wrap_content"

and if you really need the 120dp height, try to reset this size after having set the image src: 如果您确实需要120dp的高度,请在设置了图像src后尝试重置此尺寸:

 inflatedImageView.setImageURI(mUrls.get(position));

 inflatedImageView.setLayoutParams(new GridView.LayoutParams(120, 120));

also set the scaleType before adding the picture (preferably in the xml) : 在添加图片之前(最好在xml中)还要设置scaleType:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:scaleType="centerInside"
    android:background="@drawable/bgimages" 
    android:maxWidth="120dp"
    android:padding="5dp">

</ImageView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM