如何将自定义ListView的第一项选择为默认
lvModifierSet.setAdapter(modifierSetListAdapter);
AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
ModifierSet modifierSet = modifierSets.get(position);
Toast.makeText(context, modifierSet.getModifier_set_name(), Toast.LENGTH_SHORT).show();
}
};
lvModifierSet.setOnItemClickListener(onItemClickListener);
这是我的自定义适配器
public class ModifierSetListAdapter extends ArrayAdapter<ModifierSet> {
private Context context;
private int resourceLayout;
private List<ModifierSet> modifierSets;
public ModifierSetListAdapter(Context context, int resource, List<ModifierSet> modifierSets) {
super(context, resource, modifierSets);
this.context = context;
resourceLayout = resource;
this.modifierSets = modifierSets;
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = LayoutInflater.from(context);
view = vi.inflate(resourceLayout, null);
}
ModifierSet modifierSet = modifierSets.get(position);
TextView tvModifierName = view.findViewById(R.id.modifier_set_tv_modifier_name);
tvModifierName.setText(modifierSet.getModifier_set_name());
return view;
}
}
我在下面尝试过 link
答案 0 :(得分:0)
尝试
检查适配器中的位置0
设置默认的textColor
tvModifierName.setTextColor(Your Color) ;
//现在检查位置0
if (position ==0){
tvModifierName.setTextColor(Your Color) ;
or set backGround
tvModifierName.setBackgroundColor(Your Color) ;
}
答案 1 :(得分:0)
您有列表视图选择器吗?如果不是,则需要创建一个选择器xml:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@color/pressed_color"/>
<item
android:drawable="@color/default_color" />
</selector>
然后将其作为背景添加到您的列表中:
<ListView>
...
android:background="@drawable/selector"
...
</ListView>
然后它应该可以使用您看到的链接中的方法