1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
| import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Checkable; import android.widget.FrameLayout;
import androidx.annotation.NonNull; import androidx.annotation.Nullable;
import com.sotrun.app.R;
import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class ColorSelectAdapter extends ArrayAdapter<ColorSelectAdapter.Bean> { public ColorSelectAdapter(@NonNull Context context, int resource) { super(context, resource, android.R.id.text1); }
public ColorSelectAdapter(@NonNull Context context, int resource, int textViewResourceId) { this(context, resource, textViewResourceId, new ArrayList<>()); }
public ColorSelectAdapter(@NonNull Context context, int resource, @NonNull Bean[] objects) { this(context, resource, android.R.id.text1, Arrays.asList(objects)); }
public ColorSelectAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull Bean[] objects) { this(context, resource, textViewResourceId, Arrays.asList(objects)); }
public ColorSelectAdapter(@NonNull Context context, int resource, @NonNull List<Bean> objects) { this(context, resource, android.R.id.text1, objects); }
public ColorSelectAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List<Bean> objects) { super(context, resource, textViewResourceId, objects); }
@NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { View view = super.getView(position, convertView, parent); if (view instanceof CheckableProxyView) {
} else { ViewGroup temp = new CheckableProxyView(getContext()); temp.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); temp.addView(view); view = temp;
} CheckableProxyView checkableProxyView = (CheckableProxyView) view; Checkable tvTitle = view.findViewById(android.R.id.text1); checkableProxyView.setCheckNotify(new CheckableProxyView.CheckNotify() { @Override public void setChecked(boolean checked) { tvTitle.setChecked(checked); }
@Override public boolean isChecked() { return tvTitle.isChecked(); }
@Override public void toggle() { tvTitle.toggle(); } }); Bean item = getItem(position); View viewColor = view.findViewById(R.id.view_color); viewColor.setBackgroundColor(getContext().getResources().getColor(item.color)); return view; }
public static class CheckableProxyView extends FrameLayout implements Checkable {
public CheckableProxyView(Context context) { super(context); }
public CheckableProxyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); }
public CheckableProxyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
public CheckableProxyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); }
@Override public void setChecked(boolean checked) { if (checkNotify != null) { checkNotify.setChecked(checked); } }
@Override public boolean isChecked() { if (checkNotify != null) { return checkNotify.isChecked(); } return false; }
@Override public void toggle() { if (checkNotify != null) { checkNotify.toggle(); } }
public void setCheckNotify(CheckNotify checkNotify) { this.checkNotify = checkNotify; }
CheckNotify checkNotify;
interface CheckNotify { void setChecked(boolean checked);
boolean isChecked();
void toggle(); } }
@Override public boolean hasStableIds() { return true; }
@Override public long getItemId(int position) { return position; }
public static class Bean { public int color; public String title;
@Override public String toString() { return title; } } }
|