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.