0
votes

i am drawing a sliderbar when a page swipe occurs in viewpager. So i draw it in ViewPager onPageChange event. Problem is; onDraw method is (sometimes) not called. THE SAME CODE WORKS IN OTHER PROJECT BUT NOT IN THIS ONE which makes it weirder.

My SliderBar View Class;

public class SliderBar extends View {

Paint p = new Paint();
int startX;
int width;
Rect r = new Rect();

public SliderBar(Context c,int x,int width){
    super(c);
    this.startX = x;
    this.width = width;
    this.setWillNotDraw(false);// these didnt fix
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    p.setColor(Color.parseColor("#bbbbbbbb"));
    p.setAntiAlias(true);
    p.setStyle(Paint.Style.FILL);

    r.set(startX,0,startX+width,500);

    canvas.drawRect(r,p);

 }
}

My Call Location

cvp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            int width = cvp.getWidth()/btnCount;
            int currentPos = cvp.getCurrentItem();
            int startX;
            if(currentPos> position)
                startX = (currentPos-1) * width - (cvp.getWidth()-positionOffsetPixels)/btnCount;
            else
                startX = (currentPos-1) * width + positionOffsetPixels/btnCount;

            SliderBar dsb = new SliderBar(getBaseContext(),startX,width);
            ll.removeAllViews();
            ll.addView(dsb,lp);
            ll.invalidate(); // these didnt fix
            mainLayout.invalidate();// these didnt fix
        }

        @Override
        public void onPageSelected(int position) {}

        @Override
        public void onPageScrollStateChanged(int state) {}
    });

  My LinearLayout
        android:layout_width="match_parent" android:orientation="vertical"
        android:background="#77000000"
        android:layout_height="0dp"
        android:layout_weight="1" android:id="@+id/ll"
2
what is ll? what is mainLayout? why are you creating and adding SliderBar each time onPageScrolled is called?pskink
Because every time that slide frame occurs, i am updating my slider view for page slide event. This code works in other project not in this one. mainLayout is main layout, ll is where i add my view. It only updates the view when scroll event is idle or finishedMert Serimer

2 Answers

1
votes

Try add flag FLAG_ACTIVITY_NO_ANIMATION in your intent.

Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);

Issue related commit on google source

0
votes

View#invalidate() may has no effect while Activity doing window animations. So FLAG_ACTIVITY_NO_ANIMATION may resolve your issue. In my case onDraw() method not call during animation frame update.