2016-09-06 4 views
1

で画像を読み込むdoesnot私のコードは、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 
      } 
     } 
    }; 
} 

currentIndex = 4で

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

です2回クリックすると、前または次の画像が読み込まれます。 ワンクリックで、以前の画像または次の画像に移動する必要があります。 私のロジックには何が欠けていますか?誰も助けることができます。前もって感謝します。

+0

activity_main –

+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]); 
     } 
    } 
} 

完全に動作するデモ

<?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> 
関連する問題