Przeglądaj źródła

列表更新数据优化

zengjiebin 7 lat temu
rodzic
commit
93df1a2391

+ 53 - 16
app/src/main/java/com/sheep/gamegroup/util/ListUtil.java

@@ -21,6 +21,7 @@ public class ListUtil {
     public static <T> T getItem(List<T> list, int index) {
         return hasIndex(list, index) ? list.get(index) : null;
     }
+
     public static <T> T getLast(List<T> list) {
         int size = ListUtil.size(list);
         return size > 0 ? list.get(-1 + size) : null;
@@ -35,7 +36,7 @@ public class ListUtil {
         return new ArrayList<>();
     }
 
-    public static <R,V> int addAll(Conversion<R, V> conversion, List<V> list, R...elements) {
+    public static <R, V> int addAll(Conversion<R, V> conversion, List<V> list, R... elements) {
         int length = elements.length;
         for (int i = 0; i < length; i++) {
             V v = conversion.convert(elements[i], i);
@@ -43,7 +44,8 @@ public class ListUtil {
         }
         return length;
     }
-    public static <R,V> int addAll(Conversion<R, V> conversion, List<V> list, List<R> elements) {
+
+    public static <R, V> int addAll(Conversion<R, V> conversion, List<V> list, List<R> elements) {
         int size = elements.size();
         for (int i = 0; i < elements.size(); i++) {
             V v = conversion.convert(elements.get(i), i);
@@ -51,15 +53,17 @@ public class ListUtil {
         }
         return size;
     }
-    public static <T> int addAll(List<T> list, T...elements) {
+
+    public static <T> int addAll(List<T> list, T... elements) {
         return addAll(list, asList(elements));
     }
+
     public static <T> int addAll(List<T> list, List<? extends T> elements) {
         int count = 0;
-        if(list == null){
+        if (list == null) {
             list = emptyList();
         }
-        if(elements != null) {
+        if (elements != null) {
             for (T element : elements) {
                 int index = list.indexOf(element);
                 if (index != -1) {
@@ -71,12 +75,13 @@ public class ListUtil {
         }
         return count;
     }
+
     public static <T> int addAll(List<T> list, List<? extends T> elements, int maxCount) {
         int count = 0;
-        if(list == null){
+        if (list == null) {
             list = emptyList();
         }
-        if(elements != null) {
+        if (elements != null) {
             for (T element : elements) {
                 int index = list.indexOf(element);
                 if (index != -1) {
@@ -84,7 +89,37 @@ public class ListUtil {
                 } else if (list.add(element)) {
                     count++;
                 }
-                if(count >= maxCount)//最多只加maxCount条数据
+                if (count >= maxCount)//最多只加maxCount条数据
+                    break;
+            }
+        }
+        return count;
+    }
+
+    public static <T> int addAllItem(List<T> list, List<? extends T> elements) {
+        int count = 0;
+        if (list == null) {
+            list = emptyList();
+        }
+        if (elements != null) {
+            for (T element : elements) {
+                list.add(element);
+                count++;
+            }
+        }
+        return count;
+    }
+
+    public static <T> int addAllItem(List<T> list, List<? extends T> elements, int maxCount) {
+        int count = 0;
+        if (list == null) {
+            list = emptyList();
+        }
+        if (elements != null) {
+            for (T element : elements) {
+                list.add(element);
+                count++;
+                if (count >= maxCount)//最多只加maxCount条数据
                     break;
             }
         }
@@ -100,13 +135,14 @@ public class ListUtil {
     }
 
     @SafeVarargs
-    public static <T> ArrayList<T> asList(T...items) {
+    public static <T> ArrayList<T> asList(T... items) {
         ArrayList<T> list = emptyList();
         Collections.addAll(list, items);
         return list;
     }
+
     @SafeVarargs
-    public static <T,R> ArrayList<R> asList(CallBack<T, R> callBack, T...items) {
+    public static <T, R> ArrayList<R> asList(CallBack<T, R> callBack, T... items) {
         ArrayList<R> list = emptyList();
         for (T t : items) {
             R r = callBack.call(t);
@@ -116,12 +152,12 @@ public class ListUtil {
     }
 
     public static <T> List<T> removeItem(List<T> list, CallBack<T, Boolean> callBack) {
-        if(isEmpty(list)){
+        if (isEmpty(list)) {
             return null;
         }
         List<T> removeList = emptyList();
         for (T t : list) {
-            if(callBack.call(t))
+            if (callBack.call(t))
                 removeList.add(t);
         }
         list.removeAll(removeList);
@@ -129,12 +165,13 @@ public class ListUtil {
     }
 
     public static <T> void set(List<T> list, T t, int position) {
-        if(hasIndex(list, position))
+        if (hasIndex(list, position))
             list.set(position, t);
     }
 
     /**
      * 清除末尾的空数据
+     *
      * @param list
      */
     public static <T> void removeNull(List<T> list) {
@@ -146,12 +183,12 @@ public class ListUtil {
         });
     }
 
-    public static <T>  void removeAll(List<T> allList, List<T> removeList) {
-        if(allList != null && removeList != null)
+    public static <T> void removeAll(List<T> allList, List<T> removeList) {
+        if (allList != null && removeList != null)
             allList.removeAll(removeList);
     }
 
-    public static interface CallBack<I, R>{
+    public static interface CallBack<I, R> {
         public R call(I i);
     }
 }

+ 4 - 5
app/src/main/java/com/sheep/gamegroup/view/activity/BaseListActivity.java

@@ -103,7 +103,7 @@ public abstract class BaseListActivity<T> extends BaseActivity {
     }
     public void initData() {
         final String urlKey = getKey(page, per_page);
-        if(isFirstGetACache() && page == 1) {
+        if(isFirstGetACache()) {
             //先尝试获取缓存数据
             lastCacheList = DataUtil.getInstance().getCacheList(urlKey, getTClass());
             loadList(lastCacheList);
@@ -129,7 +129,8 @@ public abstract class BaseListActivity<T> extends BaseActivity {
                         lastMessage = baseMessage;
                         boolean isNewData = DataUtil.getInstance().isNewData(urlKey);
                         if(isNewData || !isFirstGetACache()) {
-                            list.removeAll(lastCacheList);
+                            if(isFirstGetACache())
+                                ListUtil.removeAll(list, lastCacheList);
                             List<T> newList = baseMessage.getDatas(getTClass());
                             loadList(newList);
                         }else {
@@ -154,9 +155,7 @@ public abstract class BaseListActivity<T> extends BaseActivity {
         initData();
     }
     private void loadList(List<T> newList){
-        if(!isFirstGetACache() && page == 1)
-            list.clear();
-        ListUtil.addAll(list, newList);
+        ListUtil.addAllItem(list, newList);
         notifyDataSetChanged();
     }
     protected void notifyDataSetChanged(){

+ 3 - 4
app/src/main/java/com/sheep/gamegroup/view/fragment/BaseListFragment.java

@@ -162,7 +162,8 @@ public abstract class BaseListFragment<T> extends BaseFragment {
                         lastMessage = baseMessage;
                         boolean isNewData = DataUtil.getInstance().isNewData(urlKey);
                         if(isNewData || !isFirstGetACache()) {
-                            ListUtil.removeAll(list, lastCacheList);
+                            if(isFirstGetACache())
+                                ListUtil.removeAll(list, lastCacheList);
                             List<T> newList = baseMessage.getDatas(getTClass());
                             loadList(newList);
                         }else {
@@ -207,9 +208,7 @@ public abstract class BaseListFragment<T> extends BaseFragment {
         initData();
     }
     private void loadList(List<T> newList){
-        if(!isFirstGetACache() && page == 1)
-            list.clear();
-        ListUtil.addAll(list, newList);
+        ListUtil.addAllItem(list, newList);
         notifyDataSetChanged();
     }
     public void notifyDataSetChanged(){

+ 4 - 4
app/src/main/java/com/sheep/gamegroup/view/fragment/FgtTryMakeMoney.java

@@ -470,11 +470,11 @@ public class FgtTryMakeMoney extends BaseFragment implements TryMakeMoneyContrac
             switch (about_to_begin) {
                 case 0:
                     ListUtil.removeAll(releaseEtyLists, lastCacheReleaseEtyLists);
-                    ListUtil.addAll(releaseEtyLists, releaseEtyList);
+                    ListUtil.addAllItem(releaseEtyLists, releaseEtyList);
                     break;
                 case 1:
                     ListUtil.removeAll(releaseEtyListsBegin, lastCacheReleaseEtyLists);
-                    ListUtil.addAll(releaseEtyListsBegin, releaseEtyList);
+                    ListUtil.addAllItem(releaseEtyListsBegin, releaseEtyList);
                     break;
             }
         }
@@ -512,11 +512,11 @@ public class FgtTryMakeMoney extends BaseFragment implements TryMakeMoneyContrac
             switch (about_to_begin) {
                 case 0:
                     lastCacheReleaseEtyLists = cacheTaskReleaseEtyList;
-                    ListUtil.addAll(releaseEtyLists, cacheTaskReleaseEtyList);
+                    ListUtil.addAllItem(releaseEtyLists, cacheTaskReleaseEtyList);
                     break;
                 case 1:
                     lastCacheReleaseEtyListsBegin = cacheTaskReleaseEtyList;
-                    ListUtil.addAll(releaseEtyListsBegin, cacheTaskReleaseEtyList);
+                    ListUtil.addAllItem(releaseEtyListsBegin, cacheTaskReleaseEtyList);
                     break;
             }
             notifyDataSetChanged();

+ 1 - 1
app/src/main/res/layout/act_audit_layout.xml

@@ -11,7 +11,7 @@
         android:layout_height="wrap_content"
         android:background="@mipmap/audit_top_bg"
         android:paddingBottom="@dimen/content_padding_15"
-        android:layout_marginTop="5dp">
+        android:paddingTop="5dp">
 
 
         <ImageView