Java “为什么?”;“解锁”;滚动后项目是否显示为禁用?
我编写了一个自定义适配器(Java “为什么?”;“解锁”;滚动后项目是否显示为禁用?,java,android,android-listview,android-arrayadapter,Java,Android,Android Listview,Android Arrayadapter,我编写了一个自定义适配器(LevelAdapter,因为我正在显示一个游戏的级别列表),它从虚拟对象列表中填充一个ListView片段。每个虚拟对象都拥有一个布尔“unlocked”属性,我试图根据基础虚拟项是否为“unlocked”对列表中的每个项有条件地禁用和设置一些样式属性。为此,我还采用了 下面的代码可以正常工作,直到用户向下滚动并向上滚动,此时“未锁定”项目不会被禁用,但它们确实会丢失相应的样式 public class LevelAdapter extends ArrayAdapte
LevelAdapter
,因为我正在显示一个游戏的级别列表),它从虚拟对象列表中填充一个ListView
片段。每个虚拟对象都拥有一个布尔“unlocked”属性,我试图根据基础虚拟项是否为“unlocked”对列表中的每个项有条件地禁用和设置一些样式属性。为此,我还采用了
下面的代码可以正常工作,直到用户向下滚动并向上滚动,此时“未锁定”项目不会被禁用,但它们确实会丢失相应的样式
public class LevelAdapter extends ArrayAdapter<DummyContent.DummyItem> {
private List<DummyContent.DummyItem> objects;
private DummyContent.DummyItem item;
private Context context;
private int textViewResourceId;
private int resource;
public LevelAdapter(Context context, int resource, int textViewResourceId, List<DummyContent.DummyItem> objects) {
super(context, resource, textViewResourceId, objects);
this.objects = objects;
this.context = context;
this.resource = resource;
this.textViewResourceId = textViewResourceId;
}
static class ViewHolder {
TextView text;
int position;
boolean unlocked;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
// https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
ViewHolder holder;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(resource, null);
holder = new ViewHolder();
holder.text = (TextView) v.findViewById(textViewResourceId);
holder.position = position;
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
item = objects.get(position);
holder.text.setText(item.title);
if (item.unlocked == false) {
holder.text.setTextColor(Color.LTGRAY);
Typeface type = Typeface.create("", Typeface.ITALIC);
holder.text.setTypeface(type);
}
return v;
}
@Override
public boolean areAllItemsEnabled() {
return true; // technically untrue, but a hack to show the line divider between items
}
@Override
public boolean isEnabled(int position) {
item = objects.get(position);
if (item.unlocked == true) {
return true;
} else {
return false;
}
}
}
公共类级别适配器扩展了ArrayAdapter{
私有列表对象;
私有DummyContent.DummyItem;
私人语境;
私有int textViewResourceId;
私有int资源;
公共级别适配器(上下文上下文、int资源、int textViewResourceId、列表对象){
超级(上下文、资源、textViewResourceId、对象);
this.objects=对象;
this.context=上下文;
这个资源=资源;
this.textViewResourceId=textViewResourceId;
}
静态类视窗夹{
文本查看文本;
内部位置;
布尔解锁;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
// https://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
视窗座;
如果(v==null){
LayoutInflater vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(资源,空);
holder=新的ViewHolder();
holder.text=(TextView)v.findViewById(textViewResourceId);
holder.position=位置;
v、 setTag(支架);
}否则{
holder=(ViewHolder)v.getTag();
}
item=objects.get(位置);
holder.text.setText(项目名称);
如果(item.unlocked==false){
holder.text.setTextColor(Color.LTGRAY);
字体类型=Typeface.create(“,Typeface.ITALIC”);
holder.text.setTypeface(类型);
}
返回v;
}
@凌驾
公共布尔值areAllItemsEnabled(){
return true;//技术上不正确,但显示项目之间的行分隔符
}
@凌驾
公共布尔值isEnabled(整型位置){
item=objects.get(位置);
如果(item.unlocked==true){
返回true;
}否则{
返回false;
}
}
}
您应该为unlocked==true
案例创建else语句
现在,如果适配器获得视图以重用具有未锁定false的项以填充具有未锁定true的项,则不会更改样式,而是保留旧样式。工作:
}否则{holder.text.setTextColor(Color.BLACK);Typeface type=Typeface.create(“,Typeface.NORMAL);holder.text.setTypeface(type);}
if (item.unlocked == false) {
holder.text.setTextColor(Color.LTGRAY);
Typeface type = Typeface.create("", Typeface.ITALIC);
holder.text.setTypeface(type);
}