gpt4 book ai didi

android - 加载图像并单击上一个按钮不会在 android 中加载图像

转载 作者:行者123 更新时间:2023-11-30 01:01:17 24 4
gpt4 key购买 nike

这是我的代码

private int[] imageArray;
private int currentIndex;
private int startIndex;
private int endIndex;
Button nxt,prv;

在 OnCreate 方法中

    prv = (Button)findViewById(R.id.zoomIn);
nxt = (Button)findViewById(R.id.zoomOut);
imageArray = new int[4];
imageArray[0] = R.drawable.ic_launcher;
imageArray[1] = R.drawable.abc_ab_share_pack_mtrl_alpha;
imageArray[2] = R.drawable.abc_btn_check_to_on_mtrl_000;
imageArray[3] = R.drawable.abc_btn_rating_star_on_mtrl_alpha;

startIndex = 0;
endIndex = 3;

nxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextImage();
}
});
prv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
previousImage();
}
});

方法

public void nextImage() {
System.out.println(" currentIndex nextImage" + currentIndex);
if(currentIndex != -1 && currentIndex <= endIndex ) {
ivImage.setImageResource(imageArray[currentIndex]);
currentIndex++;
}
else if(currentIndex == -1){
currentIndex++;
}

new Runnable() {
@Override
public void run() {
if (currentIndex > endIndex) {
currentIndex--;
previousImage();
} else {
nextImage();
}

}
}; // here 1000(1 second) interval to change from current to next image

}

public void previousImage() {
System.out.println(" currentIndex previousImage" + currentIndex);
if(currentIndex != -1 && currentIndex <= endIndex) {
ivImage.setImageResource(imageArray[currentIndex]);
currentIndex--;
}
else if(currentIndex > endIndex){
currentIndex--;
}

new Runnable() {
@Override
public void run() {
if (currentIndex < startIndex) {
currentIndex++;
nextImage();
} else {
previousImage(); // here 1000(1 second) interval to change from current to previous image
}
}
};
}

当点击 2 次后 currentIndex = 4 时,它会加载上一张或下一张图像。我需要它只需单击一下即可移动到上一张或下一张图像。我的逻辑中缺少什么。谁能帮忙。提前致谢。

最佳答案

使用您的代码的完整工作演示

public class MainActivity extends AppCompatActivity {

private Context context;
private int[] imageArray;
private int currentIndex = 0;
Button nxt, prv;
ImageView image;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

image = (ImageView) findViewById(R.id.imageView);
prv = (Button) findViewById(R.id.previous);
nxt = (Button) findViewById(R.id.next);

imageArray = new int[4];
imageArray[0] = R.mipmap.ic_launcher;
imageArray[1] = R.drawable.abc_ab_share_pack_mtrl_alpha;
imageArray[2] = R.drawable.abc_btn_check_to_on_mtrl_000;
imageArray[3] = R.drawable.abc_btn_rating_star_on_mtrl_alpha;

nxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentIndex++;
nextImage();
}
});
prv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentIndex--;
previousImage();
}
});

}

private void nextImage() {
System.out.println(" currentIndex previousImage" + currentIndex);
if (currentIndex != -1) {
if (currentIndex < imageArray.length) {
image.setImageResource(imageArray[currentIndex]);
} else {
currentIndex = 0;
image.setImageResource(imageArray[currentIndex]);
}
} else {
currentIndex = imageArray.length;
image.setImageResource(imageArray[currentIndex]);
}
}

private void previousImage() {
System.out.println(" currentIndex previousImage" + currentIndex);
if (currentIndex != -1) {
if (currentIndex < imageArray.length) {
image.setImageResource(imageArray[currentIndex]);
} else {
currentIndex = 0;
image.setImageResource(imageArray[currentIndex]);
}
} else {
currentIndex = imageArray.length-1;
image.setImageResource(imageArray[currentIndex]);
}
}
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="horizontal">


<Button
android:id="@+id/next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Next"
android:textSize="20dp" />

<Button
android:id="@+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/next"
android:layout_alignStart="@+id/next"
android:layout_below="@+id/next"
android:text="Previous"
android:textSize="20dp" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="@+id/previous" />

</RelativeLayout>

关于android - 加载图像并单击上一个按钮不会在 android 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39347565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com