0

I am working on an application where i have a gridview containing all my images from the gallery of my phone and when i click on an image it opens in another activvity (i already did this part). I need to make the new activity swipe so i can see all the images one after the other by swiping on the screen. When i swipe, the image changes so i see the next one etc.. I need to do this using a ViewPager . Can anyone help me do it ?

This is my main activity code:

package com.example.gridviewgallery;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class MainActivityGridView extends ActionBarActivity {

    int columnIndex;
    Cursor cursor = null;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity_grid_view);

        String[] projection = {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.DATA};
        cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, 
                 MediaStore.Images.Thumbnails.IMAGE_ID);
        columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

        GridView gridview = (GridView) findViewById(R.id.gridView1);
        gridview.setAdapter(new GridAdapter(getApplicationContext(), cursor, columnIndex));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id)
            {
                cursor.moveToPosition(position);
                columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
                int i = cursor.getInt(columnIndex);
                Intent intent = new Intent(view.getContext(), ImageActivity.class);
                intent.putExtra("id", i);
                intent.putExtra("pos", position);
                startActivity(intent);
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

This is my second activity code :

package com.example.gridviewgallery;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.widget.ImageView;

public class ImageActivity extends ActionBarActivity
{
    MainActivityGridView magv;
    int pos,id,columnIndex;
    ImageView imageView;

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

        Intent intent = getIntent();
        id = intent.getExtras().getInt("id");
        pos = intent.getExtras().getInt("pos");

        imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setImageURI( Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
                 String.valueOf(id)));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

and this is my grid adapter class :

package com.example.gridviewgallery;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;

public class GridAdapter extends CursorAdapter
{
    //private Context mContext;
    //private Cursor mCursor;
    private int mColumnIndex;
    //private ImageView imageView;

    @SuppressWarnings("deprecation")
    public GridAdapter(Context context, Cursor c, int ci) {
        super(context, c);
        mContext = context;
        mCursor = c;
        mColumnIndex = ci;
    }

    @Override
    public void bindView(View convertView, Context context, Cursor curs)
    {
        ImageView imageView = (ImageView) convertView;
        int id = curs.getInt(mColumnIndex);
        imageView.setImageURI( Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
             String.valueOf(id)));
        imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setPadding(8, 8, 8, 8);
        //imageView = (ImageView) convertView.findViewById(R.id.image);
    }

    @Override
    public View newView(Context context, Cursor curs, ViewGroup parent)
    {
        /*imageView = new ImageView(mContext);

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.grid_image, parent, false);

       /* int id = curs.getInt(mColumnIndex);
        imageView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, String.valueOf(id)));
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);*/

        //return retView;
        return new ImageView(context);
    }

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

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

    public int mygetItemId(int position) {
        return 0;
    }

}

Thanks.

user3783005
  • 542
  • 1
  • 9
  • 20
  • Check out this link http://stackoverflow.com/questions/13796382/android-viewpager-as-image-slide-gallery – Jagdeep Singh Jul 16 '14 at 09:24
  • thanks man .. but i am new at android programing and i didnt realy understand what was written .. can you show me how to do it in my case ? – user3783005 Jul 16 '14 at 09:34
  • See here: http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/ – RED_ Jul 16 '14 at 09:50

0 Answers0