Android开发笔记之:ListView刷新顺序的问题详解

  public class ListOrderActivity extends Activity {

  private ListView mTaskList;

  private EditText mAddTaskEditor;

  private LayoutInflater mFactory;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.list_activity);

  mFactory = LayoutInflater.from(getApplication());

  mTaskList = (ListView) findViewById(R.id.task_list);

  final View headerView = mFactory.inflate(R.layout.header_view, null);

  mTaskList.addHeaderView(headerView);

  mAddTaskEditor = (EditText) headerView.findViewById(R.id.task_editor);

  mAddTaskEditor.setOnKeyListener(new OnKeyListener() {

  @Override

  public boolean onKey(View view, int keycode, KeyEvent event) {

  if (keycode == KeyEvent.KEYCODE_DPAD_CENTER || keycode == KeyEvent.KEYCODE_ENTER) {

  // finish editing

  final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

  inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

  final String text = mAddTaskEditor.getText().toString();

  if (!TextUtils.isEmpty(text)) {

  final ContentValues values = new ContentValues(1);

  values.put(TaskColumns.TASK, text);

  values.put(TaskColumns.TYPE, Task.TYPE_TODAY);

  getContentResolver().insert(Task.CONTENT_URI, values);

  }

  mAddTaskEditor.setText("");

  }

  return false;

  }

  });

  final Cursor cursor = getContentResolver().query(Task.CONTENT_URI, Task.PROJECTION, TaskColumns.TYPE + " = " + Task.TYPE_TODAY, null, null);

  final TaskAdapter adapter = new TaskAdapter(getApplication(), cursor);

  mTaskList.setAdapter(adapter);

  }

  private class TaskAdapter extends CursorAdapter {

  private Cursor mCursor;

  public TaskAdapter(Context context, Cursor c) {

  super(context, c);

  mCursor = c;

  }

  @Override

  public void bindView(View view, Context context, Cursor cursor) {

  if (view == null) {

  view = mFactory.inflate(R.layout.today_task_item, null);

  }

  final ViewSwitcher switcher = (ViewSwitcher) view.findViewById(R.id.action_switcher);

  // if (switcher.getDisplayedChild() == 1) {

  // switcher.clearAnimation();

  // switcher.showPrevious();

  // switcher.clearAnimation();

  // }

  final CheckBox toggle = (CheckBox) view.findViewById(R.id.action_toggle_done);

  final short done = cursor.getShort(ProjectionIndex.DONE);

  final int id = cursor.getInt(ProjectionIndex.ID);

  toggle.setOnCheckedChangeListener(null);

  toggle.setChecked(done != 0);

  toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

  @Override

  public void onCheckedChanged(CompoundButton view, boolean checked) {

  final Uri uri = ContentUris.withAppendedId(Task.CONTENT_URI, id);

  final ContentValues values = new ContentValues(1);

  values.put(TaskColumns.DONE, checked ? 1 : 0);

  getContentResolver().update(uri, values, null, null);

  }

  });

  view.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

  switcher.showNext();

  if (switcher.getDisplayedChild() == 0) {

  switcher.getInAnimation().setAnimationListener(null);

  return;

  }

  final ImageView delete = (ImageView) v.findViewById(R.id.action_delete_task);

  delete.setOnClickListener(new OnClickListener() {

  @Override

  public void onClick(View v) {

  switcher.getInAnimation().setAnimationListener(new AnimationListener() {

  @Override

  public void onAnimationEnd(Animation animation) {

  switcher.getInAnimation().setAnimationListener(null);

  final Uri uri = ContentUris.withAppendedId(Task.CONTENT_URI, id);

  getContentResolver().delete(uri, null, null);

  }

  @Override

  public void onAnimationRepeat(Animation animation) {

  }

  @Override

  public void onAnimationStart(Animation animation) {

  }

  });

  switcher.showPrevious();

  }

  });

  }

  });

  TextView task = (TextView) view.findViewById(R.id.task);

  final String taskContent = cursor.getString(ProjectionIndex.TASK);

  if (done != 0) {

  final Spannable style = new SpannableString(taskContent);

  style.setSpan(new StrikethroughSpan(), 0, taskContent.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

  style.setSpan(new StyleSpan(Typeface.ITALIC) , 0, taskContent.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

  task.setText(style);

  task.setTextAppearance(getApplication(), R.style.done_task_item_text);

  } else {

  task.setText(taskContent);

  task.setTextAppearance(getApplication(), R.style.task_item_text);

  }

  }

  @Override

  public View newView(Context context, Cursor cursor, ViewGroup parent) {

  View view = mFactory.inflate(R.layout.today_task_item, null);

  return view;

  }

  @Override

  public void onContentChanged() {

  mCursor.requery();

  }

  }

  }