Java ArrayAdapter的getView()速度较慢
当用户选择一个Java ArrayAdapter的getView()速度较慢,java,android,listview,android-arrayadapter,Java,Android,Listview,Android Arrayadapter,当用户选择一个ListViewItem时,我正在更改该行的背景图像。这似乎发生得很慢。我不知道为什么 McClickListener listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListViewItem
时,我正在更改该行的背景图像。这似乎发生得很慢。我不知道为什么
McClickListener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//quotesAdapter.setSelectedPosition(position);
setupDetailView(position);
setupChartView(position);
setupARView(position);
emptyView.setVisibility(View.INVISIBLE);
ViewGroup vg = (ViewGroup)v;
TextView nameText = (TextView) vg.findViewById(R.id.nameText);
TextView priceText = (TextView) vg.findViewById(R.id.priceText);
TextView changeText = (TextView) vg.findViewById(R.id.changeText);
//change the old row back to normal
if(oldView != null){
oldView.setBackgroundResource(R.drawable.stocks_gradient);
nameText.setTextAppearance(getApplicationContext(), R.style.BlueText);
priceText.setTextAppearance(getApplicationContext(), R.style.BlueText);
changeText.setTextAppearance(getApplicationContext(), R.style.BlueText);
}
//change the selected row
v.setBackgroundResource(R.drawable.stocks_selected_gradient);
nameText.setTextColor(Color.WHITE);
priceText.setTextColor(Color.WHITE);
changeText.setTextColor(Color.WHITE);
//keep a reference to the old row, for the next time user clicks
oldView = v;
}
});
}
listView.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(适配器视图a、视图v、内部位置、长id){
//引用自适应设置所选位置(位置);
设置详细视图(位置);
设置图表视图(位置);
设置视图(位置);
emptyView.setVisibility(View.INVISIBLE);
视图组vg=(视图组)v;
TextView nameText=(TextView)vg.findViewById(R.id.nameText);
TextView priceText=(TextView)vg.findViewById(R.id.priceText);
TextView changeText=(TextView)vg.findViewById(R.id.changeText);
//将旧行更改回正常行
如果(旧视图!=null){
oldView.setBackgroundResource(R.drawable.stocks_gradient);
nameText.setTextAppearance(getApplicationContext(),R.style.BlueText);
priceText.setTextAppearance(getApplicationContext(),R.style.BlueText);
changeText.settext外观(getApplicationContext(),R.style.BlueText);
}
//更改所选行
v、 退步地资源(R.可提取.库存\选定\梯度);
nameText.setTextColor(Color.WHITE);
priceText.setTextColor(Color.WHITE);
changeText.setTextColor(Color.WHITE);
//保留对旧行的引用,以便下次用户单击时使用
oldView=v;
}
});
}
原始代码:
private class QuoteAdapter extends ArrayAdapter<Quote> {
private ArrayList<Quote> items;
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public void setSelectedPosition(int pos) {
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
}
TextView nameText = (TextView) v.findViewById(R.id.nameText);
TextView priceText = (TextView) v.findViewById(R.id.priceText);
TextView changeText = (TextView) v.findViewById(R.id.changeText);
// change the row color based on selected state
if (selectedPos == position) {
v.setBackgroundResource(R.drawable.stocks_selected_gradient);
nameText.setTextColor(Color.WHITE);
priceText.setTextColor(Color.WHITE);
changeText.setTextColor(Color.WHITE);
} else {
v.setBackgroundResource(R.drawable.stocks_gradient);
nameText.setTextAppearance(getApplicationContext(), R.style.BlueText);
priceText.setTextAppearance(getApplicationContext(), R.style.BlueText);
changeText.setTextAppearance(getApplicationContext(), R.style.BlueText);
}
Quote q = items.get(position);
if (q != null) {
if (nameText != null) {
nameText.setText(q.getSymbol());
}
if (priceText != null) {
priceText.setText(q.getLastTradePriceOnly());
}
if (changeText != null) {
try {
float floatedChange = Float.valueOf(q.getChange());
if (floatedChange < 0) {
if (selectedPos != position)
changeText.setTextAppearance(getApplicationContext(), R.style.RedText); // red
} else {
if (selectedPos != position)
changeText.setTextAppearance(getApplicationContext(), R.style.GreenText); // green
}
} catch (NumberFormatException e) {
System.out.println("not a number");
} catch (NullPointerException e) {
System.out.println("null number");
}
changeText.setText(q.getChange() + " (" + q.getPercentChange() + ")");
}
}
return v;
}
}
私有类QuoteAdapter扩展了ArrayAdapter{
私有ArrayList项;
//用于在ListView中保留选定位置
private int selectedPos=-1;//未选择的初始值
公共QuoteAdapter(上下文上下文、int-textViewResourceId、ArrayList项){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
公共无效设置选定位置(内部位置){
selectedPos=pos;
//通知视图此更改
notifyDataSetChanged();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.layout.mainrow,空);
}
TextView nameText=(TextView)v.findViewById(R.id.nameText);
TextView priceText=(TextView)v.findViewById(R.id.priceText);
TextView changeText=(TextView)v.findViewById(R.id.changeText);
//根据选定状态更改行颜色
如果(selectedPos==位置){
v、 退步地资源(R.可提取.库存\选定\梯度);
nameText.setTextColor(Color.WHITE);
priceText.setTextColor(Color.WHITE);
changeText.setTextColor(Color.WHITE);
}否则{
v、 退耕地资源(R.可开采、存量和梯度);
nameText.setTextAppearance(getApplicationContext(),R.style.BlueText);
priceText.setTextAppearance(getApplicationContext(),R.style.BlueText);
changeText.settext外观(getApplicationContext(),R.style.BlueText);
}
报价q=项目。获取(位置);
如果(q!=null){
如果(nameText!=null){
nameText.setText(q.getSymbol());
}
if(priceText!=null){
priceText.setText(q.getLastTradePriceOnly());
}
if(changeText!=null){
试一试{
float-floatedChange=float.valueOf(q.getChange());
如果(浮动变化<0){
如果(已选择位置!=位置)
changeText.setTextAppearance(getApplicationContext(),R.style.RedText);//红色
}否则{
如果(已选择位置!=位置)
changeText.setTextAppearance(getApplicationContext(),R.style.GreenText);//绿色
}
}捕获(数字格式){
System.out.println(“不是数字”);
}捕获(NullPointerException e){
System.out.println(“空编号”);
}
changeText.setText(q.getChange()+”(“+q.getPercentChange()+”);
}
}
返回v;
}
}
更新:具有视窗夹模式的适配器
private class QuoteAdapter extends ArrayAdapter<Quote> {
private ArrayList<Quote> items;
// used to keep selected position in ListView
private int selectedPos = -1; // init value for not-selected
public QuoteAdapter(Context context, int textViewResourceId, ArrayList<Quote> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public void setSelectedPosition(int pos) {
selectedPos = pos;
// inform the view of this change
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder; // to reference the child views for later actions
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
// cache view fields into the holder
holder = new ViewHolder();
holder.nameText = (TextView) v.findViewById(R.id.nameText);
holder.priceText = (TextView) v.findViewById(R.id.priceText);
holder.changeText = (TextView) v.findViewById(R.id.changeText);
// associate the holder with the view for later lookup
v.setTag(holder);
}
else {
// view already exists, get the holder instance from the view
holder = (ViewHolder)v.getTag();
}
// change the row color based on selected state
if (selectedPos == position) {
v.setBackgroundResource(R.drawable.stocks_selected_gradient);
holder.nameText.setTextColor(Color.WHITE);
holder.priceText.setTextColor(Color.WHITE);
holder.changeText.setTextColor(Color.WHITE);
} else {
v.setBackgroundResource(R.drawable.stocks_gradient);
holder.nameText.setTextAppearance(getApplicationContext(), R.style.BlueText);
holder.priceText.setTextAppearance(getApplicationContext(), R.style.BlueText);
holder.changeText.setTextAppearance(getApplicationContext(), R.style.BlueText);
}
Quote q = items.get(position);
if (q != null) {
if (holder.nameText != null) {
holder.nameText.setText(q.getSymbol());
}
if (holder.priceText != null) {
holder.priceText.setText(q.getLastTradePriceOnly());
}
if (holder.changeText != null) {
try {
float floatedChange = Float.valueOf(q.getChange());
if (floatedChange < 0) {
if (selectedPos != position)
holder.changeText.setTextAppearance(getApplicationContext(), R.style.RedText); // red
} else {
if (selectedPos != position)
holder.changeText.setTextAppearance(getApplicationContext(), R.style.GreenText); // green
}
} catch (NumberFormatException e) {
System.out.println("not a number");
} catch (NullPointerException e) {
System.out.println("null number");
}
holder.changeText.setText(q.getChange() + " (" + q.getPercentChange() + ")");
}
}
return v;
}
}
私有类QuoteAdapter扩展了ArrayAdapter{
私有ArrayList项;
//用于在ListView中保留选定位置
private int selectedPos=-1;//未选择的初始值
公共QuoteAdapter(上下文上下文、int-textViewResourceId、ArrayList项){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
公共无效设置选定位置(内部位置){
selectedPos=pos;
//通知视图此更改
notifyDataSetChanged();
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
ViewHolder;//为以后的操作引用子视图
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u充气器_
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true" >
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="@color/my_color" />
<item
android:state_selected="true"
android:drawable="@color/my_color" />
</item>
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="@android:drawable/list_selector_background" />
</selector>