Skip to content Skip to sidebar Skip to footer

Android Gallery With First Thumbnail As Coverpage

Thanks for reading! Some background: I am building a Gallery app from the tutorial here Only change I made to this code is to replace i.setLayoutParams(new Gallery.LayoutParam

Solution 1:

You need to set the text in your textviews everytime and not only when you inflate the view.

Solution 2:

You should probably have 1 single layout file which holds the ui components in the gallery. Right now you have 2 TextView components which are independent of the Gallery. Instead, create some layout resource like this: gallery_item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><ImageViewandroid:id="@+id/imageView"android:layout_width="fill_parent"android:layout_height="fill_parent"android:adjustViewBounds="true"android:background="@android:drawable/picture_frame"android:layout_centerInParent="true" /><TextViewandroid:id="@+main/tvTitle1"android:text="Title 1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+main/tvTitle2"></TextView><TextViewandroid:id="@+main/tvTitle2"android:text="Title 2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"></TextView></RelativeLayout>

So in your getView:

Viewv= convertView;
        if(v == null) {
            LayoutInflatervi= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.gallery_item, null);

            holder = newViewHolder();
            holder.text1 = (TextView)v.findViewById(R.id.tvTitle1);
            holder.text2 = (TextView)v.findViewById(R.id.tvTitle2);
            holder.imageView = (ImageView)v.findViewById(R.id.imageView);

            v.setTag(holder);
        }
        elseholder= (ViewHolder)v.getTag();

        if(position == 0) {
          holder.text1.setVisibility(View.VISIBLE);
          holder.text2.setVisibility(View.VISIBLE);
          holder.imageView.setVisibility(View.GONE);
        }
        else {
          holder.text1.setVisibility(View.GONE);
          holder.text2.setVisibility(View.GONE);
          holder.imageView.setVisibility(View.VISIBLE);
        }

        return v;

You may find that there is some strange behavior going on when you scroll through items, so you might have to directly access each UI component rather than using the holder:

((TextView)v.findViewById(R.id.tvTitle1)).setVisibility(View.GONE);, etc

You also may be interested in setting different types of views for getView: http://developer.android.com/reference/android/widget/Adapter.html#getItemViewType(int)

Solution 3:

After trying all approaches suggested here, I wasn't able to still get a custom gallery the way I wanted it to. I kept running into ClassCastException. So, here's what worked for me till now. This is just a workaround and incase someone comes up with a better way to design a custom gallery - do post your answer here so I can accept it.

Thanks for helping out!

Post a Comment for "Android Gallery With First Thumbnail As Coverpage"