I need to create a layout which consists of a list of rectangles of different width and color. So I decided to create a custom view, which draw a rectangle and create a subclass of CursorAdapter
, which will fill the ListView
with my rectangles, setting their size and color with the data from Cursor.
I tried to create view in newView()
method and add it to root, but it's not working.
Do I need to create some layout and inflate it in newView()
? And how should I set the size and color of rectangles?
I read this answer and it has nothing in common with my question. Please read the question before you mark it as answered.
UPDATE
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//View is created programmatically
//Set color depending on context type.
View view = LayoutInflater.from(context).inflate(R.layout.record_item_layout, parent, false);
Log.i(LOG_TAG, "(newView)new view have been created");
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log.i(LOG_TAG, "(bindView) binding to out view");
int colorId = cursor.getInt(cursor.getColumnIndex(TrackerContract.UserRecordsEntry.COLUMN_CONTEXT_ID));
RecordView recordView = (RecordView) view;
//These layout params we need to set height to MAX, and width depending on record duration.
recordView.setLayoutParams(new ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT));
recordView.setColorId(colorId);
recordView.invalidate();
//do something?
}
<?xml version="1.0" encoding="utf-8"?>
<mypackage.RecordView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/record_view"
custom:colorId="5"/>