GridView未显示在水平滚动条内

时间:2013-07-10 15:35:56

标签: android android-gridview horizontalscrollview

我需要你帮助我将GridView显示在水平滚动条中。滚动时我想一次显示一个GridView。 GridView中的每个单元格都显示一个图像。我在我的Adapter类中的一些相关方法中加入了断点,如getCount()和getView(int position,View convertView,ViewGroup parent),我看到它们被调用了。但是,getItem(int position)没有被调用 - 我不确定是否需要调用它。这是我的代码:

GalleryAdapter.java(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(创建GridView并为其分配适配器的类。它还将GridView放在水平滚动视图中):

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;

}
}

注意:MPSample扩展了FragmentActivity并作为上下文传递

我的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>

有人可以帮我理解为什么我的GridView没有显示出来吗?感谢您的任何帮助。

0 个答案:

没有答案