我有一个填充有项目的ListView,其中一些包含Button。有时,Buttons onClick()正确注册,但其他时候,onClick()根本不起作用。我正在检查isClickable()是否为true,并且在所有情况下都是如此。为什么Button可以将isClickable()设置为true,但仍不注册onClick()的可能原因是什么?
编辑:这是我的适配器的getView()
public View getView(int position, View convertView, ViewGroup parent) {
final ExpandableListItem object = mData.get(position);
final View view = storedViews.get(position);
if (object instanceof FillerParamListItem) {
return view;
}
//Load the holder
ExpandingViewHolder holder = (ExpandingViewHolder) view.getTag();
//Handle collapsing and expanding the cells
if (((ParameterActivity) activity).listView.isExpanded() && !object.isExpanded()) {
view.setAlpha(.20f);
} else {
view.setAlpha(1f);
}
if (object instanceof SwitchParamListItem) {
....
} else if (object instanceof DomainListItem) {
....
} else if (object instanceof ParameterListItem) {
if (((ParameterActivity) activity).listView.isExpanded() && !object.isExpanded()) {
holder.expandingLayout.setVisibility(View.GONE);
holder.valueTextView.setVisibility(View.VISIBLE);
if (activity.getCoreApplication().getOfflineMode()) {
holder.collapsedLayout.setBackground(activity.getResources().getDrawable(R.drawable.main_list_item_background_offline));
} else {
holder.collapsedLayout.setBackground(activity.getResources().getDrawable(R.drawable.main_list_item_background));
}
holder.collapsedLayout.setElevation(0);
} else {
if (object.isExpanded()) {
System.out.println("expanded parameter item");
holder.expandingLayout.setVisibility(View.VISIBLE);
holder.valueTextView.setVisibility(View.GONE);
holder.valueEditText.setVisibility(View.VISIBLE);
if (activity.getCoreApplication().getOfflineMode()) {
holder.expandingLayout.setBackground(activity.getResources().getDrawable(R.drawable.parameter_activity_expanded_layout_background_offline));
holder.collapsedLayout.setBackground(activity.getResources().getDrawable(R.drawable.parameter_activity_expanded_layout_background_offline));
} else {
holder.expandingLayout.setBackground(activity.getResources().getDrawable(R.drawable.parameter_activity_expanded_layout_background));
holder.collapsedLayout.setBackground(activity.getResources().getDrawable(R.drawable.parameter_activity_expanded_layout_background));
}
holder.collapsedLayout.setElevation(3.0f * activity.getResources().getDisplayMetrics().density);
} else {
holder.expandingLayout.setVisibility(View.GONE);
holder.valueTextView.setVisibility(View.VISIBLE);
holder.valueEditText.setVisibility(View.INVISIBLE);
if (activity.getCoreApplication().getOfflineMode()) {
holder.collapsedLayout.setBackground(activity.getResources().getDrawable(R.drawable.main_list_item_background_offline));
} else {
holder.collapsedLayout.setBackground(activity.getResources().getDrawable(R.drawable.main_list_item_background));
}
holder.collapsedLayout.setElevation(0);
}
}
if (object.getImgResource() != ExpandableListItem.NO_ICON) {
holder.imageView.setImageBitmap(object.getImage());
} else {
holder.imageView.setVisibility(View.INVISIBLE);
}
holder.titleTextView.setText(object.name);
holder.valueTextView.setText(object.getValue());
holder.unitTextView.setText(object.unitSym);
holder.descriptionTextView.setText(object.description);
holder.useTextView.setText(object.hint);
ParameterListItem parameterListItem = (ParameterListItem) object;
//Register listeners for the seeker bar and EditText for this specific parameter
TextListener editTextListener = new TextListener(parameterListItem, activity, view, holder.valueEditText);
holder.seekbar.setOnSeekBarChangeListener(parameterListItem.getListener());
holder.valueEditText.addTextChangedListener(editTextListener);
parameterListItem.registerTextListener(editTextListener);
holder.valueEditText.setOnEditorActionListener(editTextListener);
parameterListItem.registerSeekBar(holder.seekbar);
holder.valueEditText.setTag("PROGRAM");
holder.valueEditText.setText(object.getValue());
//show value and symbols within the expanded cells for the seeker bar track
holder.minValueTextView.setText(Util.getStringForFloat(parameterListItem.getMinValue()) + object.unitSym);
holder.maxValueTextView.setText(Util.getStringForFloat(parameterListItem.getMaxValue()) + object.unitSym);
holder.middleValueTextView.setText(Util.getStringForFloat((parameterListItem.getMaxValue() + parameterListItem.getMinValue()) / 2) + object.unitSym);
view.setOnTouchListener(editTextListener);
}
object.assocView = view;
return view;
}
,这里是XML(我现在仅包括按钮部分)。这里的RelativeLayout包含在“适配器中的expandingLayout”中:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/parameter_activity_parameter_use_layout"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:background="@android:color/transparent">
<Button
android:id="@+id/parameter_activity_parameter_cancel_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:onClick="cancelClicked"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:text="Cancel"
android:textColor="@color/button_text_color"
android:textSize="21sp"
android:textStyle="bold"/>
<Button
android:id="@+id/parameter_activity_parameter_undo_button"
android:layout_height="wrap_content"
android:layout_width = "wrap_content"
android:layout_centerHorizontal="true"
android:clickable="true"
android:onClick="undoClicked"
android:background="?android:attr/selectableItemBackground"
android:text="Undo Last Change"
android:textColor="@color/button_text_color"
android:textSize="21sp"
android:textStyle="bold"/>
<Button
android:id="@+id/parameter_activity_parameter_apply_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:clickable="true"
android:onClick="applyClicked"
android:background="?android:attr/selectableItemBackground"
android:text="Apply"
android:textColor="@color/button_text_color"
android:textSize="21sp"
android:textStyle="bold" />
</RelativeLayout>