2012-06-01 47 views
4

通過引用我到達touchevent listner它記錄觸摸和我已經使用viewpager swping ..因爲我們知道java的缺點是通過價值的東西,我米仍然嘗試在匿名內部類中傳遞布爾值。在交換機stmt中更改的值不會在交換機stmt外持續存在。 flag2值.anyone有解決方案嗎?我的代碼如下提前不能通過引用的布爾變量在android

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) { 

    } 

    } 


} 

感謝

+0

,其值是沒有得到改變? –

+0

flag2值..它在全局聲明爲真 – Zombie

+0

什麼是匿名內部類?你不要發表一個。但是,非靜態匿名內部類應該能夠更改類中字段的值,但不能改變方法範圍變量,除非它們是final(它不會幫助您更改布爾值)。 –

回答

0

先的方法在SimpleViewPagerforAndroidActivity如下所示你也可以使用無效如果你不希望返回任何

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

做一個對象在其他類如下圖所示

SimpleViewPagerforAndroidActivity ABC =新SimpleViewPagerforAndroidActivity();

,並在開關語句調用方法ontouch事件由

abc.method(false or true); 

這將更新烏爾FLAG 2和u可以通過

Log.d("flag2",""+abc.flag2); 
+0

我也試過這個。因爲對象在java中有引用類型。但這太辛苦的工作..採取了布爾陣列,並試圖它..也dint工作..有什麼固定通過viewpager滑動和touchListener觸摸效果都在一起..我的意思是,一邊刷它不能採取touchListener行動? – Zombie

+0

我已經在日誌中檢查並得到值爲true,這是parfacr如果沒有得到比顯示您的整個代碼 – Khan

+0

看到整個代碼 – Zombie

0

檢查,通過在登錄打印也許嘗試迫使它在UI線程上運行?

IE:

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

      //Do whatever here on the UI thread 
     } 
    });  
}