2011-05-13 50 views
0

我有一個視圖,通過使用viewFlipper和onFling手勢來填充。當我扔屏幕填充下一個視圖時,需要2到3秒才能顯示下一個視圖。任何想法可能會發生什麼?這完全太慢了!用手勢填充viewFlipper的速度很慢

下面是我的一些代碼片段:

private void updateView() { 
     Log.i(DEBUG_TAG, "updateView"); 
     // Preferences 
     // boolean to track view on ...toggle method to pass boolean into and display appropriate 

     mAdapter = new ForecastAdapter(mContext, R.layout.forecast_item, mWdm); 
     mListView = (ListView) findViewById(R.id.forecast_list); 

     mGestureDetector = new GestureDetector(new MyGestureDetector()); 
     mForecastDetails = (RelativeLayout) findViewById(R.id.detail_view); 

     initIndicators(); 

     if (mListView.getVisibility() == View.VISIBLE) { 
      displayForecastListView(); 
     } else { 
      displayForecastDetailsView(); 
     } 
    }
private void displayForecastDetailsView() { 

     mListView.setVisibility(View.GONE); 
     mForecastDetails.setVisibility(View.VISIBLE); 

     mMetric = mWdm.metric; 
     // Set the touch listener for the main view to be our custom gesture listener 
     //mainview.setOnTouchListener(new View.OnTouchListener() { 
     this.setOnTouchListener(new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       if (mGestureDetector.onTouchEvent(event)) { 
        return true; 
       } 
       return false; 
      } 
     }); 

     mFlipper = (ExceptionCheckedViewFlipper) findViewById(R.id.details_flipper); 
     mNext = (ImageButton) findViewById(R.id.next_btn); 
     mNext.setImageBitmap(ACCUWX.applyFilter(
       mContext, BitmapFactory.decodeResource(
         mContext.getApplicationContext().getResources(), R.drawable.right_arrow))); 
     mPrevious = (ImageButton) findViewById(R.id.previous_btn); 
     mPrevious.setImageBitmap(ACCUWX.applyFilter(
       mContext, BitmapFactory.decodeResource(
         mContext.getApplicationContext().getResources(), R.drawable.left_arrow))); 
     mNext.setOnClickListener(this); 
     mPrevious.setOnClickListener(this); 

     populateDetailView(mIndex); 
    }
private void populateDetailView(int index) { 

     mForecastDayIndicator.setForecastIndex(index); 

     Log.i(DEBUG_TAG, "pouplateView"); 
     TextView tv; 
     ImageView icon; 
     ImageView alarmIcon; 
     ImageView viewToggle; 
     ForecastModel fm; 
     boolean isEnabled = false; 
     Boolean isAlarm = false; 

     LinearLayout includes = (LinearLayout)mFlipper.getCurrentView(); 

     viewToggle = (ImageView)includes.findViewById(R.id.view_toggle); 

     viewToggle.setOnClickListener(this); 

     LinearLayout ll = (LinearLayout)includes.findViewById(R.id.detail_container); 
     if(index >= 0 && index lessThan mWdm.forecast.size()) { 
      fm = mWdm.forecast.get(index); 

      // day/date 
      tv = (TextView)includes.findViewById(R.id.day_date); 
      tv.setText(fm.name + " " + fm.date); 

      // hi temp 
      tv = (TextView)includes.findViewById(R.id.hi_temp); 
      tv.setText(fm.high + DEG); 

      // lo temp 
      tv = (TextView)includes.findViewById(R.id.lo_temp); 
      tv.setText(fm.low + DEG); 

      // realfeel hi lo 
      tv = (TextView)includes.findViewById(R.id.realtemp); 
      tv.setText(fm.reelfeelhigh + DEG + "/" + fm.reelfeellow + DEG); 


      // day info 
      tv = (TextView)includes.findViewById(R.id.day); 
      tv.setText(mContext.getApplicationContext().getResources().getString(R.string.day).toUpperCase()); 

      // day icon 
      icon = (ImageView)includes.findViewById(R.id.day_icon); 
      icon.setImageResource(ACCUWX.getDrawableId("icon_" + fm.iconCode)); 

      // day shorttext 
      tv = (TextView)includes.findViewById(R.id.day_shorttext); 
      tv.setText(fm.shortText); 


      // night info 
      tv = (TextView)includes.findViewById(R.id.night); 
      tv.setText(mContext.getApplicationContext().getResources().getString(R.string.night).toUpperCase()); 

      // night icon 
      icon = (ImageView)includes.findViewById(R.id.night_icon); 
      icon.setImageResource(ACCUWX.getDrawableId("icon_" + fm.nightCode)); 

      // night shorttext 
      tv = (TextView)includes.findViewById(R.id.night_shorttext); 
      tv.setText(fm.nightText); 

      // headings 
      // precip 
      tv = (TextView)includes.findViewById(R.id.precip_title); 
      tv.setText(mContext.getApplicationContext().getResources().getString(R.string.precip).toUpperCase()); 

      // day 
      tv = (TextView)includes.findViewById(R.id.day_title); 
      tv.setText(mContext.getApplicationContext().getResources().getString(R.string.day).toUpperCase()); 

      // night 
      tv = (TextView)includes.findViewById(R.id.night_title); 
      tv.setText(mContext.getApplicationContext().getResources().getString(R.string.night).toUpperCase()); 

      // wind 
      tv = (TextView)includes.findViewById(R.id.wind_title); 
      tv.setText(mContext.getApplicationContext().getResources().getString(R.string.wind).toUpperCase()); 


      // Precip fields 
      String rainIceUnits = (mMetric == ACCUWX.Units.METRIC) ? mContext.getApplicationContext().getResources().getString(R.string.mm) : mContext.getApplicationContext().getResources().getString(R.string.inches); 
      String snowUnits = (mMetric == ACCUWX.Units.METRIC) ? mContext.getApplicationContext().getResources().getString(R.string.cm) : mContext.getApplicationContext().getResources().getString(R.string.inches); 
      String speedUnits = (mMetric == ACCUWX.Units.METRIC) ? mContext.getApplicationContext().getResources().getString(R.string.kph) : mContext.getApplicationContext().getResources().getString(R.string.mph); 

      // set text color to white if measurements other than 0, else gray out 

      // rain 
      isAlarm = Alarms.checkRainAlarms(mWdm, fm); 
      alarmIcon = (ImageView) includes.findViewById(R.id.rain_alarm_img); 
      if (isAlarm == true) alarmIcon.setVisibility(View.VISIBLE); 
      else alarmIcon.setVisibility(View.INVISIBLE); 

      isEnabled = !(checkForZero(fm.rain) && checkForZero(fm.nightRain)); 
      tv = (TextView)findViewById(R.id.rain); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.rain); 
      tv = (TextView)includes.findViewById(R.id.day_rain); 
      tv.setText(fm.rain + " " + rainIceUnits); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.nightRain); 
      tv = (TextView)includes.findViewById(R.id.night_rain); 
      tv.setText(fm.nightRain + " " + rainIceUnits); 
      tv.setEnabled(isEnabled); 

      // snow 
      isAlarm = Alarms.checkSnowAlarms(mWdm, fm); 
      alarmIcon = (ImageView) includes.findViewById(R.id.snow_alarm_img); 
      if (isAlarm == true) alarmIcon.setVisibility(View.VISIBLE); 
      else alarmIcon.setVisibility(View.INVISIBLE); 

      isEnabled = !(checkForZero(fm.snow) && checkForZero(fm.nightSnow)); 
      tv = (TextView)findViewById(R.id.snow); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.snow); 
      tv = (TextView)includes.findViewById(R.id.day_snow); 
      tv.setText(fm.snow + " " + snowUnits); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.nightSnow); 
      tv = (TextView)includes.findViewById(R.id.night_snow); 
      tv.setText(fm.nightSnow + " " + snowUnits); 
      tv.setEnabled(isEnabled); 

      // ice 
      isAlarm = Alarms.checkIceAlarms(mWdm, fm); 
      alarmIcon = (ImageView) includes.findViewById(R.id.ice_alarm_img); 
      if (isAlarm == true) alarmIcon.setVisibility(View.VISIBLE); 
      else alarmIcon.setVisibility(View.INVISIBLE); 

      isEnabled = !(checkForZero(fm.ice) && checkForZero(fm.nightIce)); 
      tv = (TextView)findViewById(R.id.ice); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.ice); 
      tv = (TextView)includes.findViewById(R.id.day_ice); 
      tv.setText(fm.ice + " " + rainIceUnits); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.nightIce); 
      tv = (TextView)includes.findViewById(R.id.night_ice); 
      tv.setText(fm.nightIce + " " + rainIceUnits); 
      tv.setEnabled(isEnabled); 

      // Wind Data 
      isAlarm = Alarms.checkGustsAlarms(mWdm, fm); 
      alarmIcon = (ImageView) includes.findViewById(R.id.gusts_alarm_img); 
      if (isAlarm == true) alarmIcon.setVisibility(View.VISIBLE); 
      else alarmIcon.setVisibility(View.INVISIBLE); 

      isEnabled = !(checkForZero(fm.gust) && checkForZero(fm.nightGust)); 
      tv = (TextView)findViewById(R.id.gusts); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.gust); 
      tv = (TextView)includes.findViewById(R.id.day_gusts); 
      tv.setText(fm.gust + " " + speedUnits); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.nightGust); 
      tv = (TextView)includes.findViewById(R.id.night_gusts); 
      tv.setText(fm.nightGust + " " + speedUnits); 
      tv.setEnabled(isEnabled); 

      // speed 
      isAlarm = Alarms.checkSpeedAlarms(mWdm, fm); 
      alarmIcon = (ImageView) includes.findViewById(R.id.speed_alarm_img); 
      if (isAlarm == true) alarmIcon.setVisibility(View.VISIBLE); 
      else alarmIcon.setVisibility(View.INVISIBLE); 

      isEnabled = !(checkForZero(fm.wind) && checkForZero(fm.nightWind)); 
      tv = (TextView)findViewById(R.id.speed); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.wind); 
      tv = (TextView)includes.findViewById(R.id.day_speed); 
      tv.setText(fm.wind + " " + speedUnits); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.nightWind); 
      tv = (TextView)includes.findViewById(R.id.night_speed); 
      tv.setText(fm.nightWind + " " + speedUnits); 
      tv.setEnabled(isEnabled); 

      // tstorms 
      isAlarm = Alarms.checkTstormAlarms(mWdm, fm); 
      alarmIcon = (ImageView) includes.findViewById(R.id.tstorms_alarm_img); 
      if (isAlarm == true) alarmIcon.setVisibility(View.VISIBLE); 
      else alarmIcon.setVisibility(View.INVISIBLE); 

      isEnabled = !(checkForZero(fm.tstorm) && checkForZero(fm.nightTstorm)); 
      tv = (TextView)findViewById(R.id.tstorms); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.tstorm); 
      tv = (TextView)includes.findViewById(R.id.day_tstorms); 
      tv.setText(fm.tstorm + PERCENT); 
      tv.setEnabled(isEnabled); 

      isEnabled = !checkForZero(fm.nightTstorm); 
      tv = (TextView)includes.findViewById(R.id.night_tstorms); 
      tv.setText(fm.nightTstorm + PERCENT); 
      tv.setEnabled(isEnabled); 
     } 

    }
+0

你在測試什麼設備? – FoamyGuy 2011-05-13 19:14:35

+0

Nexus One 2.3.4 – taraloca 2011-05-13 19:27:40

回答

0

嘗試與ViewSwiper實現它,看看它是否有差別。您是否在尋找視圖以用手指移動?或者只是更快地對滑動手勢做出反應?如果前者沒有本地構建的方法來實現這種效果,那麼最好的辦法就是查看家庭屏幕的源代碼,並從那裏獲取您需要的內容以創建自己的自定義視圖。

編輯:如果你可以讓你的findViewById()調用發生在填充方法之外,它可能會加快它的速度。嘗試在創建時抓住這些引用,並將它們留在周圍。這樣,你可以調用.setText()等,而不必做查找然後設置它。我認爲(但不是100%),只要使用ViewFlipper的佈局與setContentView()一起顯示,您應該能夠使用findViewById()獲得對viewFlipper中任何內容的引用,即使它不是當前顯示的孩子。

+0

我已經做了什麼ViewSwiper用手勢做什麼,不做什麼。我相信我的緩慢存在於視圖的所有變量和計算以及正在進入邏輯的調用中。我將在上面發佈我的整個填充方法作爲修正。 – taraloca 2011-05-16 14:31:40

+0

是......已經試過了!我實際上最終把它放回原來的,並發現我的findViewById在填充方法之外對性能沒有任何作用。我感謝你的所有想法,如果你有更多的想法,我歡迎他們:) – taraloca 2011-05-16 17:42:13