I have an image slider and I get two photos (photo link address) from my server.
I have also created a button in my activity and I want to click on it to display the replaced photos on the server.
Photos are replaced when I exit the app and log in again, but I want to reload by clicking the button.
ImageSlider imageSliderFirst;
Button btn_ref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// init
imageSliderFirst = findViewById(R.id.slider_first);
btn_ref = findViewById(R.id.btn_ref);
//-------------------------
List<SlideModel> slideModel1 = new ArrayList<>();
slideModel1.add(new SlideModel("https://iranhonarbazar.ir/danial/photos/mp_slide.jpg"));
slideModel1.add(new SlideModel("https://iranhonarbazar.ir/danial/photos/up_slide.jpg"));
imageSliderFirst.setImageList(slideModel1, true);
btn_ref.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// refresh slider ???
//-------------------------
}
});
You must create an Adapter
for the ImageSlider
to manage adding the image to it.
image_layout.xml
<ImageView
android:id="@+id/myimage"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:contentDescription="@string/app_name" />
public class SliderAdapter extends SliderViewAdapter<SliderAdapter.SliderAdapterViewHolder> {
private final ArrayList<SliderData> list;
public SliderAdapter(Context context) {
this.list = new ArrayList<>();
}
public void setList(ArrayList<SliderData> list){
this.list = list;
}
@Override
public SliderAdapterViewHolder onCreateViewHolder(ViewGroup parent) {
// here you going to set by the layout you create
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_layout, null);
return new SliderAdapterViewHolder(inflate);
}
@Override
public void onBindViewHolder(SliderAdapterViewHolder viewHolder, final int position) {
final SliderData sliderItem = list.get(position);
// I Use Glide to load the image to the ImageView
Glide.with(viewHolder.itemView)
.load(sliderItem.getImgUrl())
.fitCenter()
.into(viewHolder.imageViewBackground);
}
@Override
public int getCount() {
return list.size();
}
static class SliderAdapterViewHolder extends SliderViewAdapter.ViewHolder {
View itemView;
ImageView imageViewBackground;
public SliderAdapterViewHolder(View itemView) {
super(itemView);
imageViewBackground = itemView.findViewById(R.id.myimage);
this.itemView = itemView;
}
}
}
Activity
you can set the SliderAdapter
of the ImageSlider
SliderAdapter sliderAdapter;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
imageSliderFirst = findViewById(R.id.slider_first);
btn_ref = findViewById(R.id.btn_ref);
sliderAdapter = new SliderAdapter(this);
imageSliderFirst.setSliderAdapter(adapter);
btn_ref.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<SlideModel> slideModel1 = new ArrayList<>();
slideModel1.add(new SlideModel("https://iranhonarbazar.ir/danial/photos/mp_slide.jpg"));
slideModel1.add(new SlideModel("https://iranhonarbazar.ir/danial/photos/up_slide.jpg"));
adapter.setList(slideModel1)
}
});
}