当我滚动我的列表项时,它的松散状态意味着第一个元素行,如果在那之后扩展第二行显示的其他项目。
供您参考也附上我的代码。
ExpListViewAdapterWithCheckBox.java (适配器类)
public class ExpListViewAdapterWithCheckBox extends BaseExpandableListAdapter {
// Define activity context
private Context mContext;
private boolean toggleState;
/*
* Here we have a Hashmap containing a String key
* (can be Integer or other type but I was testing
* with contacts so I used contact name as the key)
*/
private HashMap<String, List<String>> mListDataChild;
private ArrayList<Integer> selectedItemsList = new ArrayList<Integer>();
// ArrayList that is what each key in the above
// hashmap points to
private ArrayList<String> mListDataGroup;
// Hashmap for keeping track of our checkbox check states
private HashMap<Integer, boolean[]> mChildCheckStates;
// Our getChildView & getGroupView use the viewholder patter
// Here are the viewholders defined, the inner classes are
// at the bottom
private GroupViewHolder groupViewHolder;
/*
* For the purpose of this document, I'm only using a single
* textview in the group (parent) and child, but you're limited only
* by your XML view for each group item :)
*/
private String groupText;
private String childText;
/* Here's the constructor we'll use to pass in our calling
* activity's context, group items, and child items
*/
public ExpListViewAdapterWithCheckBox(Context context, ArrayList<String> listDataGroup, HashMap<String, List<String>> listDataChild) {
mContext = context;
mListDataGroup = listDataGroup;
mListDataChild = listDataChild;
// Initialize our hashmap containing our check states here
mChildCheckStates = new HashMap<Integer, boolean[]>();
}
@Override
public int getGroupCount() {
return mListDataGroup.size();
}
/*
* This defaults to "public object getGroup" if you auto import the methods
* I've always make a point to change it from "object" to whatever item
* I passed through the constructor
*/
@Override
public String getGroup(int groupPosition) {
return mListDataGroup.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// I passed a text string into an activity holding a getter/setter
// which I passed in through "ExpListGroupItems".
// Here is where I call the getter to get that text
groupText = getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.group_item, null);
// Initialize the GroupViewHolder defined at the bottom of this document
groupViewHolder = new GroupViewHolder();
groupViewHolder.mGroupText = (TextView) convertView.findViewById(R.id.groupTextView);
groupViewHolder.mGroupText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.mGroupText.setText(groupText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return mListDataChild.get(mListDataGroup.get(groupPosition)).size();
}
/*
* This defaults to "public object getChild" if you auto import the methods
* I've always make a point to change it from "object" to whatever item
* I passed through the constructor
*/
@Override
public String getChild(int groupPosition, int childPosition) {
return mListDataChild.get(mListDataGroup.get(groupPosition)).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
// I passed a text string into an activity holding a getter/setter
// which I passed in through "ExpListChildItems".
// Here is where I call the getter to get that text
childText = getChild(mGroupPosition, mChildPosition);
final ChildViewHolder childViewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(R.layout.child_item, childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView
.getTag(R.layout.child_item);
}
childViewHolder.mChildText.setText(childText);
/*
* You have to set the onCheckChangedListener to null
* before restoring check states because each call to
* "setChecked" is accompanied by a call to the
* onCheckChangedListener
*/
//childViewHolder.mToggle.setOnScrollChangeListener(null);
childViewHolder.mToggle.setFocusable(true);
childViewHolder.mToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(childViewHolder.mToggle.getState().equals(FlashButton.FlashEnum.AUTOMATIC)){
childViewHolder.mToggle.setState(FlashButton.FlashEnum.ON);
}else if(childViewHolder.mToggle.getState().equals(FlashButton.FlashEnum.ON)){
childViewHolder.mToggle.setState(FlashButton.FlashEnum.OFF);
}else if(childViewHolder.mToggle.getState().equals(FlashButton.FlashEnum.OFF)){
childViewHolder.mToggle.setState(FlashButton.FlashEnum.AUTOMATIC);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
public void setToggleStatus(boolean status) {
toggleState = status;
}
public ArrayList<Integer> getarray() {
return selectedItemsList;
}
@Override
public boolean hasStableIds() {
return false;
}
class GroupViewHolder {
TextView mGroupText;
}
class ChildViewHolder {
TextView mChildText;
FlashButton mToggle;
public ChildViewHolder(View v) {
mToggle = (FlashButton) v.findViewById(R.id.tstb_1);
mChildText = (TextView) v.findViewById(R.id.childTextView);
}
}
}
还附加了代码,我得到了三个状态切换的逻辑。 的 FlashButton.java
public class FlashButton extends ToggleButton {
public enum FlashEnum {
AUTOMATIC, ON, OFF
}
public interface FlashListener {
void onAutomatic();
void onOn();
void onOff();
}
private FlashEnum mState;
private FlashListener mFlashListener;
public FlashButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
int next = ((mState.ordinal() + 1) % FlashEnum.values().length);
setState(FlashEnum.values()[next]);
performFlashClick();
}
});
//Sets initial state
setState(FlashEnum.AUTOMATIC);
}
private void performFlashClick() {
if (mFlashListener == null) return;
switch (mState) {
case AUTOMATIC:
mFlashListener.onAutomatic();
break;
case ON:
mFlashListener.onOn();
break;
case OFF:
mFlashListener.onOff();
break;
}
}
private void createDrawableState() {
switch (mState) {
case AUTOMATIC:
setBackgroundResource(R.drawable.timer15x);
break;
case ON:
setBackgroundResource(R.drawable.onswitch1x);
break;
case OFF:
setBackgroundResource(R.drawable.switchoff1x);
break;
}
}
public FlashEnum getState() {
return mState;
}
public void setState(FlashEnum state) {
if (state == null) return;
this.mState = state;
createDrawableState();
}
public FlashListener getFlashListener() {
return mFlashListener;
}
public void setFlashListener(FlashListener flashListener) {
this.mFlashListener = flashListener;
}
}
这个可扩展的Listview问题或者在我的代码中做了一些更改来解决它。