在GridView的片段不显示 [英] Gridview not displaying in fragment
问题描述
我试着去遵循这个的的GridView带自动调整图片例子,试图在一个片段。
这里是code:
FragmentOne
公共类FragmentOne扩展片段{GridView控件GridView控件;@覆盖
公共查看onCreateView(LayoutInflater吹气,@Nullable的ViewGroup容器,@Nullable捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.fragment_one,集装箱,FALSE); GridView控件=(GridView控件)view.findViewById(R.id.gvList);
gridView.setAdapter(新Top_MyAdapter(getActivity()getApplicationContext())。);
返回视图。
}
私有类Top_MyAdapter延伸BaseAdapter {
私人列表<项目>项目=新的ArrayList<项目>();
私人LayoutInflater吹气;
公共Top_MyAdapter(上下文C){
吹气= LayoutInflater.from(C); items.add(新项目(图片1,R.drawable.image_placeone));
items.add(新项目(图像2,R.drawable.image_placetwo));
items.add(新项目(图像3,R.drawable.image_placethree));
items.add(新项目(图像4,R.drawable.image_placefour));
items.add(新项目(图片5,R.drawable.image_placefive));
} @覆盖
公众诠释的getCount(){
返回items.size();
} @覆盖
公共对象的getItem(INT位置){
返回items.get(位置);
} @覆盖
众长getItemId(INT位置){
返回items.get(位置).drawableId;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
视图V = convertView;
ImageView的图片;
TextView的名称; 如果(V == NULL){
V = inflater.inflate(R.layout.gridviewitem,父母,假);
v.setTag(R.id.gvItemImage,v.findViewById(R.id.gvItemImage));
v.setTag(R.id.gvItemText,v.findViewById(R.id.gvItemText));
} 照片=(ImageView的)v.getTag(R.id.gvItemImage);
名称=(TextView中)v.getTag(R.id.gvItemText); 项目项目=(项目)的getItem(位置); picture.setImageResource(item.drawableId);
name.setText(item.name); 返回伏; }
私有类项目
{
最终字符串名称;
最终诠释drawableId; 项目(字符串名称,诠释drawableId)
{
this.name =名称;
this.drawableId = drawableId;
}
}}
fragmentone
<的FrameLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
>
< GridView控件
机器人:ID =@ + ID / gvList
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:verticalSpacing =0dp
机器人:horizontalSpacing =0dp
机器人:stretchMode =spacingWidth
机器人:为numColumns =2/>
< /&的FrameLayout GT;
gridviewitem
<的FrameLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_height =match_parent
机器人:layout_width =match_parent>
< com.Sample.SquareImageView
机器人:ID =@ + ID / gvItemImage
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:scaleType =centerCrop
/>
<的TextView
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / gvItemText
机器人:paddingLeft =10dp
机器人:paddingRight =10dp
机器人:paddingTop =15dp
机器人:paddingBottom会=15dp
机器人:layout_gravity =底
机器人:文字颜色=@机器人:彩色/白
机器人:背景=#5500/>
< /&的FrameLayout GT;
SquareImageView
公共类SquareImageView扩展的ImageView
{
公共SquareImageView(上下文的背景下)
{
超级(上下文);
}公共SquareImageView(上下文的背景下,ATTRS的AttributeSet)
{
超(背景下,ATTRS);
}公共SquareImageView(上下文的背景下,ATTRS的AttributeSet,INT defStyle)
{
超(背景下,ATTRS,defStyle);
}@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
setMeasuredDimension(getMeasuredWidth(),getMeasuredWidth()); //捕捉到宽
}
}
这里的问题是,在我的片段无法显示。没有错误,它只是不显示了,突然有上rightside滚动条。
有可能在getView方法问题。它看起来像一个虚假的实施意见持有者格局。
我们可以尝试无纹。
您可以尝试一下本作是一定要找准问题。
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
convertView = inflater.inflate(R.layout.gridviewitem,父母,假); ImageView的图像=(ImageView的)convertView.findViewById(R.id.gvItemImage);
TextView的文本=(TextView的)convertView.findViewById(R.id.gvItemText); image.setImageResource(的getItem(位置).drawableId);
text.setText(的getItem(位置)。名称); 返回convertView;
}
静态类项目
{
最终字符串名称;
最终诠释drawableId; 项目(字符串名称,诠释drawableId)
{
this.name =名称;
this.drawableId = drawableId;
}
}
块引用>Im trying to follow this Gridview with Auto resize image example and tried it in a Fragment. Here is the code:
FragmentOne
public class FragmentOne extends Fragment{ GridView gridView; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_one, container, false); gridView = (GridView) view.findViewById(R.id.gvList); gridView.setAdapter(new Top_MyAdapter(getActivity().getApplicationContext())); return view; } private class Top_MyAdapter extends BaseAdapter { private List<Item> items = new ArrayList<Item>(); private LayoutInflater inflater; public Top_MyAdapter(Context c) { inflater = LayoutInflater.from(c); items.add(new Item("Image 1", R.drawable.image_placeone)); items.add(new Item("Image 2", R.drawable.image_placetwo)); items.add(new Item("Image 3", R.drawable.image_placethree)); items.add(new Item("Image 4", R.drawable.image_placefour)); items.add(new Item("Image 5", R.drawable.image_placefive)); } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return items.get(position).drawableId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ImageView picture; TextView name; if(v == null){ v = inflater.inflate(R.layout.gridviewitem, parent, false); v.setTag(R.id.gvItemImage, v.findViewById(R.id.gvItemImage)); v.setTag(R.id.gvItemText, v.findViewById(R.id.gvItemText)); } picture = (ImageView) v.getTag(R.id.gvItemImage); name = (TextView) v.getTag(R.id.gvItemText); Item item = (Item) getItem(position); picture.setImageResource(item.drawableId); name.setText(item.name); return v; } private class Item { final String name; final int drawableId; Item(String name, int drawableId) { this.name = name; this.drawableId = drawableId; } } }
fragmentone
<FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id = "@+id/gvList" android:layout_width="match_parent" android:layout_height="match_parent" android:verticalSpacing="0dp" android:horizontalSpacing="0dp" android:stretchMode="spacingWidth" android:numColumns="2"/> </FrameLayout>
gridviewitem
<FrameLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <com.Sample.SquareImageView android:id="@+id/gvItemImage" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id = "@+id/gvItemText" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="15dp" android:paddingBottom="15dp" android:layout_gravity="bottom" android:textColor="@android:color/white" android:background="#55000000"/> </FrameLayout>
SquareImageView
public class SquareImageView extends ImageView { public SquareImageView(Context context) { super(context); } public SquareImageView(Context context, AttributeSet attrs) { super(context, attrs); } public SquareImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width } }
The problem here is that is is not displaying in my Fragment. There's no error it's just not displaying, and suddenly there is a scrollbar on the rightside ..
解决方案There may be problem in getView method. It looks like a false implementation of view holder pattern.
We can try without pattern.
Can you try this for being sure to identify problem.
@Override public View getView(int position, View convertView, ViewGroup parent) { convertView = inflater.inflate(R.layout.gridviewitem, parent, false); ImageView image = (ImageView) convertView.findViewById(R.id.gvItemImage); TextView text = (TextView) convertView.findViewById(R.id.gvItemText); image.setImageResource(getItem(position).drawableId); text.setText(getItem(position).name); return convertView; } static class Item { final String name; final int drawableId; Item(String name, int drawableId) { this.name = name; this.drawableId = drawableId; } }
这篇关于在GridView的片段不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!