1.MainActivity
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
ImageViewModel imageViewModel = ViewModelProviders.of(this).get(ImageViewModel.class);
imageViewModel.init();
imageViewModel.getCurImage().observe(this, this::onChanged);
binding.last.setOnClickListener(view -> {
imageViewModel.lastImage(() -> {
Toast.makeText(this, "已经是第一张图片!!!", Toast.LENGTH_SHORT).show();
});
});
binding.next.setOnClickListener(view -> {
imageViewModel.nextImage(() -> {
Toast.makeText(this, "没有下一张图片!!!", Toast.LENGTH_SHORT).show();
});
});
}
private void onChanged(Integer drawable) {
@SuppressLint("UseCompatLoadingForDrawables")
Drawable newDrawable = getResources().getDrawable(drawable, null);
binding.image.setImageDrawable(newDrawable);
}
}
2.ViewModel
public class ImageViewModel extends ViewModel {
private final MutableLiveData<Integer> curImage = new MutableLiveData<>();
private final List<Integer> images = ImageRepository.getInstance().getImages();
public MutableLiveData<Integer> getCurImage() {
return curImage;
}
public void init() {
if (images.size() > 0) {
curImage.postValue(images.get(0));
}
}
public void lastImage(Runnable runnable) {
int index = images.indexOf(curImage.getValue());
if (index > 0) {
curImage.postValue(images.get(index - 1));
} else {
runnable.run();
}
}
public void nextImage(Runnable runnable) {
int index = images.indexOf(curImage.getValue());
if (index < images.size() - 1) {
curImage.postValue(images.get(index + 1));
} else {
runnable.run();
}
}
}
3.Repository
public class ImageRepository {
private static ImageRepository IMAGE_REPOSITORY = null;
private List<Integer> images = new ArrayList<>();
private ImageRepository() {
images.add(R.drawable.ic_0);
images.add(R.drawable.ic_1);
images.add(R.drawable.ic_2);
}
public static ImageRepository getInstance() {
if (IMAGE_REPOSITORY == null) {
synchronized (ImageRepository.class) {
IMAGE_REPOSITORY = new ImageRepository();
}
}
return IMAGE_REPOSITORY;
}
public List<Integer> getImages() {
return images;
}
}
4.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="TODO" />
<Button
android:id="@+id/last"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:text="上一个"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintRight_toLeftOf="@+id/next"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/image" />
<Button
android:id="@+id/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:text="下一个"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintLeft_toRightOf="@+id/last"
app:layout_constraintTop_toBottomOf="@+id/image" />
</androidx.constraintlayout.widget.ConstraintLayout>