创建多选择自定义ListView

时间:2014-04-03 14:49:01

标签: android

我正在尝试修改我的ListView以便它:

  1. 包含图片
  2. 包含文字说明(fileName)
  3. 包含一个复选框。
  4. 列表应该是多选的。我应该能够遍历所有选中的复选框并获取说明。

    目前,我可以使用复选框显示普通的ListView。

     ArrayAdapter<String> ad = new ArrayAdapter<String>(HelloDropboxActivity.this, android.R.layout.simple_list_item_multiple_choice,fileName);
                    mTestListOutput.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
                    mTestListOutput.setAdapter(ad);
    

    使用Button的onClick - 我可以循环选中复选框并将文本输入到这样的ArrayList中:

    ArrayList<String> dbLinks = new ArrayList<String>();
     int cntChoice = mTestListOutput.getCount();
      SparseBooleanArray sparseBooleanArray = mTestListOutput.getCheckedItemPositions();
    
                    for(int i = 0; i < cntChoice; i++)
                    {
                        if(sparseBooleanArray.get(i) == true)
                        {
    
                            dbLinks.add(mTestListOutput.getItemAtPosition(i).toString());
                        }
                    }
    

    我正在修复一个ArrayList: private ArrayList路径;

    那么如何将这些结合在一起以创建自定义ListView? 我看了很多例子并尝试修改它们但我无处可去。

    在这里,我试图在list_row.xnl中创建列表行的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    
        android:orientation="horizontal"
        android:padding="5dip" >
    
        <!--  Thumbnail image -->
        <LinearLayout android:id="@+id/thumbnail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip"
            android:layout_alignParentLeft="true"
    
            android:layout_marginRight="5dip">
    
            <ImageView
                android:id="@+id/list_image"
                android:layout_width="50dip"
                android:layout_height="50dip"
                />
    
        </LinearLayout>
    
     <!-- File Name -->
        <TextView
            android:id="@+id/filename"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:textColor="#343434"
            android:textSize="10dip"
            android:layout_marginTop="1dip"
            android:layout_toRightOf="@+id/thumbnail"
            android:text="Just gona stand there and ..." />
    
    </RelativeLayout>
    

1 个答案:

答案 0 :(得分:0)

我认为存在的问题是,当选择列表项时,它实际上已被选中,但您无法看到任何差异,因为根视图必须实现“可检查”。有办法使这项工作虽然它更复杂,我认为它应该是。

首先,您需要添加以下类。这是一个可检查的LinearLayout,它应该将来自可检查界面的调用转发到其中的复选框。对于您要使用的任何视图组,应该很容易修改它。

public class CheckableLinearLayout extends LinearLayout implements Checkable {
private CheckBox _checkbox;

public CheckableLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    // find checked text view
    int childCount = getChildCount();
    for (int i = 0; i < childCount; ++i) {
        View v = getChildAt(i);
        if (v instanceof CheckBox) {
            _checkbox = (CheckBox) v;
        }
    }
}

public boolean isChecked() {
    return _checkbox != null ? _checkbox.isChecked() : false;
}

public void setChecked(boolean checked) {
    if (_checkbox != null) {
        _checkbox.setChecked(checked);
    }
}

public void toggle() {
    if (_checkbox != null) {
        _checkbox.toggle();
    }
}
}

接下来使用上面的CheckableLinearLayout定义您要为每个项目使用的布局:

<com.myPackage.CheckableLinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<CheckBox
    android:id="@+id/checkBox1"
    android:focusable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CheckBox" />

<TextView
    android:id="@+id/textView1"
    android:focusable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<ImageView
    android:id="@+id/imageView1"
    android:focusable="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/background_iv" />

</com.myPackage.CheckableLinearLayout>

然后,更改创建适配器的行,以便改为使用上面布局的ID。

相关问题