Android - How to fix Fisrt Item always align to Left and Last Item always align to Right Gallery

Viewed 640

The Gallery by default align the first element and the last element to center.

I have tried android gallery image position problem. For the fisrt time, first item align to left. It's OK. But when i swipe gallery, first item still has spacing to left and last item still has spacing to right.

image_gallery_items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="1sp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="130dp"
    android:layout_height="110dp"
    android:layout_gravity="center" >
</ImageView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TextView>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >

<Gallery
    android:id="@+id/galleryid"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" />

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {

private Activity activity;
private static LayoutInflater inflater = null;

public ImageAdapter(Activity a) {
    activity = a;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder {
    public TextView text;
    public ImageView image;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    ViewHolder holder;
    if (convertView == null) {
        vi = inflater.inflate(R.layout.image_gallery_items, null);
        holder = new ViewHolder();
        holder.text = (TextView) vi.findViewById(R.id.textView1);
        holder.image = (ImageView) vi.findViewById(R.id.image);
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();
    holder.text.setText(name[position]);
    final int stub_id = data[position];
    holder.image.setImageResource(stub_id);

    return vi;
}

private int[] data = { R.drawable.category, R.drawable.category_002,
        R.drawable.category_003, R.drawable.category_004,
        R.drawable.category_005, R.drawable.category_006,
        R.drawable.category_007, R.drawable.category_008,
        R.drawable.category_009, R.drawable.category_010 };

private String[] name = { "Image1", "Image2", "Image3", "Image4", "Image5",
        "Image6", "Image7", "Image8", "Image9", "Image10" };

}

MainActivity.java

public class MainActivity extends Activity {

private Gallery galleryView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    galleryView = (Gallery) findViewById(R.id.galleryid);
    galleryView.setAdapter(new ImageAdapter(this));
    galleryView.setSelection(1);

    galleryView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {

            Toast.makeText(MainActivity.this, "" + position,
                    Toast.LENGTH_SHORT).show();

        }
    });

}

}

My Question is : How to fixed First Item of Gallery always to left and Last Item to right without spacing.

enter image description here

0 Answers
Related