Arjun Anil Arjun Anil - 1 year ago 100
Android Question

GridView gets disoriented while scrolling

Items in the gridView sometimes overlap or move up or down a bit.But gets fixed when scrolling back up again.Unfortunately I can't provide an image because I am new here .I am not sure if i am doing something in code.But when use listView there is no problem.

Xml for the gridView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.gametalks.MainActivity"
android:background="@color/card_white">

<GridView
android:columnWidth="10dp"
android:numColumns="2"
android:id="@+id/listView"
android:dividerHeight="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:verticalSpacing="30dp"
></GridView>

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>

</RelativeLayout>


Here is the adapter:

[public class GameAdapter extends ArrayAdapter<GameNews> {


public GameAdapter(@NonNull Context context, List<GameNews> gameNews ){
super(context, 0, gameNews);
if(gameNews == null)
{
return;
}
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

GameNews currentNews = getItem(position);

// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.grid_item, parent, false);
final ViewHolder v = new ViewHolder();

// Find the TextView in the grid_item.xml layout with the ID version_name

v.Heading = (TextView) listItemView.findViewById(R.id.title);
v.description = (TextView) listItemView.findViewById(R.id.description);
v.newsImage = (ImageView) listItemView.findViewById(R.id.image) ;
v.source = (TextView) listItemView.findViewById(R.id.source);
v.time = (TextView) listItemView.findViewById(R.id.time);
v.menuButton = (Button) listItemView.findViewById(R.id.popup);
listItemView.setTag(v);


v.menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

PopupMenu popup = new PopupMenu(getContext(),view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();

}
});
}

ViewHolder viewHolder = (ViewHolder) listItemView.getTag();

//Setting title to current news title
viewHolder.Heading.setText(currentNews.getTitle());
viewHolder.description.setText(currentNews.getDescription());
viewHolder.newsImage.setTag(currentNews.getphotoUrl());
viewHolder.source.setText(currentNews.getSource());
viewHolder.time.setText(currentNews.getTime());
// Picasso.with(getContext()).load(currentNews.getphotoUrl()).into(viewHolder.newsImage);


if (URLUtil.isValidUrl(currentNews.getphotoUrl())) {
Picasso.with(getContext())
.load(currentNews.getphotoUrl())
.resize(350,300)
.tag(tag)
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(viewHolder.newsImage);

} else {
Picasso.with(getContext())
.load(R.drawable.right_line)
.resize(350,300)
.tag(tag)
.noPlaceholder()
.into(viewHolder.newsImage);
}

return listItemView;
}




public static class ViewHolder {
TextView Heading;
TextView description;
ImageView newsImage;
TextView source;
TextView time;
Button menuButton;


}
}][1]


If you need any other details please ask.

Answer Source

This happened due to the reason that i set the size of gridView items to wrap content and hence different items had different sizes.This caused unstability in the gridView.I fixed it by giving the items of the gridView a fixed dp size.And now it has no problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download