GridView图片不在片段中显示,仅在主要活动中显示 [英] GridView picture not showing up in fragment, only on Main Activity
本文介绍了GridView图片不在片段中显示,仅在主要活动中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个将url加载到其中的gridview。我可以在MainActivity中使用它,但是当我将代码粘贴到Gridview时。图像没有显示。
这是我在片段上的代码
public static String [] eatFoodyImages = {
http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg,
http://i.imgur.com/C9pBVt7.jpg,
};
@Override
public查看onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundled savedInstanceState){
查看rootView = inflater.inflate(R.layout .fragment_main,container,false);
//膨胀这个片段的布局
GridView gridview =(GridView)rootView.findViewById(R.id.gridview);
gridview.setAdapter(new ImageListAdapter(getActivity(),eatFoodyImages));
返回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
工具: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 0dp/>
Image_view.xml
<?xml version =1.0encoding =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;
私人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);
$ b @Override
public View getView(int position,View convertView,ViewGroup parent){
if(null == convertView){
convertView = inflater.inflate(R.layout.image_view,parent,false);
$ b $毕加索
.with(上下文)
.load(imageUrls [position])
.fit()//稍后会解释
.into((ImageView)convertView);
返回convertView;
$ div $解析方案
你在 onCreateView
方法中有一个错误。
它应该看起来像这样:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main,container,false);
//膨胀这个片段的布局
GridView gridview =(GridView)rootView.findViewById(R.id.gridview);
gridview.setAdapter(new ImageListAdapter(getActivity(),eatFoodyImages));
返回rootView;
}
I am trying to create an gridview that load url into it. I can get it working in MainActivity but when I paste my code to Gridview. The image isnt showing.
Here is my code on Fragment
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;
}
}
解决方案
You have a bug in the onCreateView
method.
It should look like that:
@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;
}
这篇关于GridView图片不在片段中显示,仅在主要活动中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文