0

我正在尝试实现一个全选复选框,onClick 将检查列表中的所有复选框。

在活动 onCreate 中,它会加载此布局。selectall 复选框将始终位于顶部的位置。

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="50dp" >

    <CheckBox 
        android:id="@+id/cb_selectall" 
        android:text="@string/s_ab_selectall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </CheckBox>
 </LinearLayout>

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:cacheColorHint="#0000"
        android:clipToPadding="true"
        android:dividerHeight="1dp"
        android:scrollbars="none"
        android:soundEffectsEnabled="true" >
    </ListView>

这是我尝试使用 setOnCheckedChangeListener 实现全选的适配器。

public class MyMediaAdapter extends ArrayAdapter<Media> {

        private final List<Media> list;
        private final Activity context;

        public MyMediaAdapter(Activity context, List<Media> list) {
            super(context, R.layout.media_view, list);
            this.context = context;
            this.list = list;
        }

        private class ViewHolder {
            protected TextView fName, fSub, fDuration, fSize;
            protected CheckBox checkbox;
            // protected CheckBox checkbox1;
        }

        @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        // Moved here to ensure the checkbox is persistent
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.media_view, null);
            // Moved out of the if-else to solve the problem
            // view being recycled each time it scrolls
            // final ViewHolder viewHolder = new ViewHolder();
            viewHolder.fName = (TextView) view.findViewById(R.id.tvfname);
            viewHolder.fSub = (TextView) view.findViewById(R.id.tvsub);
            viewHolder.fDuration = (TextView) view.findViewById(R.id.tvduration);
            viewHolder.fSize = (TextView) view.findViewById(R.id.tvsize);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);

            // viewHolder.checkbox.setId(position);
            viewHolder.checkbox.setOnClickListener(new OnClickListener() {  
                public void onClick(View v) {  
                    // TODO Auto-generated method stub  
                    CheckBox cb = (CheckBox) v;  
                    int id = cb.getId();  
                    if (selection[id]) {  
                        cb.setChecked(false);  
                        selection[id] = false; 
                        list.get(id).setSelected(false);
                    } else {  
                        cb.setChecked(true);  
                        selection[id] = true;  
                        list.get(id).setSelected(true);
                    }  
                }  
            });  

            view.setTag(viewHolder);
            // Moved out of the if-else to solve the problem
            // view being recycled each time it scrolls
            // viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            // Moved out of the if-else to solve the problem
            // view being recycled each time it scrolls
            // ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
            viewHolder = (ViewHolder) view.getTag();
        }

        final CheckBox checkbox1 = (CheckBox) findViewById(R.id.cb_selectall);

        checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton button, boolean checked) {
                // TODO Auto-generated method stub

                if (checked) {
                    checkbox1.setText("Click to Deselect All");

                    for (int i = 0; i < getMediaList.size(); i++) {
                        selection[i] = true;
                        list.get(i).setSelected(true);
                    }
                } else {
                    checkbox1.setText("Click to Select All");

                    for (int i = 0; i < getMediaList.size(); i++) {
                        selection[i] = false;
                        list.get(i).setSelected(false);
                    }
                }
                Toast.makeText(getApplicationContext(), "this " +checked, Toast.LENGTH_LONG).show();
            }

        });

        // Moved here to ensure the checkbox is persistent
        viewHolder.checkbox.setId(position);
        viewHolder.checkbox.setTag(list.get(position));

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.fName.setText(list.get(position).getName());
        holder.fSub.setText(list.get(position).getPath());
        // Converting duration from String to Long
        long milli = Long.valueOf(list.get(position).getDuration());
        // Put it in % min, % sec format to display
        holder.fDuration.setText(util.readableTime(milli));
        // Convert data size from String to Long
        long datasize = Long.valueOf(list.get(position).getData());
        // Put in human readable format
        holder.fSize.setText(util.readableFileSize(datasize));
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
    }

问题是,当我勾选全选复选框时,我能够获取列表中的项目,就在视图本身中,单个复选框仅在我向下滚动时显示。当我选中全选复选框时,它不会立即得到检查。

更新:我通过简单地调用 notifyDataSetChanged(); 解决了我的问题;在 checkchangelistener 之后。

4

1 回答 1

0

使您的父布局为相对布局,然后在您的全选复选框中添加此属性,

android:layout_alignParentTop="true"
于 2013-04-21T13:35:54.747 回答