创建列表视图后查看持有人警告
Posted
技术标签:
【中文标题】创建列表视图后查看持有人警告【英文标题】:View holder warning after creating list view 【发布时间】:2015-10-22 03:34:54 【问题描述】:创建列表视图并为每个项目的文本分配不同的颜色后,View rowView = inflater.inflate(R.layout.list_item, parent, false);
内的inflater.inflate(R.layout.list_item, parent, false)
会以黄色突出显示,并且我收到与我的视图持有者相关的警告。应该将受影响的代码更改为什么才能消除此警告?
来自视图适配器的无条件布局膨胀:应该使用 View Holder 模式以实现更平滑的滚动
public class FragmentColourChooserList extends android.support.v4.app.Fragment
public FragmentColourChooserList()
// Required empty constructor
ListView list_colourchooser;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_colour_chooser_list, container, false);
String[] listContent =
getActivity().getResources().getString(R.string.item_0),
getActivity().getResources().getString(R.string.item_1),
getActivity().getResources().getString(R.string.item_2),
getActivity().getResources().getString(R.string.item_3),
getActivity().getResources().getString(R.string.item_4),
getActivity().getResources().getString(R.string.item_5),
getActivity().getResources().getString(R.string.item_6),
getActivity().getResources().getString(R.string.item_7),
getActivity().getResources().getString(R.string.item_8),
getActivity().getResources().getString(R.string.item_9)
;
list_colourchooser = (ListView)v.findViewById(R.id.list_colourchooser);
MyColoringAdapter adapter = new MyColoringAdapter(getActivity(),listContent);
list_colourchooser.setAdapter(adapter);
return v;
private class MyColoringAdapter extends ArrayAdapter<String>
private final Context context;
private final String[] values;
public MyColoringAdapter(Context context, String[] values)
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
class Holder
TextView txtView;
@Override
public View getView(int position, View convertView, ViewGroup parent)
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
Holder holder = new Holder();
holder.txtView = (TextView) rowView.findViewById(R.id.list_item);
txtView.setText(values[position]);
int textColorId = R.color.white; // Default color
switch (position)
case 0:
textColorId = R.color.brown; break;
case 1:
textColorId = R.color.red; break;
case 2:
textColorId = R.color.yellow; break;
case 3:
textColorId = R.color.green; break;
case 4:
textColorId = R.color.pink; break;
case 5:
textColorId = R.color.grey; break;
case 6:
textColorId = R.color.purple; break;
case 7:
textColorId = R.color.white; break;
case 8:
textColorId = R.color.darkblue; break;
case 9:
textColorId = R.color.lightblue; break;
txtView.setTextColor(getResources().getColor(textColorId));
return rowView;
【问题讨论】:
首先要做的是搜索 ViewHolder 是什么以及为什么要使用它 由于您是新手,请使用RecyclerView
,ViewHolder
模式为您服务。
花了这么长时间创建这段代码!?没有意义(尤其是不知道要改变什么)
【参考方案1】:
尝试使用 viewHolder 类,而不是直接从 rowView 获取值到单独的类型中。
创建一个持有者类
class Holder
private TextView txtView;
public TextView getTxtView()
return txtView;
尝试替换
TextView textView = (TextView) rowView.findViewById(R.id.list_item);
与 持有人 holderObject = new Holder(); holderObject.getTxtView() = (TextView) rowView.findViewById(R.id.list_item);
【讨论】:
把它放在 FragmentColourChooserList 类里面或者那个类外面没有区别。 不起作用。我现在收到这些警告:Class 'Holder' is never used
和 Field 'txtView' is never used
持有人持有人=新持有人(); holder.txtView = (TextView) rowView.findViewById(R.id.list_item);你这样做了吗?
是的。代码已更新,我现在收到此错误:Cannot resolve symbol 'txtView'
您似乎已将 txtView 设为私有。以上是关于创建列表视图后查看持有人警告的主要内容,如果未能解决你的问题,请参考以下文章