Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

android - GridView elements changes their place dynamically when scroll screen

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;
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...