【问题标题】:Customized list view with Checked and unchecked all CheckBox columns by another CheckBox in android?自定义列表视图,在 android 中由另一个 CheckBox 选中和取消选中所有 CheckBox 列?
【发布时间】:2016-03-15 16:44:44
【问题描述】:

我的适配器类中有两个CheckBox checkbox1, checkbox2。我希望如果我检查CheckBox 列表中的所有CheckBox 都应该被检查。例如,我的ListView 中有 20 个CheckBox,如果我选中一个,则应检查所有其他 19 个。

这个适配器类

public class ListViewAdapter extends ArrayAdapter<Friend> {

private List<Friend> myFriends;
private Activity activity;
private int selectedPosition = -1;

public ListViewAdapter(Activity context, int resource, List<Friend> objects) {
    super(context, resource, objects);

    this.activity = context;
    this.myFriends = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    // If holder not exist then locate all view from UI file.
    if (convertView == null) {
        // inflate UI from XML file
        convertView = inflater.inflate(R.layout.item_listview, parent, false);
        // get all UI view
        holder = new ViewHolder(convertView);
        // set tag for holder
        convertView.setTag(holder);
    } else {
        // if holder created, get tag from view
        holder = (ViewHolder) convertView.getTag();
    }

    holder.checkBox1.setTag(position); // This line is important.
    holder.checkBox2.setTag(position+100);

    holder.friendName.setText(getItem(position).getName());
    if (position == selectedPosition) {
        holder.checkBox.setChecked(true);
    } else holder.checkBox.setChecked(false);

  if( holder.checkBox1.getTag().equals(position)){                    holder.checkBox1.setOnClickListener(onStateChangedListener(holder.checkBox1, position));
   }else{
       holder.checkBox2.setOnClickListener(onStateChangedListener(holder.checkBox2, position));
 }   
     return convertView;
}
 private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) {
    return new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkBox.isChecked()) {
                selectedPosition = position;
            } else {
                selectedPosition = -1;
            }
            notifyDataSetChanged();
        }
    };
}

private static class ViewHolder {
    private TextView friendName;
    private CheckBox checkBox1,checkBox2;

    public ViewHolder(View v) {
        checkBox1 = (CheckBox)v.findViewById(R.id.check);
        checkBox2=(CheckBox)v.findViewById(R.id.check1);
        friendName = (TextView) v.findViewById(R.id.name);
    }
}
}

【问题讨论】:

    标签: java android android-layout listview android-activity


    【解决方案1】:

    添加两个布尔标志。 isChecked1 在对象级别,isChecked2 在模型中的类级别(静态变量)。默认情况下为假。

    我假设checkbox2 是一旦选中就会将所有复选框更改为选中的那个。

    现在你们所有的模型都将有isChecked1 变量。单击任何checkbox1 时,将模型中的isChecked1 布尔值更改为单击位置上的true。并致电notifyDatasetChanged

    如果用户检查checkbox2,只需将静态变量更改为true。并致电notifyDatasetChanged

    在您的getView 方法中添加简单条件,如果isChecked2 为真,则选中所有复选框,否则仅选中模型具有isChecked1 真的那些复选框。

    【讨论】:

      【解决方案2】:

      为什么您不为此目的使用可扩展列表视图而不是列表视图。

      <ExpandableListView
            android:id="@+id/laptop_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
      </ExpandableListView>
      

      【讨论】:

      • 在ExpandableListView中如何实现我的帖子
      猜你喜欢
      • 2014-05-05
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2012-04-29
      • 2023-04-05
      相关资源
      最近更新 更多