1

我正在创建一个列表视图,每行都有自定义布局。每个自定义布局都有一个复选框。我有一个复选框,单击时必须单击列表视图中的所有复选框。我可以单击可见行中的复选框,但我面临不可见行的问题。我尝试在列表适配器中检查所有内容,但它不起作用。

MainActivity.java

public class MainActivity extends Activity implements OnItemClickListener{

    ListView listView;
    ArrayAdapter<Model> adapter;
    static List<Model> list = new ArrayList<Model>();
    public static int count = 0;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        listView = (ListView) findViewById(R.id.my_list);
        adapter = new MyAdapter(this,getModel());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        count = listView.getCount();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        TextView label = (TextView) v.getTag(R.id.label);
        CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
        Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
    }

    private String isCheckedOrNot(CheckBox checkbox) {
        if(checkbox.isChecked())
            return "is checked";
        else
            return "is not checked";
    }

    private List<Model> getModel() {
        list.add(new Model("Linux"));
        list.add(new Model("Windows7"));
        list.add(new Model("Suse"));
        list.add(new Model("Eclipse"));
        list.add(new Model("Ubuntu"));
        list.add(new Model("Solaris"));
        list.add(new Model("Android"));
        list.add(new Model("iPhone"));
        list.add(new Model("Java"));
        list.add(new Model(".Net"));
        list.add(new Model("PHP"));
        list.add(new Model("U1"));
        list.add(new Model("S1"));
        list.add(new Model("A1"));
        list.add(new Model("i1"));
        list.add(new Model("J1"));
        list.add(new Model(".N1"));
        list.add(new Model("PP1"));
        return list;
    }
}

我的适配器.java

public class MyAdapter extends ArrayAdapter<Model> {

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

    TextView text;
    CheckBox checkbox;
    boolean flag;

    public MyAdapter(Activity context, List<Model> list) {
        super(context, R.layout.row, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        final CheckBox c = (CheckBox) ((ViewGroup) parent.getParent()).getChildAt(0);


        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            convertView = inflator.inflate(R.layout.row, null);
        }

            text = (TextView) convertView.findViewById(R.id.label);
            checkbox = (CheckBox) convertView.findViewById(R.id.check);
            checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            int getPosition = (Integer) buttonView.getTag(); 
                            list.get(getPosition).setSelected(buttonView.isChecked());
                        }
                    });

        checkbox.setTag(position); 

        text.setText(list.get(position).getName());

        c.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                for (int i = 0; i < parent.getChildCount(); i++) {

                    RelativeLayout r = (RelativeLayout) parent.getChildAt(i);

                    CheckBox check1 = (CheckBox) r.getChildAt(1);

                    if(check1 instanceof CheckBox){

                        check1.setChecked(true);
                    }
                }
            }
        });

        if(flag){

            list.get(position).setSelected(flag);

        }

        checkbox.setChecked(list.get(position).isSelected());   

        return convertView;
    }
}

主要的.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <CheckBox
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="10dip"
            android:focusable="false"
            android:focusableInTouchMode="false" >
        </CheckBox>

        <ListView
            android:id="@+id/my_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />

    </LinearLayout>

行.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30sp" >
    </TextView>

    <CheckBox
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="10dip"
        android:focusable="false"
        android:focusableInTouchMode="false" >
    </CheckBox>

</RelativeLayout>
4

1 回答 1

1

列表视图只有您可以看到的尽可能多的孩子,因此getChildCount()只会返回可见的内容。

您将需要在您的Model类中有一个字段来存储其checked状态。所以在你的适配器的getView方法中,你会做类似的事情

Model myModel = getItem(position);
checkBox.setChecked(myModel.isChecked());

Select all应该循环遍历列表并将所有Model对象标记为选中并更新列表视图中的可见视图。下次滚动复选框时,将从相关模型对象中读取其选中状态,从内部getView()

于 2012-08-21T05:55:22.753 回答