Items inside GridView getting repeated when screen scrolls

前提是你 提交于 2019-11-27 11:09:12

It's normal that you see the same items as you scroll down the GridView because in the getView method you set the drawables for the ImageView only when the convertView is null(for example for the first elements that are seen when the GridView appear on the screen). If the convertView is not null, meaning you have a recycled row view, you don't set the correct image and you remain with the image that was previously set on this recycled view. Try to modify the getView method like this:

public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(     Context.LAYOUT_INFLATER_SERVICE );
        v = inflater.inflate(R.layout.gridview_item_layout, parent, false);
    } else {
        v = (View) convertView;
    }
    TextView text = (TextView)v.findViewById(R.id.grid_item_text);
    text.setText(mTextIds[position]);
    ImageView image = (ImageView)v.findViewById(R.id.grid_item_image);
    image.setImageDrawable(mThumbIds[position]);
    return v;
}

Clicking an element shows you the correct items because you use the position parameter to retrieve the data.

These is my code for GirdView with Button + Textview

public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.grid_item, null);  
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        TextView text = (TextView)convertView.findViewById(R.id.texto_grid);
        text.setText(app_listaActiva_NOMBRES[position]);
        Button image = (Button)convertView.findViewById(R.id.miniatura_grid);
        image.setBackgroundResource(R.drawable.custom_button);
        image.setOnClickListener(new MyOnClickListener2(position,app_listaActiva_SONIDOS));
        return convertView;
    }

Change here and try it again,

   View v;
if (convertView == null) {  // if it's not recycled, initialize some attributes
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(     Context.LAYOUT_INFLATER_SERVICE );
    v = inflater.inflate(R.layout.gridview_item_layout, null);
    TextView text = (TextView)v.findViewById(R.id.grid_item_text);
    text.setText(mTextIds[position]);


} else {

    v = (View) convertView;
}
if(view!=null)
        {
               ImageView image = (ImageView)v.findViewById(R.id.grid_item_image);
               image.setImageDrawable(mThumbIds[position]);
               notifyDataSetChanged();  //Calling this helped to solve the problem. 
        }


return v;
}

Send and gridview or listview and In constructor implement this method implement onscroll listener

    this.mGridView = mGridView;
    this.mGridView.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            Log.v("onScrollStateChanged", "onScrollStateChanged");
            if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
                isScrollStop = true;

                notifyDataSetChanged();
            } else {
                isScrollStop = false;
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            Log.v("onScroll", "onScroll");
        }
    });

Using ViewHolder static class fixed repeating items for me.

Also changing layout_width and layout_height to fill_parent didn't help.

I went through this tutorial http://android-vogue.blogspot.com/2011/06/custom-gridview-in-android-with.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!