0

I need your help in getting my GridView displayed inside a Horizontal Scroll Bar. While scrolling I want to display one GridView at a time. Each cell in GridView displays an image. I put break points at some relevant method in my Adapter class like getCount() and getView(int position, View convertView, ViewGroup parent) and I see they are getting called. However, getItem(int position) is not getting called - I am not sure whether it needs to be called or not. Here is my code:

GalleryAdapter.java(Adapter for GridView)

public class GalleryAdapter extends BaseAdapter {

private MPSample mpSample;
private File[] listOfFiles;

public GalleryAdapter(MPSample mpSample) {
    this.mpSample = mpSample;
    File galleryDir = new File(mpSample.mPath);
    if (galleryDir != null && galleryDir.exists()) {
        listOfFiles = galleryDir.listFiles();
    }
}

@Override
public int getCount() {
    int count = 0;
    if (listOfFiles != null) {
        count = listOfFiles.length;
    }
    return count;
}

@Override
public Object getItem(int position) {
   return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView = new ImageView(mpSample);

    if (convertView == null) {
        File defaultPicture = Utils.getDefaultPictureFromCurrentDir(listOfFiles[position]);
        Drawable drawable = Drawable.createFromPath(defaultPicture.getPath());
        imageView.setImageDrawable(drawable);
        imageView.setAdjustViewBounds(true);
        imageView.setScaleType(ScaleType.FIT_XY);

    } else {
        imageView = (ImageView) convertView;
    }
    return imageView;
}

}

GalleryFragment.java (Class that creates the GridView and assigns adapter to it. Also it puts the GridView inside the Horizontal Scroll View):

public class GalleryFragment extends Fragment {

private MPSample mpSample;

public GalleryFragment(MPSample mpSample) {
    this.mpSample = mpSample;   
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.gallery, container, false);   

    HorizontalScrollView horizontalScrollView = (HorizontalScrollView)view.findViewById(R.id.h_scroll_view_gallery);
    GridView gridView = new GridView(mpSample);
    gridView.setNumColumns(3);
    gridView.setAdapter(new GalleryAdapter(mpSample));
    gridView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.0f));
    gridView.setPadding(150, 50, 50, 250);
    gridView.setVisibility(View.VISIBLE);
    horizontalScrollView.addView(gridView);     

    return view;

}
}

Note: MPSample extends FragmentActivity and is passed as a context

My XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent_color"
>

<HorizontalScrollView 
        android:id="@+id/h_scroll_view_gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">           
 </HorizontalScrollView>

</RelativeLayout>

Can someone please help me to understand why my GridView is not getting displayed? Appreciate any help from you.

Raj
  • 1,119
  • 4
  • 21
  • 41
  • gridview scrolls by itself so why do you need a gridview inside a scrollview? – Raghunandan Jul 10 '13 at 16:01
  • The view has 3 by 3 images (9) with each row and each column having 3 images. There is some good padding on the right and left side of the view. The look and feel should be that the next view should be completely hidden. So I thought going with grid view would be a good idea. – Raj Jul 10 '13 at 16:21

0 Answers0