GridView android layout changes automatically
I created a GridView
to show all images with some information. The grid shows the Images and the details. But when I scroll up and down, arrangement of images changes automatically. Why is this happening?
Images.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Grid1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="3dp"
android:background="#eee"
android:columnWidth="140dp"
android:gravity="center"
android:horizontalSpacing="3dp"
android:numColumns="auto_fit"
android:scrollingCache="false"
android:verticalSpacing="10dp" />
grid_single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp">
<ImageView
android:id="@+id/grid_image"
android:layout_width="50dp"
android:layout_height="50dp"></ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="file"></TextView>
<TextView
android:id="@+id/path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="path"
android:textSize="9sp" />
</LinearLayout>
</LinearLayout>
Images.java
public class Images extends Activity {
GridView grid;
String[] web = {
"Google",
"Github",
"Instagram",
"Facebook",
"Flickr",
"Pinterest",
"Quora",
"Twitter",
"Vimeo",
"WordPress",
"Youtube",
"Stumbleupon",
"SoundCloud",
"Reddit",
"Blogger"
};
String[] imageId = {
"/mnt/sdcard/DCIM/Camera/IMG_20140729_224838.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg",
"/mnt/sdcard/DCIM/final_bstSnapshot_79847.jpg"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.images);
CustomGrid adapter = new CustomGrid(Images.this, web, imageId);
grid = (GridView) findViewById(R.id.Grid1);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Images.this, "You Clicked at " + web[+position], Toast.LENGTH_SHORT).show();
}
});
}
}
CustomGrid.java
public class CustomGrid extends BaseAdapter {
private final String[] web;
private final String[] Imageid;
private Context mContext;
public CustomGrid(Context c, String[] web, String[] Imageid) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
//////
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(Imageid[position], options);
`
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 49, 49);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(Imageid[position], options);
//////
imageView.setImageBitmap(bm);//.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
}
链接地址: http://www.djcxy.com/p/93384.html