我有一个带复选框的列表视图。
以下是我的getView方法,
viewWillTransitionToSize()
我如何知道用户正在检查的复选框的数量。
答案 0 :(得分:1)
请尝试这样做。
在适配器中全局定义。
private static int checkBoxCounter = 0;
mViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked && checkBoxCounter>=2){
list.get(position).setChecked(false);
mViewHolder.mCheckBox.setChecked(false);
}else {
if (isChecked) {
starOne.add((Integer) mViewHolder.mCheckBox.getTag());
checkBoxCounter++;
} else {
checkBoxCounter--;
}
cCheckedItems.setText(checkBoxCounter); //<-- ADD THIS
Toast.makeText(context, "there are " + checkBoxCounter + " items selected", Toast.LENGTH_LONG).show();
}}});
希望这可以帮助您解决问题。
答案 1 :(得分:1)
//add a int to you adapter class
int count = 0;
//add listener to getView()
mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
count++;
} else {
count--;
}
}
});
答案 2 :(得分:1)
//Create a modal class
mSparseBooleanArray = new SparseBooleanArray();
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (convertView == null) {
//inflate the view for each row of listview
view = layoutInflater.inflate(R.layout.rating_item, null);
mViewHolder = new ViewHolder();
mViewHolder.mstatus = (TextView) view.findViewById(R.id.ratingStatus);
mViewHolder.mCheckBox = (CheckBox) view.findViewById(R.id.checkBox);
view.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) view.getTag();
}
if (StarListHolder.getInstance().getStarOne().toString() != null) {
mViewHolder.mCheckBox.setTag(position);
String starTest = String.valueOf(listForView.get(position));
ArrayList<String> starOne = StarListHolder.getInstance().getStarOne();
List<String> starList = StarTest.getInstance().getStarList();
mViewHolder.mstatus.setText(starList.get(position));
mViewHolder.mstatus.setOnCheckedChangeListener(new CheckchangeListener(data,mCheckBox));
}
return view;
}
//get all checked items by calling this method
public ArrayList<String> getCheckedItems() {
ArrayList<String> mTempArry = new ArrayList<String>();
for (int i = 0; i < listForView.size(); i++) {
if (mSparseBooleanArray.get(i)) {
String data = listForView.get(i);
mTempArry.add(data);
}
}
return mTempArry;
}
class CheckchangeListener implements OnCheckedChangeListener {
private int position;
public CheckchangeListener(int position) {
// TODO Auto-generated constructor stub
this.position= position;
}
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
checkbox
.setButtonDrawable(R.drawable.notification_selected_checkbox);
} else {
checkbox
.setButtonDrawable(R.drawable.notification_checkbox);
}
buttonView.setChecked(isChecked);
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
}
}
答案 3 :(得分:1)
我猜猜你想要对这些数据做些什么&amp;反映列表中的更改。以下是步骤:
1)在适配器中创建一个接口。
interface OnListFragmentInteractionListener {
void onListFragmentInteraction(String strSelectedItemName, int intPosition);
}
2)根据您是否使用
onBindViewHolder
或{{{{1} 1}}分别。
getView
3)在您的Activity&amp;中实施界面通知适配器已在列表中进行了更改。
RecyclerView
这将触发要刷新的行。要制定处理用户界面以反映此用户互动的规定,请在BaseAdapter
或holder.Row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mListener.onListFragmentInteraction(holder. strSelectedItemName, intPosition);
}
}
});
方法中插入此条件
@Override
public void onListFragmentInteraction(String strSelectedItemName, int intPosition) {
if(lsSelection == null)
lsSelection = new ArrayList<>();
if(lsSelection.contains(strSelectedItemName)) {
lsSelectedAreas.remove(strSelectedItemName);
recyclerViewAdapter.getSelection().remove(strSelectedItemName);
recyclerViewAdapter.getSelection().put(strSelectedItemName, false);
recyclerViewAdapter.notifyItemChanged(intPosition);
} else {
lsSelectedAreas.add(strSelectedItemName);
recyclerViewAdapter.getSelection().remove(strSelectedItemName);
recyclerViewAdapter.getSelection().put(strSelectedItemName, true);
recyclerViewAdapter.notifyItemChanged(intPosition);
}
}
完成所有操作后,只需返回getView
即可获得最终选择。
注意:我使用ImageView代替复选框,并在整个行中应用了tap事件,因为我使用的是自定义设计的复选框。
答案 4 :(得分:0)
//On your Activity or fragment Class
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int count = listView.getAdapter().getCount();
LinearLayout ll=(LinearLayout)v.findViewById(R.id.ll1);
for (int i = 0; i < count; i++) {
ll = (LinearLayout) gridView.getChildAt(i);
CheckBox cb = (CheckBox)ll.findViewById(R.id.checkbox1);
TextView tv = (TextView)ll.findViewById(R.id.textView1);
// StringBuilder stringBuilder=new StringBuilder();
if (cb.isChecked()) {
Log.e("CheckBoxch", "" + cb.getTag().toString());
Log.e("text", "" + tv.getTag().toString());
stringBuilder.append(tv.getTag().toString() + ",");
type_of_truck_appended = stringBuilder.toString();
if (pwindowforTruckName.isShowing()) {
pwindowforTruckName.dismiss();
}
}
}
}