目录
一.摘要
SortedList继承Object,属于android.utils包工具类,与java.utils包下的List、ArrayList木有什么关系(继承或实现)。一个排序的list实现类保证items选项的顺序,list顺序、items值改变发送提醒通知,比如:RecyclerView.Adapter。比较items之间的位置关系使用compare(Object,Object)方法,该方法采用的是二分查找获取items。如果items排序的标准发生改变,当编辑时未避免数据的不一致需要确保回调合适的方法。SortedList.Callback元素用于控制items的顺序和通知改变。
二.内部类
SortedList嵌套两个内部类SortedList.BatchedCallback、SortedList.Callback,前者用于批量通知被SortedList调度的事件的回调实现,后者控制SortedList的行为。
三.构造方法
SortedList(Class kclass,Callback callback)
创建一个指定类型T的SortedList对象。
SortedList(Class kclass,Callback callback,int initialCapacity)
创建一个指定类型T的SortedList对象,同时指定长度initialCapacity。
四.SortedList方法解析
查看SortedList文档后发现,SortedList可以指定位置更新数据或批量更新,删除指定的item值,数据的操作包括:比较,插入,删除,移动,改变,分别回调对应的方法:compare(),onInserted(),onRemoved(),onMoved(),onChanged(),相比ArrayList,LinkedList,SortedList操作更加方便。创建一个实体SortedListBean,使用SorteList存储实体对象,然后根据id排序,简单的用法:
- /**
- * Created by TeachCourse.cn on 2016/6/25 10:31.
- */
- public class SorteListBean implements Serializable {
- private String id;
- private String name;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public SorteListBean(String id, String name) {
- this.id = id;
- this.name = name;
- }
- }
SortedList在用法上和ArrayList或LinkedList一样,功能上比它们多一些,代码比较多,只粘贴部分
- /**
- * 初始化数据
- */
- private void initData() {
- mSortedList = new SortedList<SorteListBean>(SorteListBean.class, mBatchedCallback);
- mSortedList.add(new SorteListBean("2011110924", "zhaolin"));
- mSortedList.add(new SorteListBean("2011110925", "bocheng"));
- mSortedList.add(new SorteListBean("2011110926", "binge"));
- mSortedList.add(new SorteListBean("2011110927", "boge"));
- mSortedList.add(new SorteListBean("2011110928", "wangzai"));
- }
- /**重写内部类
- *初始化回调内部类
- */
- private SortedList.Callback<SorteListBean> mCallback = new SortedList.Callback<SorteListBean>() {
- @Override
- public int compare(SorteListBean o1, SorteListBean o2) {
- String id1 = o1.getName();
- String id2 = o2.getName();
- return id1.compareTo(id2);
- }
- @Override
- public void onInserted(int position, int count) {
- }
- @Override
- public void onRemoved(int position, int count) {
- }
- @Override
- public void onMoved(int fromPosition, int toPosition) {
- }
- @Override
- public void onChanged(int position, int count) {
- }
- @Override
- public boolean areContentsTheSame(SorteListBean oldItem, SorteListBean newItem) {
- return false;
- }
- @Override
- public boolean areItemsTheSame(SorteListBean item1, SorteListBean item2) {
- return false;
- }
- };
- /**
- * 批量回调
- */
- protected SortedList.BatchedCallback<SorteListBean> mBatchedCallback = new SortedList.BatchedCallback<SorteListBean>(mCallback);
1.add(T item),添加给出的item到list中
- mSortedList.add(new SortedListBean("2011110929","teachcourse.cn"));
2.addAll(T... items),一次性添加多个items到list中
- mSortedList.addAll(new SortedListBean("2011110929","teachcourse.cn"),new SorteListBean("2011110924", "zhaolin"));
3.addAll(Collection
- Collection<SorteListBean> collection=new ArrayList<>();
- collection.add(new SorteListBean("2011110924", "zhaolin"));
- collection.add(new SorteListBean("2011110925", "bocheng"));
- collection.add(new SorteListBean("2011110927", "boge"));
- collection.add(new SorteListBean("2011110928", "wangzai"));
- mSortedList.addAll(collection);
关于Collection的使用讲解可以查看《Collection接口解析》
4.addAll(T[] items,boolean mayModifyInput),添加给出的items到指定的list中,mayModifyInput为true允许修改输入
- SorteListBean[] sorteListBeen=new SorteListBean[]{new SorteListBean("2011110924", "zhaolin"),new SorteListBean("2011110925", "bocheng")};
- mSortedList.addAll(sorteListBeen,true);
5.beginBatchedUpdates(),批量处理适配器更新操作发生在调用当前方法和调用endBatchedUpdates()之间
- mSortedList.beginBatchedUpdates();
- try {
- mSortedList.add(new SorteListBean("2011110924", "zhaolin"))
- mSortedList.add(new SorteListBean("2011110925", "bocheng"))
- mSortedList.remove(new SorteListBean("2011110927", "boge"))
- ...
- } finally {
- mSortedList.endBatchedUpdates();
- }
6.clear(),从SortedList中删除所有的items
- mSortedList.clear();
6.endBatchedUpdates(),结束更新事务,回调处理事件,和beginBatchedUpdates()一起使用
7.get(int index),返回指定索引的item
- SortedListBean bean=mSortedList.get(0);
8.indexOf(T item),返回指定item对应的索引位置
- int index=mSortedList.indexOf(mSortedListBean);
9.recalculatePositionOfItemAt(int index),重新计算指定索引的item位置,当没有触发onChanged(int,int)回调方法时
- final int position = mSortedList.indexOf(item);
- item.incrementPriority(); // assume items are sorted by priority
- mSortedList.recalculatePositionOfItemAt(position);
10.remove(T item),删除指定的item,然后调用onRemoved()方法
- mSortedList.remove(mSortedListBean);
11.removeItemAt(int index),删除指定索引的item,然后调用onRemoved()方法
- mSortedList.removeItemAt(0);
12.updateItemsAt(int index,T item),更新指定索引的item,然后调用onChanged(int,int)方法,onMoved(int,int)方法
- mSortedList.updateItemAt(0,new SorteListBean("2011110927", "boge"));
你可能感兴趣的文章
转载请注明出处: https://www.teachcourse.cn/1870.html ,谢谢支持!