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"