我的应用中有可扩展列表。它有多个子项,每个子项行需要有5个按钮。到目前为止,我已经取得了这么多成就。
问题是我从网络服务中得到一个计数,告诉我应该拥有的按钮总数。现在假设我得到的计数是24,所以将有5个子项目,每个子项目将有5个按钮可见,而最后一个孩子应该只有四个按钮可见(5 + 5 + 5 + 5 + 4 = 24)。
到目前为止,我能够获得正确数量的子项目,并且还可以获得最后一行中应该有多少个按钮。但是,问题是我不知道如何告诉ExpandableList专门显示/隐藏特定子项的按钮。因为如果我隐藏了一些按钮,那么按钮会隐藏在所有childItems中。例如。如果我隐藏第五个按钮,它会隐藏在所有子项中,而我只是希望它隐藏在最后一行/子项中。
我只是希望能够正确引用行/子项的按钮。
这是我的代码。
ArrayList<String> times = new ArrayList<String>();
HashMap<String, ArrayList<String>> timesChild = new HashMap<String, ArrayList<String>>();
ArrayList<String> firstname = new ArrayList<String>();
ArrayList<String> lastname = new ArrayList<String>();
填充上述变量,然后......
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpandableListAdapter(Context context) {
myContext = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getLastCount(int groupPosition) {
int lastCount = timesChild.get(firstname.get(groupPosition)).size() % 5;
return lastCount;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.appointmentstab2nextchildlist, null);
}
int count = getChildrenCount(groupPosition);
int lastCount = getLastCount(groupPosition);
Button b1 = (Button) convertView
.findViewById(R.id.button1);
Button b2 = (Button) convertView
.findViewById(R.id.button2);
Button b3 = (Button) convertView
.findViewById(R.id.button3);
Button b4 = (Button) convertView
.findViewById(R.id.button4);
Button b5 = (Button) convertView
.findViewById(R.id.button5);
for(int i=0; i<count; i++) {
if(i == (count-1)) {
if(lastCount == 1) {
b2.setVisibility(View.INVISIBLE);
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
} else if(lastCount == 2) {
b3.setVisibility(View.INVISIBLE);
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
} else if(lastCount == 3) {
b4.setVisibility(View.INVISIBLE);
b5.setVisibility(View.INVISIBLE);
} else if(lastCount == 4) {
b5.setVisibility(View.INVISIBLE);
}
}
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
Log.d("child count is", Integer.toString(timesChild.get(firstname.get(groupPosition)).size()));
int count = timesChild.get(firstname.get(groupPosition)).size() / 5;
return count + 1;
}
@Override
public Object getGroup(int groupPosition) {
return firstname.get(groupPosition);
}
@Override
public int getGroupCount() {
return firstname.size();
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(
R.layout.appointmentstab2nextlist, null);
}
TextView groupName = (TextView) convertView
.findViewById(R.id.textView1);
groupName.setText(firstname.get(groupPosition) + " " + lastname.get(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}