我有一个列表视图,每个项目都包含一个具有自定义开关样式的 switchcompat 小部件。第一次填充列表视图时,自定义主题未应用于列表中第一项的 switchcompat 小部件。但是第二次填充列表时它工作正常,(列表适配器被清除并重新填充)
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switch_manage_reward_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checked="false"
android:text="active"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="#fff"
android:theme="@style/RewardSwitch" />
<style name="RewardSwitch" parent="Theme.AppCompat.Light">
<item name="colorControlActivated">#E6E329</item>
<item name="colorSwitchThumbNormal">#f1f1f1</item>
<item name="android:colorForeground">#ffcccccc</item>
</style>
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder v;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.manage_reward_item, null);
v = new ViewHolder();
v.switchRewardStatus = (SwitchCompat) convertView
.findViewById(R.id.switch_manage_reward_active);
convertView.setTag(v);
} else {
v = (ViewHolder) convertView.getTag();
}
final Reward currentReward = getItem(position);
v.switchRewardStatus.setChecked(currentReward.getActive());
return convertView;
}