我正在使用以下实现 Checkable 接口的自定义 LinearLayout。问题是即使我已经定义了我也可以选择多个项目
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
知道有什么问题吗?
package in.co.madhur.ganalyticsdashclock;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.RadioButton;
public class CheckedLinearLayout extends LinearLayout implements Checkable
{
// RadioButton radioButton;
private boolean isChecked;
private List<Checkable> checkableViews=new ArrayList<Checkable>();
public CheckedLinearLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CheckedLinearLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public CheckedLinearLayout(Context context)
{
super(context);
}
@Override
public boolean isChecked()
{
return isChecked;
}
@Override
public void setChecked(boolean checked)
{
for (Checkable c : checkableViews)
{
// Pass the information to all the child Checkable widgets
c.setChecked(isChecked);
}
}
@Override
public void toggle()
{
this.isChecked = !this.isChecked;
for (Checkable c : checkableViews)
{
// Pass the information to all the child Checkable widgets
c.toggle();
}
}
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
final int childCount = this.getChildCount();
for (int i = 0; i < childCount; ++i)
{
findCheckableChildren(this.getChildAt(i));
}
}
/**
* Add to our checkable list all the children of the view that implement the
* interface Checkable
*/
private void findCheckableChildren(View v)
{
if (v instanceof Checkable)
{
this.checkableViews.add((Checkable) v);
}
if (v instanceof ViewGroup)
{
final ViewGroup vg = (ViewGroup) v;
final int childCount = vg.getChildCount();
for (int i = 0; i < childCount; ++i)
{
findCheckableChildren(vg.getChildAt(i));
}
}
}
}
以下是我在列表视图中的单行布局:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/AccountTypeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="AccountImage"
android:src="@drawable/location_web_site" />
<TextView
android:id="@+id/property_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/AccountTypeImage"
android:ellipsize="end"
android:paddingLeft="25dp"
android:text="Header"
android:textColor="@color/app_text_main_light_color"
android:textSize="@dimen/text_size_large" />
<View
android:id="@+id/profile_header_line"
android:layout_width="fill_parent"
android:layout_height="2.0px"
android:layout_alignParentBottom="true"
android:background="@color/app_text_main_light_color" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioselect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:text="Profile"
android:textSize="@dimen/text_size_medium" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="10dp" />