4

我正在使用它记录触摸的 touchevent 列表器,并且我使用 viewpager 进行 swping.. 因为我们知道 java 的缺点是按值传递,我仍在尝试在匿名内部类中传递布尔值。在 switch stmt 中更改的值不会保留在 switch stmt 之外。flag2 值。有人对此有解决方案吗?我的代码如下

 parent.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                //flag2=!flag2;
                switch(event.getAction())
                {
                 case MotionEvent.ACTION_MOVE: 
                        awesomePager.requestDisallowInterceptTouchEvent(true);
                        //flag2=false;
                        break;
                case MotionEvent.ACTION_UP:
                    awesomePager.requestDisallowInterceptTouchEvent(true);
                     if(flag)
                     {
                       upperdock.setClickable(false);
                        upperdock.bringToFront();
                        tocparent.bringToFront();
                        tocbottom.bringToFront();
                        upperdock.setVisibility(RelativeLayout.VISIBLE);
                        tocparent.setVisibility(LinearLayout.VISIBLE);
                        tocbottom.setVisibility(LinearLayout.VISIBLE);
                        flag=false;
                    // obj.one(event.getAction());
                     }
                     else
                 {

                        parent.bringToFront();
                        upperdock.setVisibility(RelativeLayout.INVISIBLE);
                        tocparent.setVisibility(LinearLayout.INVISIBLE);
                        tocbottom.setVisibility(LinearLayout.INVISIBLE);
                        flag=true;
                 //obj.one(event.getAction());
                     }


                    flag2=true;
                    break;

                case MotionEvent.ACTION_CANCEL:
                    awesomePager.requestDisallowInterceptTouchEvent(false);
                    flag2=false;
                    break;


                }


            return flag2;

            }

        });
public class SimpleViewPagerforAndroidActivity extends Activity {
/** Called when the activity is first created. */
 boolean flag2=true;
//rest of the code
}

请帮助...需要它..

package com.beanie.samples.viewpager;

       //set of imports

      public class SimpleViewPagerforAndroidActivity extends Activity {
    /** Called when the activity is first created. */


     volatile boolean flag2=true;




       AwesomePagerAdapter awe1;




         @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);


        cxt = this;
        data = new ArrayList<WebView>();

       awesomePager = (ViewPager) findViewById(R.id.viewPager);

        awesomePager.setAdapter(new AwesomePagerAdapter(this,data));

        Bundle extras = getIntent().getExtras();

           Boolean dock2checked = extras.getBoolean("dock1");

           if(dock2checked)
           {
               Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(dock2checked), 5000);
               toast1.show();
           }
           else
           {
               Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(dock2checked), 5000);
               toast1.show();
           }







    awesomePager.setOffscreenPageLimit(11);

   //       
    web0 = new WebView(cxt);
    web0.loadUrl("file:///android_asset/1/01-Cover.html");
  WebSettings webSettings0 = web0.getSettings();
  webSettings0.setJavaScriptEnabled(true);

    web1 = new WebView(cxt);
    web1.loadUrl("file:///android_asset/1/01-Cover.html");
  WebSettings webSettings1 = web1.getSettings();
  webSettings1.setJavaScriptEnabled(true);
    web2 = new WebView(cxt);
    web2.loadUrl("file:///android_asset/5/05-01.html");
    WebSettings webSettings2 = web2.getSettings();
      webSettings2.setJavaScriptEnabled(true);

     web3 = new WebView(cxt);
    web3.loadUrl("file:///android_asset/1/01-Cover.html");
    WebSettings webSettings3 = web3.getSettings();
      webSettings3.setJavaScriptEnabled(true);
    web4 = new WebView(cxt);
    web4.loadUrl("file:///android_asset/5/05-01.html");
    WebSettings webSettings4 = web4.getSettings();
      webSettings4.setJavaScriptEnabled(true);
    web5 = new WebView(cxt);
    web5.loadUrl("file:///android_asset/1/01-Cover.html");
    WebSettings webSettings5 = web5.getSettings();
      webSettings5.setJavaScriptEnabled(true);
    web6 = new WebView(cxt);
    web6.loadUrl("file:///android_asset/5/05-01.html");
    WebSettings webSettings6 = web6.getSettings();
      webSettings6.setJavaScriptEnabled(true);

    web7 = new WebView(cxt);
    web7.loadUrl("file:///android_asset/1/01-Cover.html");
    WebSettings webSettings7 = web7.getSettings();
      webSettings7.setJavaScriptEnabled(true);

    web8 = new WebView(cxt);
    web8.loadUrl("file:///android_asset/5/05-01.html");
    WebSettings webSettings8 = web8.getSettings();
      webSettings8.setJavaScriptEnabled(true);
    web9 = new WebView(cxt);
    web9.loadUrl("file:///android_asset/1/01-Cover.html");
    WebSettings webSettings9 = web9.getSettings();
      webSettings9.setJavaScriptEnabled(true);

    web10 = new WebView(cxt);
    web10.loadUrl("file:///android_asset/5/05-01.html");
    WebSettings webSettings10 = web10.getSettings();
      webSettings10.setJavaScriptEnabled(true);



    data.add(web0);
    data.add(web1);
    data.add(web2);
    data.add(web3);
    data.add(web4);
    data.add(web5);
    data.add(web6);
    data.add(web7);
    data.add(web8);
    data.add(web9);
    data.add(web10);





      tocparent=(LinearLayout) findViewById(R.id.tocparent);
      dock2=(Button)findViewById(R.id.button1);
      //dock2.setVisibility(Button.INVISIBLE);
      tocparent.setVisibility(LinearLayout.INVISIBLE);

      topmost=(RelativeLayout) findViewById(R.id.topmost);
      parent=(RelativeLayout) findViewById(R.id.rl1);
        upperdock=(RelativeLayout) findViewById(R.id.upperdock);
        tocparent=(LinearLayout) findViewById(R.id.tocparent);
        tocbottom=(LinearLayout) findViewById(R.id.linearLayout3);
        upperdock.setVisibility(RelativeLayout.INVISIBLE);
        tocparent.setVisibility(LinearLayout.INVISIBLE);
        tocbottom.setVisibility(LinearLayout.INVISIBLE);







      web0.loadUrl("javascript:fhhf()");




parent.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                //flag2=!flag2;
                switch(event.getAction())
                {
                 case MotionEvent.ACTION_MOVE: 
                        awesomePager.requestDisallowInterceptTouchEvent(true);
                        //flag2=false;
                        break;
                case MotionEvent.ACTION_UP:
                    awesomePager.requestDisallowInterceptTouchEvent(true);
                     if(flag)
                     {
                       upperdock.setClickable(false);
                        upperdock.bringToFront();
                        tocparent.bringToFront();
                        tocbottom.bringToFront();
                        upperdock.setVisibility(RelativeLayout.VISIBLE);
                        tocparent.setVisibility(LinearLayout.VISIBLE);
                        tocbottom.setVisibility(LinearLayout.VISIBLE);
                        flag=false;
                    // obj.one(event.getAction());
                     }
                     else
                 {

                        parent.bringToFront();
                        upperdock.setVisibility(RelativeLayout.INVISIBLE);
                        tocparent.setVisibility(LinearLayout.INVISIBLE);
                        tocbottom.setVisibility(LinearLayout.INVISIBLE);
                        flag=true;
                 //obj.one(event.getAction());
                     }
                    flag2=true;


                    break;

                case MotionEvent.ACTION_CANCEL:
                    awesomePager.requestDisallowInterceptTouchEvent(false);

                    flag2=false;
                    //Toast.makeText(getApplicationContext(), s, 220).show();
                     //obj.one(event.getAction());
                    break;


                }
      /*  if(event.getAction()==MotionEvent.ACTION_UP)
        {
            flag2=true;
        }
        else if(event.getAction()==MotionEvent.ACTION_CANCEL)
        {
            flag2=false;
        }*/

            return flag2;

            }

        });








      awesomePager.setOnPageChangeListener(new OnPageChangeListener() {

           public void onPageSelected(int arg0) {
               //Toast toast1 = Toast.makeText(getBaseContext(), "PageSelected"+ arg0, 5000);
                //Toast1.show();
            if(arg0==0)
            {
            //web0.loadUrl("javascript:fhhf()");

    //              awesomePager.getChildAt(arg0 - 1);
                web0.loadUrl("javascript:fhhf1()");
                web0.loadUrl("javascript:fhhf()");

            }
            else if(arg0==1)
            {
                web1.loadUrl("javascript:fhhf1()");
                web1.loadUrl("javascript:fhhf()");


            }

            else if(arg0==3)
            {

                web3.loadUrl("javascript:fhhf1()");
            //Toast toast2 = Toast.makeText(getBaseContext(), "fun"+ arg0, 5000);
            //Toast2.show();
            }
            else if(arg0==5)
                web5.loadUrl("javascript:fhhf()");
            else if(arg0==7)
                web7.loadUrl("javascript:fhhf()");
            else if(arg0==9)
                web9.loadUrl("javascript:fhhf()");
           }

           public void onPageScrolled(int arg0, float arg1, int arg2) {

           }



           public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

            int o = awesomePager.getCurrentItem();
            WebView temp = (WebView) awesomePager.getChildAt(o); 

               if(arg0 == 1)
               {



                temp.loadUrl("javascript:fhhf1()");
               }

               else if(arg0 == 0)
               {

                   temp.loadUrl("javascript:fhhf()");
               }

           }
          });






}
 public boolean fun1()
   {
       return true;
   }
 public boolean fun2()
   {
       return false;
   }


private class AwesomePagerAdapter extends PagerAdapter  implements           ViewPager.OnPageChangeListener {

    private List<WebView> data;
    private Context ctx1;

    public AwesomePagerAdapter(Context ctx, List<WebView> data) {
        this.ctx1 = ctx;
        this.data = data;
    }







    @Override
    public int getCount() {
            return NUM_AWESOME_VIEWS;
    }

    @Override
    public Object instantiateItem(View collection, int position) {



        data.get(position).setBackgroundColor(R.color.black);
        return data.get(position);

      }

    @Override
    public void destroyItem(View collection, int position, Object view) {

            ((ViewPager) collection).removeView((WebView) view);
    }



    @Override
    public boolean isViewFromObject(View view, Object object) {



            return view==((WebView)object);

    }



    @Override
    public void finishUpdate(View arg0) {
    }


    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {}

    @Override
    public Parcelable saveState() {
            return null;
    }

    @Override
    public void startUpdate(View arg0) {}



    public void onPageScrollStateChanged(int position) {


    }



    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }



    public void onPageSelected(int position) {

    }

    }


 }

提前致谢

4

2 回答 2

0

first make a method in SimpleViewPagerforAndroidActivity as given below u can also either use void if u don't want to return anything

 boolean method(Boolean flag){
    flag2=flag;
    return flag2;
}

Make an object in other class as shown below

SimpleViewPagerforAndroidActivity abc=new SimpleViewPagerforAndroidActivity();

and in ontouch event in switch statement call method by

abc.method(false or true);  

this will update ur flag2 and u can check as by print in Log by

Log.d("flag2",""+abc.flag2);
于 2012-06-01T11:25:21.033 回答
0

也许尝试强制它在 UI 线程上运行?

IE:

public void passBoolean(final boolean boolToPass){      
    runOnUiThread(new Runnable() {
        public void run() {

            //Do whatever here on the UI thread
        }
    });    
}
于 2012-06-01T16:39:57.570 回答