毕加索将图像放在彼此之上

时间:2018-06-08 23:22:50

标签: android xml fragment picasso

我刚开始在Android上使用Picasso。所以我完全不了解它。我有一个图像片段工作正常,我可以看到我的文字和图像(大多数)。没有错误。我的问题是我有一系列想要输出的图像。但问题是它将一个图像放在位置 x,然后将下一个图像放在同一个地方。它没有将它们放在Gridview格式中。有人知道为什么好吗?

我的片段XML

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myPhotos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="00dp"
android:numColumns="auto_fit"
android:verticalSpacing="20dp"
android:horizontalSpacing="30dp"
android:stretchMode="columnWidth"
android:paddingTop="25px"
android:gravity="center"
/>

MyFragment代码

public class ImagesFragment extends Fragment {
private View view;
private ImageAdapter imageAdapter;
private  GridView gridView;
public ImagesFragment() {

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.images_fragment,container,false);
    GridView gridView = view.findViewById(R.id.myPhotos);
    imageAdapter = new ImageAdapter(getActivity(),3);
    gridView.setAdapter(imageAdapter);
    return view;
}
}

ImageAdapter

public class ImageAdapter extends BaseAdapter {
//The content of our screen
private Context context;
private ImageView imageView;
private int start;
private Integer[]imagesArray;
//ImageInfo images;
public ImageAdapter(Context context, int start)
{
    this.context = context;
    this.start = start;
    imagesArray = new Integer[4];
    imagesArray[0] =  0x55555;
    imagesArray[1] =  0x545555;
    imagesArray[2] =  0x454454;
    imagesArray[2] =  0x121210;
}
public int getCount()
{
    return imagesArray.length;
}

public Integer getItem(int position)
{
    return imagesArray[0];
}

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

public View getView(int position, View convertView, ViewGroup parent)
{
    if (convertView == null)
        //Don't put in the constructor
        imageView = new ImageView(context);
    else
        imageView = (ImageView) convertView;
        if(start ==0)
            //Works fine loads all the images at array index 0
            Picasso.get().load(imagesArray[0]).resize(400,600).into(imageView);
        else
            //WHERE THE PROBLEM IS.. I think. 
            for(int i=1;i<=start; i++)
                Picasso.get().load(imagesArray[i]).resize(400,600).into(imageView);



    return imageView;
}
}

1 个答案:

答案 0 :(得分:0)

问题在于,您使用convertview作为GridView.Convert视图实际上是FrameLayout。 将GridView转换为帧布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <GridView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/my_images"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:numColumns="2">
    </GridView>

</FrameLayout>

然后在picasso中使用convertview

Picasso
            .with(context)
            .load(imageUrls[position])
            .fit() // will explain later
            .into((ImageView) convertView);