下面是我在getView()中出现的代码片段。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final InterestItems temp;
temp = getItem(position);
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(layoutResource, null, false);
holder = new ViewHolder();
holder.tv_interest_name = (TextView) convertView.findViewById(R.id.tv_interestName_custom_row);
holder.switchCompat = (SwitchCompat) convertView.findViewById(R.id.switch_custom_row);
convertView.setTag(holder);
if(temp.isInterested)
{
holder.switchCompat.setChecked(true);
}
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv_interest_name.setText(temp.getInterestName());
holder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String name = temp.getInterestName();
if (isChecked) {
if (interfaceObject != null) {
interfaceObject.addedInterest(name, position);
list.get(position).setIsInterested(true);
}
} else {
if (interfaceObject != null) {
interfaceObject.removedInterest(name, position);
list.get(position).setIsInterested(false);
}
}
}
});
return convertView;
}
当我自动滚动时,会检查某些开关,但是没有任何值插入到数组中,也没有在特定点触发调试器。我无法理解为什么会这样。如果您有任何帮助,我们将不胜感激。
答案 0 :(得分:0)
将final设置为View,ViewGroup和LayoutInflater。希望它有所帮助。
答案 1 :(得分:0)
使用以下代码:
listView.setOnScrollListener(lv_onScrollListener);
private OnScrollListener lv_onScrollListener = new OnScrollListener() {
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
adapter.setScroll(false);
adapter.notifyDataSetChanged();
} else{
adapter.setScroll(true);
}
}
};
在适配器类中:
Boolean isScroll = false;
public void setScroll(Boolean status) {
isScroll = status;
}
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (!isScroll)
{
//Do it here
}
}
});