【问题标题】:Android onClick method ImageSwitcherAndroid onClick 方法 ImageSwitcher
【发布时间】:2014-03-25 05:42:56
【问题描述】:

基本上我得到了一个数组,它现在有 2 个图像,但最终会有更多。 每隔几秒钟从阵列切换器中获取图像。我坚持使用 onClick 方法。当用户单击该图像时,新的 Activity 类打开的最终目标。每个图像都有自己的 Activity 类,因此当用户单击它时,它应该被定向到该特定类。

我昨天确实得到了一些帮助

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(new 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;

                }
            }
         });

【问题讨论】:

  • 你的问题没有问题。
  • 终极目标,当用户点击该图像时,新的 Activity 类就会打开。每个图像都有自己的 Activity 类,因此当用户点击它时,它应该被定向到该特定类。
  • 问题是您得到的idImageSwitcher id,但您需要当前显示的实际Image?如果是这样,那么也许getDisplayedChild() 就是你想要的
  • codeMagic - 我已将完整代码添加到原始问题中,您能帮帮我吗,我怎样才能找到当前显示图像的 ID?

标签: android android-fragments onclicklistener


【解决方案1】:

我现在没有时间对此进行测试,而且我不完全确定我了解您的问题所在,但请尝试一下,看看它是否是您想要的。我会把cmets放到代码里

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;

【讨论】:

  • @Mihir 好的,如果这不是您要找的,请告诉我
  • 它现在可以工作了,请看看原来的问题。我更新了一些小东西。现在我唯一需要担心的是应用程序一直在崩溃,因为它可能在主线程中做了大量工作。
  • @Mihir 我查看了您的原始问题,但没有看到任何会导致崩溃的内容。也许您还有其他代码正在循环执行大量操作或类似操作?
  • 我已将完整代码添加到原始问题中。我确实有可运行的动画。我在 Logcat 中注意到,当我单击任何图像并且当它即将进入新的活动类时,大约 300-500 帧会跳过,这就是我看到在主线程中做大量工作的时候。我的计划是将活动转换为片段,看看是否会有所作为。
  • codeMagic - 我发现了问题,问题是数组中使用的图像大小太大.. 现在一切正常 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-07
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多