我正在创建一个简单的应用程序,其中显示自定义ListView
[项目名称,项目复选框]中的项目列表。
我想基于以下内容更新我的用户界面(显示/隐藏Button
,该ListView
不属于ListView,但属于Button
&Activity
的{{1}}部分复选框值更改意味着onCheckChange。
下面是我的Adapter类getView()
方法代码,它在我想要处理的列表加载上调用Activity
方法。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
viewHolder = new Holder();
viewHolder.name = (TextView) convertView.findViewById(R.id.textView_Name);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBox);
viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
mainDataList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
mainDataList.get(position).setLatestChanged(true);
notifyDataSetChanged();
activity.setCopyButton(); //This is the call which hides/shows ACTIVITY BUTTON WHICH I WANT TO HANDLE
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (Holder) convertView.getTag();
}
viewHolder.check.setTag(position); // This line is important.
viewHolder.name.setText(mainDataList.get(position).getName());
viewHolder.check.setChecked(mainDataList.get(position).isSelected());
return convertView;
}
在ListView
加载android时默认根据ITEM值[T / F]调用viewHolder.check.setOnCheckedChangeListener
。我必须阻止该调用或以某种方式我想基于手动复选框点击而不是默认处理UI更新。
触发点:
activity.setCopyButton(); //This is the call which hides/shows activity button which I want to handle
答案 0 :(得分:0)
请改用viewHolder.check.setOnCLickListener()
。
在代码中替换以下内容,
viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//... Your code
}
});
有了......
viewHolder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
mainDataList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
mainDataList.get(position).setLatestChanged(true);
notifyDataSetChanged();
activity.setCopyButton(); //This is the call which hides/shows ACTIVITY BUTTON WHICH I WANT TO HANDLE
}
});
答案 1 :(得分:0)
不是将位置设置为标签,而是将自定义数据设置为标签,请参阅下面的更新代码: -
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
viewHolder = new Holder();
viewHolder.name = (TextView) convertView.findViewById(R.id.textView_Name);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(viewHolder);
} else {
viewHolder = (Holder) convertView.getTag();
}
viewHolder.check.setTag(mainDataList.get(position));
viewHolder.name.setText(mainDataList.get(position).getName()); viewHolder.check.setChecked(mainDataList.get(position).isSelected());
viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CustomData data=(CustomData)buttonView.getTag();
data.setSelected(buttonView.isChecked());
data.setLatestChanged(true);
notifyDataSetChanged();
activity.setCopyButton();
}
});
return convertView;
}