片段中的自定义Gridview未填充

时间:2014-11-27 21:37:50

标签: android gridview android-fragments android-adapter

我试图在片段中创建一个自定义gridview,看起来像这样:

http://imgur.com/6fEFYTN

所以我创建了一个包含图像和标签的FragmentLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

    <ImageView
    android:id="@+id/grid_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
     />

    <TextView
        android:id="@+id/grid_label"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_marginTop="175dp"    
        android:background="@drawable/label"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="12sp"
        android:textStyle="bold" />

管理FragmentLayout的适配器如下所示:

public class CustomGridAdapter extends BaseAdapter{

private Context mContext;
private final String[] label;
private final int[] imgId;
  public CustomGridAdapter(Context c,String[] label,int[] imgId ) {
      mContext = c;
      this.imgId = imgId;
      this.label = label;
  }

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

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View grid;
      LayoutInflater inflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          if (convertView == null) {
            grid = new View(mContext);
        grid = inflater.inflate(R.layout.image_button_layout, null);
            TextView textView = (TextView) grid.findViewById(R.id.grid_label);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            textView.setText(label[position]);
            imageView.setImageResource(imgId[position]);
          } else {
            grid = (View) convertView;
          }
      return grid;
}   

}

片段网格如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<GridView
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/form_grid"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
     />

</LinearLayout>

我是通过onCreateView方法创建的,我使用的是onclickListener,因此图像可以作为按钮使用,并在按下时显示吐司:

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

    View rootView = inflater.inflate(R.layout.fragment_layout_softdrinks, container, false);

    GridView grid = (GridView) rootView.findViewById(R.id.form_grid);

    CustomGridAdapter adapter = new CustomGridAdapter(rootView.getContext(), label, imgId); //label and imgId are arrays containing strings and int respectively

    grid.setAdapter(adapter);

    grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(inflater.getContext(), "You Clicked at " +label[+ position], Toast.LENGTH_SHORT).show();
        }
    });

    return rootView;        
}

问题是当我运行代码时,屏幕是空白的,因此没有填充gridview。 对此有何建议?

1 个答案:

答案 0 :(得分:1)

首先,您需要在imgId数组和标签数组中提供有效负载。

然后您的网格视图也不会膨胀,因为您在getCount方法中返回0!

在getCount方法

中扩充布局替换
Return 0;

使用

Return imgId.length;

getCount方法告诉基本适配器您希望在网格视图中膨胀的数组中有多少图像或字符串(..数据)。如果返回0,则基本适配器认为有0个数据需要膨胀。