I am new to Android studio and I been learning viewpager to create a image slider. My image slider has 4 image in each slide. I had tried view.setOnclick in the adapter but it only setting the click on the entire the slide (not on each image). I also tried getting the each image by id and tried to clickable but not working.
Here is my code:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
Adapter adapter;
List<Model> models;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
models = new ArrayList<>();
models.add(new Model(R.raw.black,R.raw.captain,R.raw.lone,R.raw.iron));
models.add(new Model(R.raw.iron,R.raw.lone,R.raw.black,R.raw.captain));
adapter = new Adapter(models, this);
viewPager = findViewById(R.id.viewpager1);
viewPager.setAdapter(adapter);
}
Adapter.java:
public class Adapter extends PagerAdapter {
private List<Model> models;
private LayoutInflater layoutInflater;
private Context context;
public Adapter(List<Model> models, Context context) {
this.models = models;
this.context = context;
}
@Override
public int getCount() {
return models.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view.equals(object);
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.item, container, false);
ImageView image1,image2,image3,image4;
image1 = view.findViewById(R.id.image);
image2 = view.findViewById(R.id.image1);
image3 = view.findViewById(R.id.image2);
image4 = view.findViewById(R.id.image3);
image1.setImageResource(models.get(position).getImage());
image2.setImageResource(models.get(position).getImage1());
image3.setImageResource(models.get(position).getImage2());
image4.setImageResource(models.get(position).getImage3());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(position == 0) {
AlertDialog.Builder alert = new AlertDialog.Builder(context).setTitle("Alert").setMessage("Hi").setCancelable(true);
alert.show();
} else if(position == 1) {
AlertDialog.Builder alert = new AlertDialog.Builder(context).setTitle("Alert").setMessage("Hi 2").setCancelable(true);
alert.show();
}
}
});
container.addView(view,0);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View)object);
}
Model.java:
public class Model {
private int image;
private int image1;
private int image2;
private int image3;
public Model(int image, int image1,int image2,int image3) {
this.image = image;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public int getImage1() {
return image1;
}
public void setImage1(int image1) {
this.image1 = image1;
}
public int getImage2() {
return image2;
}
public void setImage2(int image2) {
this.image2 = image2;
}
public int getImage3() {
return image3;
}
public void setImage3(int image3) {
this.image3 = image3;
}
}
instead of
view.setOnClickListener(...
set OnClickListener
to every image you have
image1.setOnClickListener(...
image2.setOnClickListener(...
image3.setOnClickListener(...
image4.setOnClickListener(...