我有一个带有可扩展列表项的自定义ListView + ArrayAdapter。当我展开列表项并滚动使其不再可见时,当我向后滚动时,展开布局中的按钮将不再可单击。但是,这个问题并不一致。有时,当我上下滚动时,这些按钮是可单击的。当我展开列表项并且不滚动时,按钮可以正常工作,所以这就是为什么我认为它一定是ArrayAdapter中的错误。
我在XML中设置了onClick,当我在ArrayAdapter的getView中检查了clickable时,这些按钮都是可单击的。
这是ArrayAdapter的getView;我在适配器中使用了多种布局,并且只有ParameterListItem是可展开的/有问题的项目。
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()) {
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());
holder.valueEditText.setTag("NONE");
//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;
}
这是我的Expandable List Item的XML(第一个RelativeLayout是折叠视图,而第二个是展开视图):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentlinearlayoutid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:clipToPadding="false"
android:paddingBottom="3dp"
android:layout_gravity="center_horizontal">
<RelativeLayout
android:id="@+id/parameter_activity_parameter_background"
android:layout_height="150dp"
android:layout_width="530dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp"
android:background="@drawable/main_list_item_background"
android:layout_gravity="center">
<ImageView
android:id="@+id/parameter_activity_parameter_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:adjustViewBounds="true"
android:background="@null"
android:src="@drawable/ic_launcher"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/parameter_activity_parameter_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Title"
android:textStyle="bold"
android:textSize="21sp"
android:textColor="@color/main_text_color"/>
<EditText
android:id="@+id/parameter_activity_parameter_edit_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/parameter_activity_parameter_units"
android:layout_centerVertical="true"
android:inputType="number|numberSigned|numberDecimal"
android:text="Name"
android:textSize="18sp"
android:selectAllOnFocus="true"
android:visibility="gone"/>
<TextView
android:id="@+id/parameter_activity_parameter_value"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/parameter_activity_parameter_units"
android:layout_centerVertical="true"
android:text="4"
android:textSize="18sp" />
<TextView
android:id="@+id/parameter_activity_parameter_units"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="40dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="deg"
android:textSize="18sp" />
</RelativeLayout>
<com.idunnololz.expandingcells.ExpandingLayout
android:id="@+id/parameter_activity_parameter_expanding_layout"
android:layout_height="325dp"
android:layout_width="530dp"
android:layout_gravity="center"
android:background="@drawable/parameter_activity_expanded_layout_background"
android:elevation="3dp"
android:layout_marginTop="-15dp"
android:visibility="visible">
<View
android:layout_width="475dp"
android:layout_height="1dp"
android:background="@color/secondary_button_color"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="@+id/parameter_activity_parameter_slider_layout"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true">
<TextView
android:id="@+id/parameter_activity_parameter_min_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:text="-20"
android:textColor="@color/secondary_text_color"
android:textStyle="bold"
android:textSize= "18sp"/>
<TextView
android:id="@+id/parameter_activity_parameter_middle_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="-5"
android:textColor="@color/secondary_text_color"
android:textStyle="bold"
android:textSize= "18sp"/>
<com.usb.exo.ui.CustomSeekBar
android:id="@+id/parameter_activity_parameter_slider"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:progress="1"
android:progressDrawable="@android:color/transparent"
android:thumb="@drawable/slidebutton"
android:layout_alignParentBottom="true"
android:isScrollContainer="false" />
<TextView
android:id="@+id/parameter_activity_parameter_max_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:text="10"
android:textColor="@color/secondary_text_color"
android:textStyle="bold"
android:textSize= "18sp"/>
</RelativeLayout>
<LinearLayout
android:id="@+id/parameter_activity_parameter_description_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/parameter_activity_parameter_slider_layout"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/parameter_activity_parameter_description_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingEnd="7dp"
android:paddingStart="0dp"
android:text="Description"
android:textColor="@color/secondary_text_color"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView
android:id="@+id/parameter_activity_parameter_description_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Increasing angle will bend user forward. Adjusts user comfort in sitting."
android:textColor="@color/main_text_color"
android:textSize="15sp"
android:textStyle="normal"/>
</LinearLayout>
<LinearLayout
android:id="@+id/parameter_activity_parameter_use_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/parameter_activity_parameter_description_layout"
android:layout_marginLeft="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/parameter_activity_parameter_use_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingEnd="7dp"
android:paddingStart="0dp"
android:text="Use"
android:textColor="@color/secondary_text_color"
android:textStyle="bold"
android:textSize="18sp"/>
<TextView
android:id="@+id/parameter_activity_parameter_use_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:text="Bends user forward or backward while sitting. Angle of torso relative to vertical."
android:textColor="@color/main_text_color"
android:textSize="15sp"
android:textStyle="normal"/>
</LinearLayout>
<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>
</com.idunnololz.expandingcells.ExpandingLayout>
我希望调用“ cancelClicked”,但事实并非如此。该按钮似乎确实有一些动作(背景变暗)。