滚动屏幕时,GridView元素会动态更改其位置

DEV*_*RMA 24 android

我有一个gridview布局,所有项目插入非常好,

现在,如果我检查大屏幕,那么所有工作都很好,因为不需要滚动

但如果我检查小屏幕,那么项目会动态改变位置,

bereif的例子如下: -

就像我有28个项目并排列在网格视图7*4中,现在如果在第一个屏幕中显示最多20个项目,现在剩下的8显示在我向下滚动屏幕时,但现在第一行或第二行的一些元素也被放入最后一排.

代码在这里

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;
        }
Run Code Online (Sandbox Code Playgroud)

Hud*_*awk 55

你应该getView像这样改变你的方法.

    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;
    }
Run Code Online (Sandbox Code Playgroud)

您的问题是,当您使用convertView它时,将旧数据存储在第一个记录中.转换视图用于避免资源的布局膨胀,这会花费您的时间和内存.您应该使用旧的膨胀视图,但设置新数据.

  • 它的工作对我来说,但滚动它不能顺利工作.. ?? (2认同)