由于Listview 的布局方式是wrap_content,这样会计算父控件的高度所以造成了一种反复调用情况,但次数确定的
更深层次的解释为:
View在Draw的时候分成两个阶段:measure和layout,在measure阶段时主要就是为了计算两个参数:height和width。而且要注意的是,这是个递归的过程,从顶向下,DecorView开始依次调用自己子元素的measure。计算完成这两个参数后就开始layout,最后再是draw的调用。
对于ListView,当然每一个Item都会被调用measure方法,而在这个过程中getView和getCount就会多次调用。
简单的解决方法就是
android:layout_height="wrap_content
改为
android:layout_height="match_parent"
但是因为我要用在Dialog上,所以改为match_parent不合适
我打点发现
Log6个条目调用了42次
3个条目调用了21次
所以是7次
我是在ListView中嵌套Switch开关,点击ListView条目来切换Switch的状态,用List来控制,所以要在getView()中加入,
这时候问题来了,调用了这么多次,到底要在哪一次加呢?
一个一个分析发现是第四次。
所以:
6组数据
1=0
2=25 (7-3)×6+1
3=26 (7-3)×6+2
4=27 (7-3)×6+3
5=28 (7-3)×6+4
6=29 (7-3)×6+5
解决方法
int List_Count = getCount();
int aa = List_Count * 4 + 1;
int_list_switch = new int[List_Count];
for (int i = 0; i < List_Count; i++) {
int_list_switch[i] = aa;
aa++;
}
getView()中
//优化Switch控件
if (Optimize_getView == 0) {
list_switch.add(holder.sw_Clock);
}
if (Optimize_getView == int_list_switch[Optimize_getView_Assist]) {
list_switch.add(holder.sw_Clock);
Log.d("添加switch", "" + Optimize_getView);
if (list_switch.size() < getCount())
Optimize_getView_Assist++;
}
全部代码
public class CourseAlarmClockTipsDialog extends DialogFragment {
//private CourseAlarmclockTipsBinding mBinding;
WheelPicker mtime_picker ;
List<Integer> AdvanceTime_List = new ArrayList<>();
public HashMap<Integer, Boolean> Switch_isSelected= new HashMap<Integer, Boolean>();
public static final String SwitchStatus_KEY = "CourseAlarmClockTipsDialog_Clock_Switch";
private ListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int a = 0;
for (int i = 1; i <= 5; i++) {
a = a + 10;
AdvanceTime_List.add(a);
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
FrameLayout layout = (FrameLayout) LayoutInflater.from(getActivity()).inflate(R.layout.course_alarmclock_tips, container, false);
//mBinding = DataBindingUtil.inflate(inflater, R.layout.course_alarmclock_tips, container, false);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
Objects.requireNonNull(getDialog().getWindow()).setBackgroundDrawableResource(android.R.color.transparent);
mtime_picker = layout.findViewById(R.id.advance_time_picker);
setPicker();
mListView = layout.findViewById(R.id.coursealarmclocktipsListView1);
initList();
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void setPicker() {
//初始化,设置数据
initPicker(mtime_picker);
mtime_picker.setData(AdvanceTime_List);
//设置滚动监听
mtime_picker.setOnItemSelectedListener((picker, data, position) -> {
UIToast.toast(getActivity(), AdvanceTime_List.get(position) + "");
});
}
private void initPicker(WheelPicker picker) {
picker.setAtmospheric(true);
picker.setCurved(true);
picker.setItemTextSize(72);
picker.setItemAlign(WheelPicker.ALIGN_CENTER);
picker.setSelectedItemTextColor(0xff000000);
picker.setItemSpace(90);
}
CourseAlarmClockTipsDialog.TalkAdapter item_Adapter ;
private void initList() {
// LinkedList<CourseAlarmclockTipsItemBean> mData;
List<Course> mData = new ArrayList<>();
mData.clear();
mData.addAll(LitepalCourseUtil.getCourse(getContext()));
// 将数据源添加到 适配器 中
item_Adapter = new CourseAlarmClockTipsDialog.TalkAdapter(getContext(), (List<Course>) mData);
// 将适配器数据 添加到 ListView
mListView.setAdapter(item_Adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Switch aSwitch=item_Adapter.getaSwitch(position);
if (aSwitch.isChecked()) {
aSwitch.setChecked(false);
//进行业务处理
} else {
aSwitch.setChecked(true);
//进行业务处理
}
Switch_isSelected.put(position,aSwitch.isChecked());
MMkvListDate.SaveIBHashMap(SwitchStatus_KEY,Switch_isSelected);
Toast.makeText(getContext(), aSwitch.isChecked() + " 第" + position, Toast.LENGTH_SHORT).show();
}
});
}
/*
*BaseAdapter
*/
class TalkAdapter extends BaseAdapter implements OnClickListener {
private Context mContext;
private List<Course> mData;
private List<Switch> list_switch;
private int[] int_list_switch;
private int Optimize_getView = 0;
private int Optimize_getView_Assist =0;
public HashMap<Integer, Boolean> Switch_isSelected= new HashMap<Integer, Boolean>();
public TalkAdapter(Context mContext, List<Course> mData) {
this.mData = mData;
this.mContext = mContext;
list_switch = new ArrayList<>();
list_switch.clear();
int List_Count = getCount();
int aa = List_Count * 4 + 1;
int_list_switch = new int[List_Count];
for (int i = 0; i < List_Count; i++) {
int_list_switch[i] = aa;
aa++;
}
// int_list_switch= new int[]{0,17,18,19,28,29};
if (MMkvListDate.isDataCapacity(SwitchStatus_KEY)) {
Switch_isSelected.clear();
Switch_isSelected = MMkvListDate.getIBHashMap(SwitchStatus_KEY);
} else {
for (int i = 0; i < getCount(); i++) {
Switch_isSelected.put(i, false);
}
}
}
public Switch getaSwitch(int position) {
// Toast.makeText(mContext,list_switch.size()+ "位置:"+position, Toast.LENGTH_SHORT).show();
return list_switch.get(position);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.course_alarmclock_tips_list_item, parent, false);
holder = new ViewHolder();
holder.tv_className = (TextView) convertView.findViewById(R.id.coursealarmclocktipslistitemTextView1);
holder.tv_classTime = (TextView) convertView.findViewById(R.id.coursealarmclocktipslistitemTextView2);
holder.sw_Clock = (Switch)convertView.findViewById(R.id.coursealarmclocktipslistitemSwitch1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String CourseName =mData.get(position).getName();
String jc = LitepalCourseUtil.getJc(mData.get(position).getTime());
holder.tv_className.setText(CourseName);
holder.tv_classTime.setText(jc);
System.out.println("list_getView()" + position);
//优化Switch控件
if (Optimize_getView == 0) {
list_switch.add(holder.sw_Clock);
}
if (Optimize_getView == int_list_switch[Optimize_getView_Assist]) {
list_switch.add(holder.sw_Clock);
Log.d("添加switch", "" + Optimize_getView);
if (list_switch.size() < getCount())
Optimize_getView_Assist++;
}
Log.d("Log_test", "___" + Optimize_getView);
Optimize_getView++;
holder.sw_Clock.setChecked(Switch_isSelected.get(position));
// holder.sw_Clock.setChecked(Switch_isSelected.get(position)==null?false:true);
return convertView;
}
@Override
public void onClick(View p1) {
}
private class ViewHolder {
TextView tv_className;
TextView tv_classTime;
Switch sw_Clock;
}
}
}
网友评论