GridView没有显示所有数据

时间:2014-07-06 10:41:06

标签: android android-layout gridview

Result.java:

Fragment f = new GridViewFragement();
FragmentManager fm = getFragmentManager();

fm.beginTransaction().replace(R.id.grid, f).commit();

Activity_result.xml:

   <LinearLayout
       android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:id="@+id/grid"
       android:orientation="vertical"

    >

</LinearLayout>  

Row_grid.xml:定义网格项

 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
  android:orientation="vertical"
   android:padding="5dp" >

  <ImageView

    android:id="@+id/item_image"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/home" >
 </ImageView>

 <TextView
    android:id="@+id/item_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5dp"
    android:textSize="15sp" >
 </TextView>

</LinearLayout>

grid.xml:

         <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="4dp"
        android:columnWidth="80dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:fadeScrollbars="false"
       />

在网格视图片段中创建:

       public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // on create for activity


        View view = inflater.inflate(R.layout.grid, container, false);
        Bitmap homeIcon = BitmapFactory.decodeResource
        (this.getResources(), R.drawable.home);
        Bitmap userIcon = BitmapFactory.decodeResource
        (this.getResources(),   R.drawable.personal);
        //
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"Personal"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Building"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"xyz"));
        gridArray.add(new Item(homeIcon,"Home"));
        gridArray.add(new Item(userIcon,"User"));
        gridArray.add(new Item(homeIcon,"House"));
        gridArray.add(new Item(userIcon,"Friend"));



        gridView = (GridView) view.findViewById(R.id.gridView1);
        customGridAdapter =newCustomGridViewAdapter
        (view.getContext(),R.layout.row_grid,gridArray);

        gridView.setAdapter(customGridAdapter);

        return view;
     }

尝试显示gridview的所有数据时遇到问题。只有前三个项目连续显示才会中途切断。对不起,我无法发布图片,因为我只是代表1。 我尝试过滚动视图并使滚动条出现,但它不会滚动或显示所有数据

2 个答案:

答案 0 :(得分:1)

如果要查看设置layout_height = fill_parent所需的所有行 不是layout_width = fill_parent和layout_height = wrap_content。它不会 能够显示所有行,除非你让它们都很小 无论如何,取决于您拥有的细胞数量。如果你有很多细胞 那么即使试图让它们出现在一个屏幕上也毫无意义。

我的建议是去列表视图。

答案 1 :(得分:0)

我认为您担心显示的行数和列数。如果是,则可以使用numColumns属性设置列数,而行数取决于数据量和列数。 see this for a better insight