измените свой класс, как показано ниже, и создайте один пользовательский класс, который расширяет галерею, добавляет метод fling и изменяет xml-файл на xml ниже.
com.gallery1
public class Gallerynew extends Activity implements OnItemSelectedListener{
public boolean Visibility=true;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setSpacing(10);
g.setOnItemSelectedListener(this);
}
public class ImageAdapter extends BaseAdapter {
private Context myContext;
private int[] myImageIds = {
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.bokstavslottet04,
R.drawable.bokstavslottet05,
R.drawable.bokstavslottet06,
R.drawable.bokstavslottet07,
R.drawable.bokstavslottet08,
R.drawable.bokstavslottet09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.bokstavslottet12,
R.drawable.bokstavslottet13
};
public ImageAdapter(Context c) { this.myContext = c; }
public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
/* Image should be scaled as width/height are set. */
i.setScaleType(ImageView.ScaleType.FIT_XY);
/* Set the Width/Height of the ImageView. */
i.setLayoutParams(new Gallery.LayoutParams(700, 400));
return i;
}
/** Returns the size (0.0f to 1.0f) of the views
* depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
/* Formula: 1 / (2 ^ offset) */
return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
}
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + " of Antartica", Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
customclass: Gallery1.java
public class Gallery1 extends Gallery {
public Gallery1(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public Gallery1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Gallery1(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
return false;
}
}
XML-файл:
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
/>