2014-03-25 35 views
0

基本上我有一个数组,其中有2个图像,好像现在,但最终会有更多。 每隔几秒从阵列切换器获取图像。我坚持使用onClick方法。用户点击该图像时的最终目标新的Activity类打开。每个图像都有自己的Activity类,所以当用户点击它时,它应该被引导到那个特定的类。Android的onClick方法ImageSwitcher

我也得到了一些帮助,昨天

public void onClick(View v) { 
     Intent n = null; 
     int id = v.getId(); 
     switch (id){ 
      case R.id.someId: 
       n = new Intent(getActivity(), FragMent1.class); 
       break; 
      case R.id.someOtherId: 
       n = new Intent(getActivity(), FragMent2.class); 
       break; 
     } 

我可以设置第一个case语句是“R.id.textSwitcher”,但第二个是问题。因为我想要的图像在一个地方会以动画方式,新R.id ...意味着新的位置也有新的“mSwitcher1.setOnClickListener(新View.OnClickListener()”

public class Animation extends Fragment implements AnimationListener { 

    public HomeAnimation(){} 
    private static boolean flag = false; 
    Animation anim; 
    private Handler handler; 
    public ImageSwitcher mSwitcher,mSwitcher1; 

    int textToShow[] = {R.drawable.dabangg, R.drawable.car }; 

    int messageCount = textToShow.length; 
    int currentIndex = -1; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.imgswitch, container, false); 
     mSwitcher = (ImageSwitcher) v.findViewById(R.id.imageSwitcher1); 
     mSwitcher.setFactory(mFactory); 
     anim = AnimationUtils.loadAnimation(getActivity(),R.anim.fade_in); 

     mSwitcher.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent n = null; 
       int id = v.getId(); 
       // Fragment fragment = null; 
       //Bundle args = new Bundle(); 
       switch (v.getId()){ 
       case 0: 
        n = new Intent(getActivity(), Fragment1.class); 
        getActivity().startActivity(n); 
        break; 
       case 1: 
        n = new Intent(getActivity(), Fragment2.class); 
        getActivity().startActivity(n); 
        break; 
       } 
      } 
     }); 
     // set animation listener 
     anim.setAnimationListener(this); 
     return v;  
    } 

public void updateTextView() { 

    int maxIndex = textToShow.length; 
    Random random = new Random(); 
    int generatedIndex = random.nextInt(maxIndex); 
    mSwitcher.setImageResource(textToShow[generatedIndex]); 

}

这里是一个什么我在更新的情况下,如果有人需要它

public void updateTextView() { 

     int maxIndex = textToShow.length; 
     Random random = new Random(); 
     final int generatedIndex = random.nextInt(maxIndex); 
     mSwitcher.setImageResource(textToShow[generatedIndex]); 
     mSwitcher.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Intent n = null; 

       switch(generatedIndex){ 
       case 0: 
        n = new Intent(getActivity(), Fragment1.class); 
         getActivity().startActivity(n); 
         break; 

       case 1: 
         n = new Intent(getActivity(), Fragment2.class); 
         getActivity().startActivity(n); 
         break; 
       case 2: 
         n = new Intent(getActivity(), Fragment3.class); 
         getActivity().startActivity(n); 
         break; 

       } 
      } 
     }); 
+0

你的问题没有问题。 – ElDuderino

+0

最终目标,当用户点击该图像时,新的活动类将打开。每个图像都有自己的Activity类,所以当用户点击它时,它应该被引导到那个特定的类。 – Mihir

+0

问题是你得到的'id'是'ImageSwitcher'的''id',但你需要当前显示的实际'Image'?如果是这样,那么也许[getDisplayedChild()](http://developer.android.com/reference/android/widget/ViewAnimator.html#getDisplayedChild())是你想要的 – codeMagic

回答

1

我现在没有时间来测试这个权利,我不能完全肯定我理解您的问题,但试试这个,看看它是否是你想要的我会在代码

mSwitcher.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent n = null; 
      int id = v.getId(); 
      // Fragment fragment = null; 
      //Bundle args = new Bundle(); 
      switch (v.getId()){ 
      case 0: 
       /* 
       below should give you the index for the displayed image 
       you can compare this to the ids in your array using the index 
       */ 
       if (v.getDisplayedChild() == 0) { // if 1st image in array do this 
        n = new Intent(getActivity(), Fragment1.class); 
        getActivity().startActivity(n); 
       } 
       if (v.getDisplayedChild() == 1) { // if 2nd image in array do this 
        n = new Intent(getActivity(), Fragment2.class); 
        getActivity().startActivity(n); 
       } 
       break; 
+0

谢谢,会试试看。 – Mihir

+0

@Mhhir好吧,让我知道如果这不是你正在寻找 – codeMagic

+0

现在它的工作,请看看原来的问题。我已经更新了一些小东西。现在唯一需要担心的是应用程序一直在崩溃,因为它可能在主线程中做了很多工作。 – Mihir