androidandroid-studiogridviewonitemclicklistenerswipeview

How to set onItemClick in gridView


I am trying to make a project where the images are arranged in Gridview and when ever the image is clicked it will open the image in the ViewPager. But I am not able to set as I am a beginner in Android. Here is the code of my GridView and Swipeview.

GridView

package com.example.neelaysrivastava.pr19;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;


public class Main2Activity extends Activity {
Integer []imgID={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,
R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    GridView gridView=(GridView)findViewById(R.id.gridView);
    gridView.setAdapter(new ImageAdapter(this));
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getBaseContext(),"pic"+(position)+"select",Toast.LENGTH_SHORT).show();
        }
    });

}
public class ImageAdapter extends BaseAdapter{
   private Context context;
    public ImageAdapter(Context c){
        context =c;
    }
    @Override
    public int getCount() {
        return imgID.length;
    }

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

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

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


        ImageView imageView ;
        if(convertView==null){
            imageView=new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);}
        else{
            imageView=(ImageView)convertView;

        }
         imageView.setImageResource(imgID[position]);
        return imageView;
    }
}

}

SwipeView

package com.example.neelaysrivastava.pr19;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
 import android.view.View;

public class MainActivity extends Activity {
ViewPager viewPager;
CustomeSwipeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    viewPager =(ViewPager)findViewById(R.id.viewpager);
    adapter =new CustomeSwipeAdapter(this);
    viewPager.setAdapter(adapter);

}
public void back(View v){
    startActivity(new Intent(MainActivity.this,Main2Activity.class));
}


}

SwipeViewAdapter

package com.example.neelaysrivastava.pr19;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;


public class CustomeSwipeAdapter extends PagerAdapter {
private Context ctx;
private LayoutInflater layoutInflater;
private int []imgID={R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,
        R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10};
public CustomeSwipeAdapter(Context ctx) {
    this.ctx=ctx;
}

@Override
public int getCount() {
    return imgID.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return (view==(LinearLayout)object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
  layoutInflater =(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View item_view =layoutInflater.inflate(R.layout.extra,container,false);
    ImageView imageView=(ImageView)item_view.findViewById(R.id.imageView);
    Button button=(Button)item_view.findViewById(R.id.button);
    Button button1=(Button)item_view.findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    imageView.setImageResource(imgID[position]);
    container.addView(item_view);
    return item_view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((LinearLayout)object);
}
}

Solution

  • This works perfectly for me.

    Main2Activity.java

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(Main2Activity.this, MainActivity.class);
                intent.putExtra("picture", position);
                startActivity(intent);
            }
        });
    

    MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        adapter = new CustomeSwipeAdapter(this);
        viewPager.setAdapter(adapter);
        // add this
        viewPager.setCurrentItem(getIntent().getIntExtra("picture", 0));
    }