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

时间:2016-09-06 11:07:43

标签: android imageview buttonclick

这是我的代码

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时,它会加载上一张或下一张图像。 我需要它只需点击一下即可移动到上一张或下一张图片。 我的逻辑中缺少什么。谁能帮忙。提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用您的代码进行全面演示

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>