2015-08-18 132 views
0

我想創建一個gridview加載url到它。我可以在MainActivity中使用它,但是當我將代碼粘貼到Gridview時。圖像沒有顯示。GridView圖片不顯示在片段中,只在主要活動

這裏是我的代碼片段

public static String[] eatFoodyImages = { 
     "http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg", 
     "http://i.imgur.com/C9pBVt7.jpg", 
}; 

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

    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
    // Inflate the layout for this fragment 

    GridView gridview = (GridView) rootView.findViewById(R.id.gridview); 
    gridview.setAdapter(new ImageListAdapter(getActivity(), eatFoodyImages)); 

    return inflater.inflate(R.layout.fragment_main, container, false); 
} 

activity_main.xml中

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/fragment" 
android:name="com.example.android.movie.MainFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:layout="@layout/fragment_main" /> 

Fragment_main_xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.android.movie.MainFragment"> 

<GridView 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:adjustViewBounds="true" 
    android:horizontalSpacing="0dp" 
    android:numColumns="2" 
    android:stretchMode="columnWidth" 
    android:verticalSpacing="0dp" /> 

Image_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/list_item_movie" 
android:layout_width="match_parent" 
android:layout_height="200dp"> 

ImageListAdapter:

public class ImageListAdapter extends ArrayAdapter { 
private Context context; 
private LayoutInflater inflater; 

private String[] imageUrls; 

public ImageListAdapter(Context context, String[] imageUrls) { 
    super(context, R.layout.image_view, imageUrls); 

    this.context = context; 
    this.imageUrls = imageUrls; 

    inflater = LayoutInflater.from(context); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (null == convertView) { 
     convertView = inflater.inflate(R.layout.image_view, parent, false); 
    } 

    Picasso 
      .with(context) 
      .load(imageUrls[position]) 
      .fit() // will explain later 
      .into((ImageView) convertView); 

    return convertView; 
} 
} 

回答

1

你必須在onCreateView方法的錯誤。

應該看起來像:

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

    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
    // Inflate the layout for this fragment 

    GridView gridview = (GridView) rootView.findViewById(R.id.gridview); 
    gridview.setAdapter(new ImageListAdapter(getActivity(), eatFoodyImages)); 

    return rootView; 
}