【发布时间】:2017-07-15 08:49:01
【问题描述】:
我在我的代码中使用了可扩展列表视图。它有 3 个组,每个组有 4 个子项。子项包含一个 SwitchButton。问题是当我点击一个开关按钮时,其他开关按钮也会被勾选。当我滚动时,选中的第一个按钮将被取消选中。这里有什么问题。请帮助我。
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.shalom_radio_scedule_child_adapter, null);
}
TextView showTitleTextView = (TextView) convertView
.findViewById(R.id.showTitleTextView);
showEndTimeTextView.setText(showTime);
final SwitchButton mSwitchReminder = (SwitchButton) convertView
.findViewById(R.id.scheduler_reminder_switch);
mSwitchReminder.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if (isChecked) {
reminderStatus = "on";
} else {
reminderStatus = "off"
return convertView;
}
这是我的组头布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/transparent"
android:fadeScrollbars="false"
android:nestedScrollingEnabled="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/lblListHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:text="Sunday"
android:textColor="@color/nav_drawer_blue"
android:textSize="20sp" />
<ImageView
android:id="@+id/scheduler_image_expanded_status"
android:layout_width="match_parent"
android:layout_height="24dp"
android:layout_gravity="center"
android:paddingRight="20dp"
android:scaleType="fitEnd"
android:src="@drawable/ic_expand_row" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#e4e4e4" />
</LinearLayout>
这是孩子的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:nestedScrollingEnabled="true">
<LinearLayout
android:id="@+id/parent_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:orientation="vertical"
android:paddingLeft="20dp">
<TextView
android:id="@+id/showTitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Amme Amme"
android:textColor="@color/nav_drawer_blue"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/showTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="06:00 pm"
android:textColor="@color/nav_grey_light"
android:textSize="@dimen/text_size_normal"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=" - "
android:textColor="@color/nav_grey_light"
android:visibility="gone" />
<TextView
android:id="@+id/showEndTimeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="08:30 pm"
android:textColor="@color/nav_grey_light"
android:textSize="@dimen/text_size_normal" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.3"
android:gravity="center_vertical|end"
android:orientation="vertical"
android:paddingRight="20dp">
<com.suke.widget.SwitchButton
android:id="@+id/scheduler_reminder_switch"
android:layout_width="60dp"
android:layout_height="wrap_content"
app:sb_checked="false"
app:sb_checked_color="@color/nav_blue_light"
app:sb_show_indicator="false" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_vertical|end">
<ImageView
android:id="@+id/scheduler_reminder_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:padding="5dp"
android:src="@drawable/ic_reminder_small" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@id/parent_layout"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@color/nav_blue_light" />
</RelativeLayout>
【问题讨论】:
-
请给我你的适配器和布局代码。
-
请检查这个答案,它可能对你有帮助。 stackoverflow.com/questions/20286769/…
标签: android expandablelistview