Reputation: 2317
I have a custom view with a corresponding custom layout xml which I want to display in a list view (so I can have multiple of these in a list).
As a test I did this in onCreate():
cstmView s = findViewById(R.id.card);
s.setImg(R.drawable.ac);
with:
<views.cstmView
android:layout_width="395dp"
android:layout_height="72dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
So I know that the view is being initialized and drawn from cstmView.java
However, I want to create a list view so I can have multiple cstmView
in a scrollable list. This is what I did ( as a test I just put one cstmView in the list view):
Listv = new ArrayList<>();
//adding just one cstmview
Listv.add(new cstmView(this.getApplicationContext()));
//this is my custom adapter to accept cstm layour rathern than a textview
cstmAdapter adapter = new cstmAdapter(this, R.layout.cstm_view, ListV);
ListView cstmListView = findViewById(R.id.cstmListView);
cstmListView(adapter);
Although this is putting the cstmView in list, it is not running the overridden draw
function in cstmView
.
I think the problem is with new cstmView(this.getApplicationContext())
because the same problem arises with or without the list view. does the new
keyword not run draw()
?
this is the method that should be calling draw in cstmView.java
public void init(Context context){
System.out.println("INIT");
// To instantiate an XML layout file from the already configured R.layout
LayoutInflater.from(context).inflate(R.layout.cstm_view, this);
// Get elements in layout
text = (TextView)findViewById(R.id.title);
back = (ImageView)findViewById(R.id.img);
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
// to convert
cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
// to draw the above
setWillNotDraw(false);
}
and then the on draw function creates some bitmaps and so on...
@Override
public void draw(Canvas canvas) {
System.out.println("DRAW");
// create a bitmap with this context height and width
Bitmap offscreenBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas offscreenCanvas = new Canvas(offscreenBitmap);
super.draw(offscreenCanvas);
if (maskBitmap == null) {
maskBitmap = createMask(getWidth(), getHeight());
}
offscreenCanvas.drawBitmap(maskBitmap, 0f, 0f, maskPaint);
canvas.drawBitmap(offscreenBitmap, 0f, 0f, paint);
}
private Bitmap createMask(int width, int height) {
System.out.println("CREATE MASK");
Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
Canvas canvas = new Canvas(mask);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, width, height, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRoundRect(new RectF(0, 0, width, height), cornerRadius, cornerRadius, paint);
return mask;
}
calling invalidate()
in constructor solves the problem. However, I still am not seeing the renders on-screen that draw
is supposedly doing. Is there something wrong with the arrayAdatper
? :
@Override
public View getView(int position, View convertView, ViewGroup parent){
View rowView = convertView;
//if data already exists in list reuse the data rather than re inflate from scratch
if(rowView == null) {
LayoutInflater inflater;
inflater = LayoutInflater.from(context);
rowView = inflater.inflate(resource, parent, false);
}
return rowView;
}
Upvotes: 0
Views: 48