Ondraw方法不会调用使用Viewpager& FragmentStateAdapter

时间:2017-12-07 05:24:37

标签: android android-viewpager ondraw fragmentstatepageradapter

我使用自定义日历视图 Viewpager - FragmentStateAdapter - 片段 - Viewgroup -view

当我滑动页面时,我可以获得第一个片段和第二个片段,但是第三个片段没有显示任何内容。所以我调试并且draw()方法不会调用View类。

我使用Invalidate(), setWillNotDraw(false)但它不起作用。

我在日志中看到片段循环运行正常。我不知道问题是什么。

这是我的代码:

FragmentActivity

public class FindClassFragmentActivity extends FragmentActivity {


private static final int COUNT_PAGE = 12;

ViewPager mViewPager;

CalendarFragmentStatePagerAdapter calendarAdapter;

ImageButton previous;
ImageButton next;
TextView presentMonth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.findclass_main_layout);

    mViewPager = (ViewPager) findViewById(R.id.viewpager);
    previous = (ImageButton) findViewById(R.id.previous_month);
    next = (ImageButton) findViewById(R.id.next_month);
    presentMonth = (TextView) findViewById(R.id.present_month);

    calendarAdapter = new CalendarFragmentStatePagerAdapter(getSupportFragmentManager());

    mViewPager.setAdapter(calendarAdapter);
  //  mViewPager.setOffscreenPageLimit(24);

}

public void PrevOrNext(View v) {

    switch (v.getId()) {
        case R.id.previous_month :

            break;
        case R.id.next_month :

            break;
    }
}

}

FragmentStatePagerAdapter

public class CalendarFragmentStatePagerAdapter extends FragmentStatePagerAdapter {

private HashMap<Integer, MonthFragment> monthMap;
private int numOfMonth;

public CalendarFragmentStatePagerAdapter(FragmentManager fm) {
    super(fm);
}


@Override
public int getCount() {
    return 24;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    Log.e("solme6", "instantiateItem : " + position  );
    return super.instantiateItem(container, position);
}

@Override
public Fragment getItem(int position) {
    return MonthFragment.newInstance(position);
}

}

CalendarMonthView

public class CalendarMonthView extends ViewGroup {

private final int mScreenWidth;
private final int mWidthDate;

private int mDateOfWeek; // 1일의 요일


private int mDefaultTextSize = 40;

private int mTextColor = Color.BLUE;


public static String[] DAY_OF_WEEK = null;

public CalendarMonthView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mScreenWidth = getResources().getDisplayMetrics().widthPixels;
    mWidthDate = mScreenWidth / 7;
    DAY_OF_WEEK = getResources().getStringArray(R.array.day_of_week);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int count = getChildCount();

    Log.e("solme",Integer.toString(count));
    int maxHeight = 0;
    int childState = 0;
    int childViewFullWidth = 0;
    int childViewFullheight = 0;

    for(int i=0; i < count; i++) {
        final View child = getChildAt(i);
        getChildAt(i).setWillNotDraw(false);

        if (child.getVisibility() == GONE) {
            continue;
        }
        childViewFullWidth = child.getMeasuredWidth();
        childViewFullheight = child.getMeasuredHeight();
        measureChild(child, widthMeasureSpec, heightMeasureSpec); // 자식의 크기 측정.


        childState = combineMeasuredStates(childState, child.getMeasuredState());
    }

    setMeasuredDimension(childViewFullWidth, childViewFullheight);
    LayoutParams params = getLayoutParams();
    params.height = getMeasuredHeight();
    params.height = maxHeight;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

    final int count = getChildCount();

    int curLeft = 0; // 왼쪽 스타트 지점 값.
    int curTop = 0;

    int eachCellWidth, eachCellHeight;

    int curDayHeight;


    final int childWidth = this.getMeasuredWidth(); // 1440
    final int childHeight = this.getMeasuredHeight(); //2355


    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);

        if (child.getVisibility() == GONE)
            return;

        child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST));
        eachCellWidth = mWidthDate;
        curDayHeight = 100;
        eachCellHeight = (getMeasuredHeight() - curDayHeight) / 6;

        if (i < 7) {


            child.layout(curLeft, curTop, curLeft + eachCellWidth, curTop + curDayHeight);

            curLeft += eachCellWidth;

            if (curLeft + eachCellWidth >= childWidth) {
                curLeft = 0;
                curTop += curDayHeight;
            }

        } else {

            if (i == 7) {
                curLeft = (mDateOfWeek - 1) * eachCellWidth;
            }
            child.layout(curLeft, curTop, curLeft + eachCellWidth, curTop + eachCellHeight);

            curLeft += eachCellWidth;

            if (curLeft + eachCellWidth >= childWidth) {
                curLeft = 0;
                curTop += eachCellHeight;
            }

        }

    }
}

@Override
protected void onDraw(Canvas canvas) {
    Log.e("solme6", "CalendarMonthView : onDraw");
    super.onDraw(canvas);
}

@Override
public void draw(Canvas canvas) {
    Log.e("solme6", "CalendarMonthView : draw");
    super.draw(canvas);
}

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

private Paint makePaint(int color) {
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    p.setColor(color);
    p.setTextSize(mDefaultTextSize);
    return p;
}

public void initCalendar(int dayOfWeek, int maxDateOfMonth) {
    mDateOfWeek = dayOfWeek;
}

}

CalendarcellView

public class CalendarCellView extends View {

Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint mPaintTextWhite = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint mPaintBackground = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint mPaintBackgroundToday = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint mPaintBackgroundEvent = new Paint(Paint.ANTI_ALIAS_FLAG);
private int dayOfWeek = -1;
private boolean isStaticText = false;
private long millis;
private Rect rect;
private boolean isTouchMode;
private int dp11;
private int dp16;
private boolean hasEvent = false;
private int[] mColorEvents;
private final float RADIUS = 100f;

public CalendarCellView(Context context) {
    super(context);
    initialize(context);
    Log.e("solme6", "CalendarCellView : constructor");
}

public CalendarCellView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context);

}

private void initialize(Context context) {

    dp11 = (int) dp2px(getContext(), 11);
    dp16 = (int) dp2px(getContext(), 16);

    mPaint.setColor(Color.BLACK);
    mPaint.setTextSize(dp11);
    mPaint.setTextAlign(Paint.Align.CENTER);
    mPaintTextWhite.setColor(Color.WHITE);
    mPaintTextWhite.setTextSize(dp11);
    mPaintTextWhite.setTextAlign(Paint.Align.CENTER);
    mPaintBackground.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
    mPaintBackgroundToday.setColor(ContextCompat.getColor(getContext(), R.color.today));
    mPaintBackgroundEvent.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
    setPadding(30, 0, 30, 0);
}

public float dp2px(Context context, float dp) {
    return dp * context.getResources().getDisplayMetrics().density;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    Log.e("solme6","CalendarCellView : onMeasure");
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    Log.e("solme6", "CalendarCellView : onLayout");
    super.onLayout(changed, left, top, right, bottom);
}

@Override
protected void dispatchDraw(Canvas canvas) {
    Log.e("solme6", "CalendarCellView : dispatchDraw");
}

@Override
public void draw(Canvas canvas) {
    Log.e("solme6", "CalendarCellView : draw");
    super.draw(canvas);
}

@Override
protected void onDraw(Canvas canvas) {
    Log.e("solme6", "CalendarCellView : onDraw");
    super.onDraw(canvas);
    int xPos = (canvas.getWidth() / 2);
    int yPos = (int) ((canvas.getHeight() / 2) - ((mPaint.descent() + mPaint.ascent()) / 2));
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(millis);
    set

    CalendarMonthView calendarView = (CalendarMonthView) getParent();
    if (calendarView.getParent() instanceof ViewPager) {
        ViewGroup parent = (ViewPager) calendarView.getParent();
        CalendarCellView tagView = (CalendarCellView) parent.getTag();

        if (!isStaticText && tagView != null && tagView.getTag() != null && tagView.getTag() instanceof Long) {
            long millis = (long) tagView.getTag();
            if (isSameDay(millis, this.millis)) {
                //   RectF rectF = new RectF(xPos - dp16, getHeight() / 2 - dp16, xPos + dp16, getHeight() / 2 + dp16);
                //   canvas.drawRoundRect(rectF, RADIUS, RADIUS, mPaintBackground);
            }
        }
    }

    if (!isStaticText && isToday(millis)) {
        RectF rectF = new RectF(xPos - dp16, getHeight() / 2 - dp16, xPos + dp16, getHeight() / 2 + dp16);
        canvas.drawRoundRect(rectF, RADIUS, RADIUS, mPaintBackgroundToday);
    }

    if (isStaticText) {
        // 요일 표시
        canvas.drawText(CalendarMonthView.DAY_OF_WEEK[dayOfWeek], xPos, yPos, mPaint);
    } else {
        // 날짜 표시
        if (isToday(millis)) {
            canvas.drawText(calendar.get(Calendar.DATE) + "", xPos, yPos, mPaintTextWhite);
        } else {
            canvas.drawText(calendar.get(Calendar.DATE) + "", xPos, yPos, mPaint);
        }
    }

    if (hasEvent) {
        mPaintBackgroundEvent.setColor(getResources().getColor(mColorEvents[0]));
        RectF rectF = new RectF(xPos - 5, getHeight() / 2 + 20, xPos + 5, getHeight() / 2 + 30);
        canvas.drawRoundRect(rectF, RADIUS, RADIUS, mPaintBackgroundToday);
    }
}

private boolean isToday(long millis) {
    Calendar cal1 = Calendar.getInstance();
    return isSameDay(cal1.getTimeInMillis(), millis);

}

public void setDate(long millis) {
    this.millis = millis;
    setTag(millis);
}

public void setDayOfWeek(int dayOfWeek) {
    this.dayOfWeek = dayOfWeek;
    isStaticText = true;
}

public void setEvent(int... resid) {
    hasEvent = true;
    mColorEvents = resid;
}

public boolean isSameDay(long millis1, long millis2) {
    Calendar cal1 = Calendar.getInstance();
    Calendar cal2 = Calendar.getInstance();
    cal1.setTimeInMillis(millis1);
    cal2.setTimeInMillis(millis2);
    Log.d("hatti.calendar", "" + cal1.get(Calendar.YEAR) + "" + cal1.get(Calendar.MONTH) + "" + cal1.get(Calendar.DATE) + " VS " +
            cal2.get(Calendar.YEAR) + "" + cal2.get(Calendar.MONTH) + "" + cal2.get(Calendar.DATE));
    return (cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DATE) == cal2.get(Calendar.DATE));
}

public boolean isStaticText() {
    return isStaticText;
}
}

我是否应该在适配器上添加Fragment?

1 个答案:

答案 0 :(得分:0)

寻呼机适配器中的

    @Override
    public int getItemPosition(Object object) {

            return POSITION_NONE;

    }