我有一个GridView
,它显示了多种颜色。我希望选择其中的一些,但是我不知道如何实现这些颜色。
这是我到目前为止所拥有的:
GridView
被绘制在名为DialogFragment
的{{1}}中。这需要DialogFragmentSelectColour
中的ArrayList<ChartColours>
才能显示在ChartColours
(GridView
)和mColours
中的ArrayList<ChartColours>
中,它们已经被选中({ {1}}):
ChartColours
mSelectedColours
只是一个类,其中包含颜色的名称(作为字符串)及其rgb值(作为整数)。
public class DialogFragmentSelectColour extends DialogFragment {
private ArrayList<ChartColour> mColours, mSelectedColours;
private AdapterChartColour mColourAdapter;
private Context mContext;
private GridView mGVColours;
private OnColoursSelected mCallback;
static public DialogFragmentSelectColour newInstance(Context context, ArrayList<ChartColour> colours, ArrayList<ChartColour> selectedColours, OnColoursSelected callback) {
DialogFragmentSelectColour f = new DialogFragmentSelectColour();
f.setRequiredData(context, colours, selectedColours, callback);
return f;
}
public void setRequiredData(Context context, ArrayList<ChartColour> colours, ArrayList<ChartColour> selectedColours, OnColoursSelected callback) {
this.mContext = context;
this.mColours = colours;
this.mCallback = callback;
// Selected colours is initially null if no colours have yet been selected
if(selectedColours == null) {
this.mSelectedColours = new ArrayList<>();
}else {
this.mSelectedColours = selectedColours;
}
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// Inflate layout
return inflater.inflate(R.layout.dialog_fragment_select_colour, container, false);
}
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mGVColours = view.findViewById(R.id.gv_colours);
drawColours();
selectColours();
}
private void drawColours() {
// Now set up AdapterChartColour to display colours in grid
mColourAdapter =
new AdapterChartColour(mContext, mColours,
mSelectedColours);
mGVColours.setAdapter(mColourAdapter);
}
private void selectColours() {
// Highlight selected colours and unhighlight all others
// Loop over all colours; if colour is in "selected" array, select it; otherwise, unselect it
for(int ii=0; ii < mGVColours.getChildCount(); ii++) {
// I've tried both of the following, but neither returned a ChartColourCell:
// ChartColourCell cell = (ChartColourCell) mGVColours.getChildAt(ii);
// ChartColourCell cell = (ChartColourCell) mGVColours.getItemAtPosition(ii);
}
}
是一个显示ChartColour
的类,如下所示(我省略了一些不相关的细节):
ChartColourCell
ChartColour
如下:
public class ChartColourCell extends View {
private boolean mSelected;
private ChartColour mColour; // Colour to be shown
private Paint mPaint; // Paint variable to use
public ChartColourCell(Context context, AttributeSet attrs) {
super(context, attrs);
mSelected = false;
mPaint = new Paint();
mPaint.setStrokeWidth(10f);
}
public ChartColour getColour() { return this.mColour; }
// Set the colour to be shown in the circle
public void setColour(ChartColour colour) {
this.mColour = colour;
invalidate();
}
// Change whether the colour is selected or not
public void setSelected(boolean selected) {
this.mSelected = selected;
invalidate();
}
// Draw the circle
// If selected, overlay the circle with a tick mark
protected void onDraw(Canvas canvas) {
if(this.mColour == null) return;
// Get canvas size, initialise Paint, get radius & centre
... I've left out the details of this to save space ...
// Draw circle
canvas.drawCircle(cx, cy, radius, mPaint);
// If the colour is selected, overlay a tick
Drawable drawable = getResources().getDrawable(R.drawable.icon_tick);
drawable.setBounds(0, 0, getWidth(), getHeight());
drawable.draw(canvas);
}
}
布局AdapterChartColour
仅包含:
public class AdapterChartColour extends ArrayAdapter<ChartColour> {
private Context mContext;
private ArrayList<ChartColour> mColourArray, mSelectedArray;
public AdapterChartColour(Context context, ArrayList<ChartColour> colourArray, ArrayList<ChartColour> selectedArray) {
super(context, layout_id, colourArray);
this.mContext = context;
this.mColourArray = colourArray;
this.mSelectedArray = selectedArray;
}
private static class ViewHolder {
private ChartColourCell colourCell;
private TextView colourName, colourKey;
}
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
ViewHolder mViewHolder;
if(convertView == null) {
mViewHolder = new ViewHolder();
LayoutInflater layoutInflater
= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (layoutInflater != null) {
// Inflate appropriate layout
convertView = layoutInflater.inflate(R.layout.adapter_chart_colour_cell, parent, false);
if (convertView != null) {
// Find views
mViewHolder.colourCell = convertView.findViewById(R.id.chart_colour_cell);
mViewHolder.colourCell.setSelected(false);
convertView.setTag(mViewHolder);
ChartColour cellColour = mColourArray.get(position);
mViewHolder.colourCell.setColour(cellColour); // Causes cell to draw
// Now determine whether this colour is selected
if (mSelectedArray.contains(cellColour)) {
mViewHolder.colourCell.setSelected(true);
} else {
mViewHolder.colourCell.setSelected(false);
}
}
}
}
return convertView;
}
public int getCount() { return mColourArray.size();}
public ChartColour getItem(int position) { return mColourArray.get(position);}
}
希望这是有意义的一切,但是如果需要,我可以添加更多内容。因此,如何访问adapter_chart_colour_cell
中的单个<mypath.appname.models.ChartColourCell
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/grid_cell_column_width"
android:layout_height="@dimen/grid_cell_column_width"
android:id="@+id/chart_colour_cell"/>
,以便可以选择和取消选择它们?