我有一个带有GridView女巫适配器的Alertdialog,它加载了很多元素。 问题是该对话框需要ca。 2-5秒显示。 如何加速呢? 我在适配器的视图方法中考虑了多线程[…] 但是怎么办呢?我无法将方法主体放入可运行的线程中,因为那时我无法返回该项目(或?)。 我也在网上搜索了有关该内容的信息,但找不到一个对我有用的示例。
希望你能帮助我!
package com.ludevstudio.colorpicker;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import java.util.ArrayList;
public class ColorpickerDialog extends AlertDialog {
GridView grid;
Colorpicker picker;
PaletteStyle style;
private OnColorChoosedListener listener;
public ColorpickerDialog(Context context, Colorpicker picker) {
super(context);
this.picker = picker;
style = PaletteStyle.DEFAULT;
initUI();
}
public ColorpickerDialog(Context context, Colorpicker picker, PaletteStyle style) {
super(context);
this.picker = picker;
this.style = style;
initUI();
}
private void initUI() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_colorpicker, null);
grid = (GridView) ((View) layout).findViewById(R.id.dialog_grid);
grid.setAdapter(new GridAdapter(getContext(), picker));
setButton(AlertDialog.BUTTON_NEGATIVE, getContext().getResources().getString(R.string.dialog_cancel), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onCanceled();
}
});
setButton(AlertDialog.BUTTON_POSITIVE, getContext().getResources().getString(R.string.dialog_apply), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listener.onContiniued(picker.getSelectedColor());
}
});
setButton(AlertDialog.BUTTON_NEUTRAL, "COSTUM", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int red = Color.red(picker.getSelectedColor());
int green = Color.green(picker.getSelectedColor());
int blue = Color.blue(picker.getSelectedColor());
CostumColorDialog cscd =new CostumColorDialog(getContext(), red, green, blue);
cscd.show();
}
});
setView(layout);
}
private class GridAdapter extends BaseAdapter {
Context c;
Colorpicker picker;
public GridAdapter(Context c, Colorpicker picker) {
this.c = c;
this.picker = picker;
}
@Override
public int getCount() {
return picker.getColors().size();
}
@Override
public Object getItem(int position) {
return picker.getColors().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final PaletteItem item = new PaletteItem(c);
switch (style) {
case ROUND:
item.setBackgroundResource(R.drawable.palette_item_round);
break;
case DEFAULT:
item.setBackgroundResource(R.drawable.palette_item_rect);
break;
case ROUNDRECT:
item.setBackgroundResource(R.drawable.palette_item_roundrect);
break;
}
item.getBackground().setColorFilter(picker.getColors().get(position), PorterDuff.Mode.SRC_OVER);
item.setTextColor(Color.WHITE);
if (picker.getColors().indexOf(picker.getSelectedColor()) == position) {
item.setChecked(true);
}
item.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int index = picker.getColors().indexOf(picker.getSelectedColor());
PaletteItem i = (PaletteItem) grid.getChildAt(picker.getColors().indexOf(picker.getSelectedColor()));
i.setChecked(false);
picker.setSelectedColor(picker.colors.get(position));
item.setChecked(true);
listener.onColorChoosed(picker.getColors().get(position));
}
});
return item;
}
}
public void setListener(OnColorChoosedListener listener) {
this.listener = listener;
}
public enum PaletteStyle {
DEFAULT,
ROUND,
ROUNDRECT;
}
}