我在我的代码中使用了expandablelistview。它有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>
答案 0 :(得分:0)
在Adapter类中使用SparseBooleanArray来阻止所选位置的状态。
public class ListAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
**SparseBooleanArray mCheckStates;**
// ArrayList<HashMap<Object, Object>> list;
int rs;
LayoutInflater li;
Holder holder;
Context context;
int selectionTag = 0;
private List<SortedTollModel> sortedTollModelList;
public ListAdapter(Context ctx, int resources, List<SortedTollModel> sortedTollModelList) {
context = ctx;
rs = resources;
this.sortedTollModelList = sortedTollModelList;
li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mCheckStates = new SparseBooleanArray(sortedTollModelList.size());
}
@Override
public int getCount() {
return sortedTollModelList.size();
}
@Override
public Object getItem(int position) {
return sortedTollModelList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
holder = new Holder();
v = li.inflate(rs, null);
holder.tv_tollname = (TextView) v.findViewById(R.id.tv_toll_name);
holder.tv_cost = (TextView) v.findViewById(R.id.tv_cost);
holder.select = (CheckBox) v.findViewById(R.id.ch_select);
v.setTag(holder);
} else {
holder = (Holder) v.getTag();
holder.select.setOnCheckedChangeListener(null);
}
holder.tv_tollname.setText(sortedTollModelList.get(position).getName());
String cost;
try {
cost = sortedTollModelList.get(position).getCost();
} catch (Exception e) {
cost = "30";
}
holder.tv_cost.setText(cost + " Rs");
holder.select.setTag(position);
**holder.select.setChecked(mCheckStates.get(position, false));**
//set your SwitchButton state here.
holder.select.setOnCheckedChangeListener(this);
return v;
}
/*return list of selected tolls*/
public List<Integer> getSelectedItemCount() {
List<Integer> selectedCount = new ArrayList<>();
for (int i = 0; i < sortedTollModelList.size(); i++) {
if (mCheckStates.get(i) == true) {
selectedCount.add(i);
}
}
return selectedCount;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
int p = (Integer) buttonView.getTag();
**mCheckStates.put((Integer) buttonView.getTag(), true);** // add selected position's state in `sparse boolen array`
sendCostIntent(sortedTollModelList.get(p).getCost(), true);
} else {
int p = (Integer) buttonView.getTag();
**mCheckStates.put((Integer) buttonView.getTag(), false);**
selectionTag = 0; //selection tag for identifiying from where broadcast is sent
sendCostIntent(sortedTollModelList.get(p).getCost(), false);
}
}
void sendCostIntent(String cost, Boolean cheak) {
Intent intent = new Intent();
intent.setAction(Constants.INTENT_ACTION_DATA_UPDATE);
intent.putExtra("cost", cost);
intent.putExtra("selection_tag", selectionTag);
Log.e("Toll Adapter cost : ", cost);
intent.putExtra("cheak", cheak);
context.sendBroadcast(intent);
}
/*Selects all toll*/
public void selectAll() {
String cost = "";
int totalCost = 0;
for (int i = 0; i < sortedTollModelList.size(); i++) {
mCheckStates.put(i, true);
holder.select.setChecked(mCheckStates.get(i));
cost = sortedTollModelList.get(i).getCost();
totalCost += Integer.parseInt(cost);
}
sendCostIntent(String.valueOf(totalCost), true);
notifyDataSetChanged();
}
public void unSelectAll() {
for (int i = 0; i < sortedTollModelList.size(); i++) {
mCheckStates.put(i, false);
holder.select.setChecked(mCheckStates.get(i));
selectionTag = 1;
}
sendCostIntent(String.valueOf(0), false);
notifyDataSetChanged();
}
class Holder {
TextView tv_tollname;
TextView tv_cost;
CheckBox select;
}
}
答案 1 :(得分:0)
在xml文件中为 ExpandableListview
android:fadeScrollbars="false"
android:nestedScrollingEnabled="true"