Android版的onClick方法ImageSwitcher [英] Android onClick method ImageSwitcher

查看:150
本文介绍了Android版的onClick方法ImageSwitcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我得到了有2图像,就好像现在,但最终将有更多的一个阵列。
从阵列图像切换器每隔几秒钟。我坚持用的onClick方法。当该图像的新活动类用户点击打开了终极目标。每个图像有它自己的活动类,因此,当用户点击它,它应被定向到该特定类

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

 公共无效的onClick(视图v){
       意向N = NULL;
       INT ID = v.getId();
       开关(ID){
           案例R.id.someId:
                N =新意图(getActivity(),FragMent1.class);
                打破;
           案例R.id.someOtherId:
                N =新意图(getActivity(),FragMent2.class);
                打破;
        }

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

 公共类动画片段扩展实现AnimationListener {    公共HomeAnimation(){}
    私有静态布尔标志= FALSE;
     动画动画;
     私人处理程序处理程序;
     公共ImageSwitcher mSwitcher,mSwitcher1;    INT textToShow [] = {R.drawable.dabangg,R.drawable.car};    INT messageCount = textToShow.length;
    INT CURRENTINDEX = -1;
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
       视图V = inflater.inflate(R.layout.imgswitch,集装箱,FALSE);
        mSwitcher =(ImageSwitcher)v.findViewById(R.id.imageSwitcher1);
        mSwitcher.setFactory(mFactory);
        动画= AnimationUtils.loadAnimation(getActivity(),R.anim.fade_in);        mSwitcher.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                意向N = NULL;
                 INT ID = v.getId();
                //片段片段= NULL;
                //捆绑ARGS =新包();
                开关(v.getId()){
                情况下0:
                    N =新意图(getActivity(),Fragment1.class);
                    。getActivity()startActivity(N);
                    打破;
                情况1:
                    N =新意图(getActivity(),Fragment2.class);
                    。getActivity()startActivity(N);
                    打破;
                }
            }
        });
        //设置动画监听器
        anim.setAnimationListener(本);
        返回伏;
    }公共无效updateTextView(){    INT maxIndex = textToShow.length;
    随机随机=新的随机();
    INT generatedIndex = random.nextInt(maxIndex);
    mSwitcher.setImageResource(textToShow [generatedIndex]);

}

下面是我在更新的情况下,如果有人需要它

 公共无效updateTextView(){        INT maxIndex = textToShow.length;
        随机随机=新的随机();
        最终诠释generatedIndex = random.nextInt(maxIndex);
        mSwitcher.setImageResource(textToShow [generatedIndex]);
        mSwitcher.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                意向N = NULL;                 开关(generatedIndex){
                 情况下0:
                     N =新意图(getActivity(),Fragment1.class);
                       。getActivity()startActivity(N);
                       打破;                 情况1:
                      N =新意图(getActivity(),Fragment2.class);
                       。getActivity()startActivity(N);
                       打破;
                 案例2:
                       N =新意图(getActivity(),Fragment3.class);
                       。getActivity()startActivity(N);
                       打破;                }
            }
         });


解决方案

我现在没有时间来测试这个权利,我不能完全肯定我理解您的问题,但试试这个,看看它是什么你要。我会把意见在code

  mSwitcher.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            意向N = NULL;
             INT ID = v.getId();
            //片段片段= NULL;
            //捆绑ARGS =新包();
            开关(v.getId()){
            情况下0:
              / *
                 下面应该给你所显示的图像索引
                 你可以使用索引比较这对你的数组中的IDS
              * /
               如果(v.getDisplayedChild()== 0){//如果在阵列第一图像要这样做
                   N =新意图(getActivity(),Fragment1.class);
                   。getActivity()startActivity(N);
               }
               如果(v.getDisplayedChild()== 1){//如果在阵列第二图象要这样做
                   N =新意图(getActivity(),Fragment2.class);
                   。getActivity()startActivity(N);
               }
              打破;

Basically i got one array which has 2 image as if for now but eventually will have more. Images from array switcher every few seconds. i'm stuck with onClick method. Ultimate goal when user clicks on that image new Activity class opens up. Each image has its own Activity class so when user clicks on it, it should be directed to that particular class.

I did got some help yesterday

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;
        }

i can set first case statement to be "R.id.textSwitcher" but second is the problem. Because i want images animates to at one place, new R.id... means new location also new "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]);

}

Here is a what i updated in case if anyone needs it

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;

                }
            }
         });

解决方案

I don't have time to test this right now and I'm not completely sure I understand where your problem is but try this and see if it is what you want. I will put comments in the code

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;

这篇关于Android版的onClick方法ImageSwitcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆