GridView控件元素动态改变自己的位置,当滚动画面 [英] GridView elements changes their place dynamically when scroll screen
问题描述
我有一个gridview布局,所有的项目都插入很细,
现在,如果我检查大屏幕上,于是做了所有的工作是好的,因为没有滚动需要
但如果我检查小屏幕则项目动态地改变自己的立场,
bereif例子如下: -
像我有28个项目,并安排在一个网格视图7 * 4,现在如果高达20个项目显示在一个屏幕上,现在剩下8所示,而我滚动屏幕朝下,但首先目前的一些元素或第二排aslo放在最后一行。
code是这里
公共类ImageAdapter扩展了BaseAdapter
{
语境mContext;
//公共静态最终诠释ACTIVITY_CREATE = 10;
公共ImageAdapter(上下文C)
{
mContext = C;
}
@覆盖
公众诠释getCount将()
{
// TODO自动生成方法存根
返回providers.length;
}
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
// TODO自动生成方法存根
视图V;
如果(convertView == NULL)
{
LayoutInflater李= getLayoutInflater();
V = li.inflate(R.layout.icontext,NULL);
TextView的电视=(TextView中)v.findViewById(R.id.icon_text);
tv.setText(供应商[位置]);
ImageView的IV =(ImageView的)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
}
其他
{
V = convertView;
}
返回伏;
}
您应该改变这样你的 getView
方法。
公开查看getView(INT位置,查看convertView,ViewGroup中父){
// TODO自动生成方法存根
视图V;
如果(convertView == NULL)
{
LayoutInflater李= getLayoutInflater();
V = li.inflate(R.layout.icontext,NULL);
}其他{
V = convertView;
}
TextView的电视=(TextView中)v.findViewById(R.id.icon_text);
tv.setText(供应商[位置]);
ImageView的IV =(ImageView的)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
返回伏;
}
您的问题是,当你使用 convertView
它存储旧数据形成的第一个记录。转换视图来避免资源布局通胀花费你的时间和内存。您应该使用旧的放大视图,但设置新的数据。
i have a gridview layout, and all items are insert very fine,
now if I check on big screen then all work done is fine, because no scrolling is required
but if I check on small screen then items are changed their position dynamically,
bereif example is given below:-
like I have 28 items and arranged in a grid view 7*4, now if upto 20 items are shown in first screen, now remaining 8 is shown while i scroll screen down, but now some elements of first or second row is aslo put in last row.
code is here
public class ImageAdapter extends BaseAdapter
{
Context mContext;
//public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c)
{
mContext = c;
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return providers.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
View v;
if(convertView==null)
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icontext, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText(providers[position]);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
}
else
{
v = convertView;
}
return v;
}
You should change your getView
method like this.
public View getView(int position, View convertView, ViewGroup parent){
// TODO Auto-generated method stub
View v;
if(convertView==null)
{
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icontext, null);
}else{
v = convertView;
}
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText(providers[position]);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
return v;
}
Your problem is that when you use convertView
it stores old data form the first records. Convert view is used to avoid layout inflation from resource which costs you time and memory. You should use the old inflated view, but set new data.
这篇关于GridView控件元素动态改变自己的位置,当滚动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!