我想要一个包含一些项目的 listView,并且可以检查所有项目。但是如果第一个项目没有被选中,那么另一个项目就没有被启用,当然,它们当时不应该是可检查的。所以我像这样将 ListView 扩展到 MyListView :
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
Init();
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
Init();
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Init();
}
private void Init() {
setAdapter(new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_multiple_choice,array ));
setChoiceMode(CHOICE_MODE_MULTIPLE);
setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
SparseBooleanArray posArray = getCheckedItemPositions();
boolean bChecked = false;
if( null != posArray ) {
for( int i = 0; i < posArray.size(); i++ ) {
int nIdx = posArray.keyAt(i);
if( nIdx == 0 ) {
bChecked = posArray.get(nIdx);
break;
}
}
}
if( position == 0 ) {
bChecked = !bChecked;
for( int i = 1; i < array.length; i++ ) {
getChildAt(i).setEnabled(!bChecked);
}
}
}
});
}
private String[] array = { "Test1", "Test2", "Test3", "Test4" };
}
然后我得到了问题:禁用的项目仍然可以检查,有什么问题吗?我该怎么做?谢谢。
实际上,问题是由 pawan yadav 的建议完成的。贝娄是我的代码,希望帮助任何人遇到同样的问题。
活动主.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>
在活动的 onCreate 方法中:
ListView listView = new ListView(this);
String [] datas = new String[36]; // generate you data here
final boolean [] status = new boolean[36]; // generate you check status here
final MyAdapter adapter = new MyAdapter(datas, status);
listView.setAdapter(adapter );
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if( position== 0 || status[0] ) {
status[position] = !status[positioin];
adapter.notifyDataSetChanged();
}
}
});
RelativeLayout parentLayout = (RelativeLayout)findViewById(R.id.parentLayout);
parentLayout.addView(listView);
listview_checkedtextview.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
/>
活动的内部类:
private class MyAdapter extends BaseAdapter{
public MyAdapter( final String [] datas, final boolean [] status ) {
this.datas = datas;
this.status = status;
inflater = LayoutInflater.from(MainActivity.this);
}
@Override
public int getCount() {
return datas.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if( null == convertView ) {
convertView = inflater.inflate(R.layout.listview_checkedtextview, null);
convertView = convertView.findViewById(R.id.text1);
}
CheckedTextView checkedTextView = (CheckedTextView) convertView;
checkedTextView.setText(datas[position]);
checkedTextView.setChecked(status[position]);
checkedTextView.setEnabled( position == 0 ? true : status[0]);
return convertView;
}
private String [] datas;
private boolean [] status;
private LayoutInflater inflater;
}