开发者

Slow to populate viewFlipper with gesture

开发者 https://www.devze.com 2023-03-05 14:05 出处:网络
I have a view that I am populating by using a viewFlipper and a gesture onFling.When I fling the screen to populate next view it is taking 2 to 3 seconds to show the next view.Any ideas what might be

I have a view that I am populating by using a viewFlipper and a gesture onFling. When I fling the screen to populate next view it is taking 2 to 3 seconds to show the next view. Any ideas what might be going on? This is entirely too slow!

Here are some snippets of my code:

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_arro开发者_如何转开发w)));
        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);
        }

    }


Try to implement it with ViewSwiper and see if it makes a difference. Are you looking for the Views to move with your finger? or just respond a second or two quicker to a swipe gesture? If the former there is no natively built way to achieve that effect, best bet is to look at the source code for the homes creen and take what you need from there to create your own custom view.

Edit: If you can make your findViewById() calls happen outside of the populate method it will probably speed it up quite a bit. Try grabbing those references at create time and just keeping them around. That way you can just call .setText() etc instead of having to do the lookup and then set it. I think (but am not 100%) that as long as as the layout with your ViewFlipper has been shown with setContentView() you should be able to get the references to anything in the viewFlipper with findViewById(), even if it isn't the child that is currently shown.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号