我有一个 ExpandableListView,每个组和孩子都有一个图形、文本和一个进度条。它是任务和子任务的概述。问题是,每当我单击其中一个组时,所有进度条都会消失。我尝试了一些方法,例如将序列化对象放入 savedInstanceState 中,但没有奏效。我希望有办法解决这个问题。
有趣的事实是,在 SDK16 下没有这样的行为,只有在 SDK10 中:(
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.overview);
appState = ((AutoBauLogApp) getApplicationContext());
Bundle extras = getIntent().getExtras();
// get selected field and get data
final int position = extras.getInt("position");
Field curField = appState.getModel().getFieldArray().get(position);
// name of field
setTextView(curField, position);
// progressbar
// TODO remove test data 80
curField.setTargetProgress(80);
setProgressBar(curField);
// list for IVehicles
setIVehicleList(curField);
// map button
// setMapButton(curField, position);
// Retrive the ExpandableListView from the layout
ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
}
adapter = new ExpandableListAdapter(this, new ArrayList<Task>(), new ArrayList<ArrayList<Task>>());
// Set this blank adapter to the list view
listView.setAdapter(adapter);
fillListWithData();
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = curField.getAllTasks().size() * 70;
listView.setLayoutParams(params);
listView.requestLayout();
}
我很感谢每一个答案:)
这是适配器代码:
import java.util.ArrayList;
import kit.edu.AutoBauLog.R;
import kit.edu.AutoBauLog.storage.Task;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ExpandableListAdapter extends BaseExpandableListAdapter{
@Override
public boolean areAllItemsEnabled() {
return true;
}
private Context context;
private ArrayList<Task> groups;
private ArrayList<ArrayList<Task>> children;
public ExpandableListAdapter(Context context, ArrayList<Task> groups, Arra yList<ArrayList<Task>> children) {
this.context = context;
this.groups = groups;
this.children = children;
}
/**
* A general add method, that allows you to add a Vehicle to this list
*
* Depending on if the category opf the vehicle is present or not, the corr esponding item will either be added to an
* existing group if it exists, else the group will be created and then the item will be added
*
* @ param vehicle
*/
public void addItem(ArrayList<Task> tasks) {
for (int i = 0; i < tasks.size(); i++) {
this.groups.add(tasks.get(i));
this.children.add(tasks.get(i).getSubtasks());
}
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return children.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// Return a child view. You can load your custom layout here.
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
Task task = (Task) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
setLayout(convertView, task);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return children.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// Return a group view. You can load your custom layout here.
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Task task = (Task) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
setLayout(convertView, task);
return convertView;
}
private void setLayout(View convertView, Task task) {
convertView.invalidate();
convertView.requestLayout();
TextView tv = (TextView) convertView.findViewById(R.id.taskName);
tv.setText(task.getName() + " - " + task.getDescription());
ProgressBar pB = (ProgressBar) convertView.findViewById(R.id.adapterProgressBar);
if (task.getProgressState() != null) {
Drawable color = null;
switch (task.getProgressState()) {
case GOOD:
color = (Drawable) convertView.getResources().getDrawable(R.drawable.progresscolor_good);
break;
case OK:
color = (Drawable) convertView.getResources().getDrawable(R.drawable.progresscolor_ok);
break;
case BAD:
color = (Drawable) convertView.getResources().getDrawable(R.drawable.progresscolor_bad);
break;
}
pB.setProgressDrawable(color);
}
pB.setSecondaryProgress(task.getTargetProgress());
pB.setProgress(task.getProgress());
ImageView iV = (ImageView) convertView.findViewById(R.id.iconProgressAdapter);
// set icons
if (!task.getWorkingStatus()) {
iV.setImageResource(R.drawable.sleeping);
} else {
iV.setImageResource(R.drawable.gear);
}
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}