1

在我的应用程序中,我有一个项目列表,我通过 LoaderManager 获取项目。当我选择或取消选择项目时,它无法正常工作,我的意思是有时不会选择所有项目。也许它会在 onLoadFinished 方法中将缓存值返回给适配器。有人有想法吗?

@EFragment(R.layout.fragment_mailbox)
public class MyFragment extends ListFragment implements SearchView.OnQueryTextListener,
    LoaderManager.LoaderCallbacks<LazyList<Item>> {


@AfterViews
public void afterViews() {
    broadcastReceiver = new MailboxEmailBroadcastReceiver(this);

    setEmptyText(getString(R.string.fragment_mailbox_list_empty));

    adapter = new ItemListAdapter(getActivity(), mailAccountIdExtra, folderIdExtra);

    setListAdapter(adapter);
    setListShown(false);

    getLoaderManager().initLoader(1, null, this);        
}

@Override
public Loader<LazyList<Item>> onCreateLoader(int id, Bundle bundle) {
    ...
}

@Override
public void onLoadFinished(Loader<LazyList<Item>> listLoader, LazyList<Item> items) {
    adapter.swapList(items);
    adapter.notifyDataSetChanged();
    hideLoader();
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}    

private void selectOrDeselectItems(MenuItem item) {
    adapter.selectAllItems((isSelectAllToggle) ? false : true);        
}

}

这是我的适配器。

public class ItemListAdapter extends BaseAdapter {

@Override
public View getView(int i, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    final Item item = items.get(i);        

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.layout, parent, false);
        viewHolder = new ViewHolder();            
        viewHolder.lineView       = (ImageView) convertView.findViewById(R.id.line_view);            
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    if(item.isChecked()) {
        viewHolder.lineView.setVisibility(View.VISIBLE);
    } else
        viewHolder.lineView.setVisibility(View.INVISIBLE);

    return convertView;
}


public void swapList(List<Item> items) {
    this.items = items;
}

public List<Item> getList () {
    return items;
}   

public void selectAllItems(boolean isSelectAll) {      
    for (Item item: items) {
        item.setChecked(isSelectAll);            
    }
}


static class ViewHolder {        
    ImageView lineView;
}

}

4

1 回答 1

1

更新项目后调用 notifyDataSetChanged 刷新视图:

public void selectAllItems(boolean isSelectAll) {      
    for (Item item: items) {
        item.setChecked(isSelectAll);            
    }
    notifyDataSetChanged();
}
于 2015-02-16T09:11:02.800 回答