I have a lag problem in expandablelistview which includes a checkbox in a viewpager. I even use viewholder pattern(i dont use for groups because they can be at most 4 so not needed. I use it on childs) but still something is causing lag when scroll or whatever i do in that page is a little bit laggy(no other pages causes lag, I have only 4 page in my viewpager.). Here is my code;
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.task_group,parent,false);
}
LinearLayout bg = (LinearLayout) convertView.findViewById(R.id.taskGroupBg);
TextView tv = (TextView) convertView.findViewById(R.id.closenessTv);
CharSequence[] texts = context.getResources().getStringArray(R.array.closeness_texts);
long id = this.getGroupId(groupPosition);
if(id == -1){
bg.setBackground(setGroupBackgroundColor(Color.parseColor(groupBgColors[0])));
tv.setText(texts[0]);
}else if(id == 0){
bg.setBackground(setGroupBackgroundColor(Color.parseColor(groupBgColors[1])));
tv.setText(texts[1]);
}else if(id == 1){
bg.setBackground(setGroupBackgroundColor(Color.parseColor(groupBgColors[2])));
tv.setText(texts[2]);
}else {
bg.setBackground(setGroupBackgroundColor(Color.parseColor(groupBgColors[3])));
tv.setText(texts[3]);
}
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = infalInflater.inflate(R.layout.task_row,parent,false);
// Create a ViewHolder and store references to the two children views
holder.bg = (LinearLayout) convertView.findViewById(R.id.taskRowBg);
holder.text = (TextView) convertView.findViewById(R.id.taskTv);
holder.calendar =(LinearLayout) convertView.findViewById(R.id.calendar);
holder.cb = (CheckBox) convertView.findViewById(R.id.taskCb);
holder.dayRemain = (TextView) convertView.findViewById(R.id.dayRemainTv);
//holder.cb.setEnabled(false);
// The tag can be any Object, this just happens to be the ViewHolder
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final Task t = this.childData.get((int) getGroupId(groupPosition)).get(childPosition);
lis = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checked.get((int)getGroupId(groupPosition))[childPosition] = isChecked;
db = new TaskDb(context);
if(isChecked)
t.setStatus(DefaultValues.TASK_STATUS_COMPLETED);
else
t.setStatus(DefaultValues.TASK_STATUS_NOTDONE);
db.updateTask(t);
}
};
String text = t.getText();
long id = this.getGroupId(groupPosition);
if(id == -1)
holder.bg.setBackgroundColor(Color.parseColor(rowBgColors[0]));
else if (id == 0)
holder.bg.setBackgroundColor(Color.parseColor(rowBgColors[1]));
else if (id == 1)
holder.bg.setBackgroundColor(Color.parseColor(rowBgColors[2]));
else
holder.bg.setBackgroundColor(Color.parseColor(rowBgColors[3]));
holder.text.setText(text);
// holder.dayRemain.setText(findHourDif(System.currentTimeMillis(),t.getRemindDate()));
int status = t.getStatus();
long curTime = System.currentTimeMillis();
if(t.getRemindDate() < curTime && status == DefaultValues.TASK_STATUS_NOTDONE)
t.setStatus(DefaultValues.TASK_STATUS_OVER_NOTDONE);
else if(t.getRemindDate() < curTime && status == DefaultValues.TASK_STATUS_COMPLETED)
t.setStatus(DefaultValues.TASK_STATUS_OVER_DONE);
status = t.getStatus();
//holder.cb.setTag(childPosition);
holder.cb.setOnCheckedChangeListener(null);
holder.cb.setChecked(checked.get((int)getGroupId(groupPosition))[childPosition]);
holder.cb.setOnCheckedChangeListener(lis);
if(status == DefaultValues.TASK_STATUS_OVER_DONE || status == DefaultValues.TASK_STATUS_OVER_NOTDONE) {
holder.text.setPaintFlags(holder.text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else {
holder.text.setPaintFlags(holder.text.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
// holder.text.invalidate();
}
DateConverter dc = new DateConverter(context,t.getRemindDate());
String[] dateStr = dc.getTaskDateFormat();
holder.calendar.removeAllViews();
holder.calendar.addView(new DrawCalendar(context, dateStr));//drawCalendar(dateStr[0],dateStr[1]));
holder.calendar.invalidate();
return convertView;
//My Calendar drawer probably has no effect but still i will put
public class DrawCalendar extends View {
private RectF small = new RectF(),big = new RectF();
private Paint p = new Paint();
private Paint textP = new Paint();
private Paint.FontMetrics fm = new Paint.FontMetrics();
private String[] dates;
private float radiusSize = 20f;
public DrawCalendar(Context context,String[] dates) {
super(context);
this.dates = dates;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = canvas.getWidth();
int height = canvas.getHeight();
int smallH =height/2;
small.set(0, 0, width, smallH);
p.setColor(Color.parseColor("#6C4D4D"));
p.setStyle(Paint.Style.FILL);
p.setAntiAlias(true);
drawAsymmetricRoundRect(canvas, small, new float[]{radiusSize, radiusSize, 0, 0}, p);
/*p.setStrokeWidth(2);
p.setStyle(Paint.Style.STROKE);
p.setColor(Color.parseColor("#444444"));
drawAsymmetricRoundRect(canvas, small, new float[]{10, 10, 0, 0}, p);*/
textP.setTextSize(60);
textP.setColor(Color.parseColor("#dddddd"));
textP.setTextAlign(Paint.Align.CENTER);
textP.getFontMetrics(fm);
int textPos = smallH / 2 + smallH / 4;
canvas.drawText(dates[1], width / 2, textPos, textP);
big.set(0, smallH, width, height);
p.setStyle(Paint.Style.FILL);
p.setColor(Color.parseColor("#fafafa"));
drawAsymmetricRoundRect(canvas,big,new float[]{0,0,radiusSize,radiusSize},p);
/*p.setStyle(Paint.Style.STROKE);
p.setColor(Color.parseColor("#444444"));
p.setStrokeWidth(1);
drawAsymmetricRoundRect(canvas,big,new float[]{0,0,10,10},p);*/
textP.setTextSize(45);
textP.setColor(Color.parseColor("#444444"));
textP.setTextAlign(Paint.Align.CENTER);
textP.getFontMetrics(fm);
canvas.drawText(dates[0], width / 2, smallH + textPos, textP);
}
private void drawAsymmetricRoundRect(Canvas canvas, RectF rectF, float[] radii, Paint paint) {
float topLeftX = rectF.left + radii[0];
float topLeftY = rectF.top + radii[0];
float topRightX = rectF.right - radii[1];
float topRightY = rectF.top + radii[1];
float bottomRightX = rectF.right - radii[2];
float bottomRightY = rectF.bottom - radii[2];
float bottomLeftY = rectF.bottom - radii[3];
float bottomLeftX = rectF.left + radii[3];
/*float[] radii is a float array (length = 4), that stores sizes of radii of your corners
(clockwise, starting from top-left corner => {topLeft, topRight, bottomRight, bottomLeft}).*/
RectF topLeftCorner = new RectF(rectF.left, rectF.top, topLeftX + radii[0], topLeftY + radii[0]);
RectF topRightCorner = new RectF(topRightX - radii[1], rectF.top, rectF.right, topRightY + radii[1]);
RectF bottomRightCorner = new RectF(bottomRightX - radii[2], bottomRightY - radii[2], rectF.right, rectF.bottom);
RectF bottomLeftCorner = new RectF(rectF.left, bottomLeftY - radii[3], bottomLeftX + radii[3], rectF.bottom);
canvas.drawArc(topLeftCorner, 180, 90, true, paint);
canvas.drawArc(topRightCorner, 270, 90, true, paint);
canvas.drawArc(bottomRightCorner, 0, 90, true, paint);
canvas.drawArc(bottomLeftCorner, 90, 90, true, paint);
canvas.drawRect(topLeftX, rectF.top, topRightX, bottomLeftY < bottomRightY ? bottomLeftY : bottomRightY, paint); //top rect
canvas.drawRect(topLeftX > bottomLeftX ? topLeftX : bottomLeftX, topRightY, rectF.right, bottomRightY, paint); //right rect
canvas.drawRect(bottomLeftX, topLeftY > topRightY ? topLeftY : topRightY, bottomRightX, rectF.bottom, paint); //bottom rect
canvas.drawRect(rectF.left, topLeftY, bottomRightX < topRightX ? bottomRightX : topRightX, bottomLeftY, paint); //left rect
}