I\'m using a GridView to show a set of Categories that user can chose. Each item of the grid is consisted by an ImageView and a TextView, both retrieved from server. When an
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
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;
}
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;
}
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.