Gallery layout issues

I am trying to create a gallery in my Android project, but cannot make it work as it should.

Here is what I am trying to do:

Basically, I need a gallery that displays only one image, but still has the same controls (sliding left and right). I get images asynchronously from the Internet.

Where I am?

Well, while I'm just trying to display the gallery ... I will see the β€œone shot” step after.

What is my problem?

Well ... My gallery is not displayed on my layout. For everyone.

What is the problem?

The log data gives me that the height and width of my gallery object ... 0.

Speaking of code ...

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:background="@drawable/fond">

  <ImageView android:background="@drawable/banniere" android:layout_height="wrap_content" android:layout_width="fill_parent"/>

  <Gallery
    android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>



</LinearLayout>

Activities

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.galleryfullscreen);

    Gallery gal = (Gallery) findViewById(R.id.gallery);

    ArrayList<Photo> photos = (ArrayList<Photo>) this.getIntent().getExtras().getSerializable("Photos");
    int pos = this.getIntent().getExtras().getInt("Index");

    Log.i("Season",photos.toString());

    gal.setAdapter(new PhotoAdapter(this, photos, false));
    gal.setSelection(pos);

    Log.d("Bottom", String.valueOf(gal.getBottom()));
    Log.d("Right", String.valueOf(gal.getRight()));
    Log.d("Width", String.valueOf(gal.getWidth()));
    Log.d("Height", String.valueOf(gal.getHeight()));       
}

Add-ons are installed correctly.

Adapter

The public PhotoAdapter class extends the BaseAdapter {

private ArrayList<PhotoView> views;
private ArrayList<Photo> season;

public PhotoAdapter(Context c, ArrayList<Photo> images, boolean mini) {

    season = images;
    views = new ArrayList<PhotoView>();
    for (int i = 0; i < images.size() ; i++)
        if (mini)
        views.add(new PhotoView(c,images.get(i).getUrlMini(),false));
        else{

            views.add(new PhotoView(c,images.get(i).getUrlBig(),true));
        }

}

@Override
public int getCount() {
    return views.size();
}

@Override
public Object getItem(int position) {
    return views.get(position);
}   

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return views.get(position);
}

public ArrayList<Photo> getSeason() {
    return season;
}

}

( gridview)

PhotoView...

PhotoView ImageView {

Bitmap image;

public PhotoView(Context context, String url, boolean Gallery) {

    super(context);
    if (!Gallery){
    this.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT,GridView.LayoutParams.WRAP_CONTENT));
    this.setScaleType(ImageView.ScaleType.FIT_CENTER);
    this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
    new DownloadImageTask().execute(url);
    }
    else{
        this.setLayoutParams(new Gallery.LayoutParams(100,100));
        this.setScaleType(ImageView.ScaleType.FIT_CENTER);
        this.setBackgroundDrawable(((getResources().getDrawable(R.drawable.blk))));
        this.setImageDrawable((getResources().getDrawable(R.drawable.blk)));
    }

}

public Bitmap getImage(){
    return image;
}

public void setImage(Bitmap img){
    image = img;
    this.setImageBitmap(image);
}

, . , . , , ressource, , , .

Sooo... "0" ? LayoutParams ( XML, ), . getView. LayoutParams , .

xmlns: android = "http://schemas.android.com/apk/res/android" , .

, . , ... ! , /, ...

!

+3
1

XML , , . LinearLayout ( ), layout_width, fill_parent.

LinearLayout.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
  android:orientation="vertical"
  android:background="@drawable/fond">
+2

Source: https://habr.com/ru/post/1770764/


All Articles