如何将Listview与Check和Uncheck项目Android分开?

时间:2013-08-19 04:38:40

标签: android listview scrollview android-linearlayout

在Scrollview中,我在每个列表项目中都有一个带有Textview和复选框的列表视图,我想在同一个列表视图中单独显示项目检查和取消选中,具有不同的布局1.Active与取消选中项目和2.finished with checked items。任何人都可以找到解决方案。

1.Layout

<ScrollView
    android:id="@+id/scrollVie2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/linearLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:fillViewport="true"
    android:layout_below="@+id/linearLayout1"
    android:layout_marginTop="80dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/listv"
            android:layout_width="fill_parent"
            android:layout_height="570dp" >
        </ListView>

        <LinearLayout
            android:layout_width="476dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.67"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Finished"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>
    </LinearLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:1)

尝试使用这样的代码......我在显示图像视图和复选框..

public View getView(int position, View convertview, ViewGroup arg2) {
    if (convertview == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertview = (RelativeLayout) inflater.inflate(
                R.layout.images_gridview, null);
    }
    ImageView gridImage = (ImageView) convertview.findViewById(R.id.image);
    CheckBox checkBox = (CheckBox) convertview.findViewById(R.id.checkBox1);
    checkBox.setTag(position);
    if (photos.get(position).isSelected()) {
        checkBox.setChecked(true);
    }
    WindowManager manager = (WindowManager) context
            .getSystemService(Activity.WINDOW_SERVICE);
    int width;
    if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
        width = manager.getDefaultDisplay().getWidth();
    } else {
        Point point = new Point();
        manager.getDefaultDisplay().getSize(point);
        width = point.x;
    }

    LayoutParams params = new LayoutParams(width / 3, width / 3);
    gridImage.setLayoutParams(params);
    gridImage.setImageURI(Uri.parse(photos.get(position)
            .getThumbnilImageUrl()));
    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkedButton,
                boolean checked) {
            if (checked) {
                selectedPhotos.add(photos.get((Integer) checkedButton
                        .getTag()));
                photos.get((Integer) checkedButton.getTag()).setSelected(
                        true);
            } else {
                selectedPhotos.remove(photos.get((Integer) checkedButton
                        .getTag()));
                photos.get((Integer) checkedButton.getTag()).setSelected(
                        false);
            }
        }
    });

    return convertview;
}