Android Recycler Animation View Adapter Filter

I am trying to optimize the filter method for the RecyclerView Adapter in Android. The list is used as an ArrayList. I saw this post , but every time they are filtered from the original list.

Example: if there are 10 results for String 'a', then the user type is 'm', the results of 'am' are a subset of the results of 'a' (results.size () <= 10).

I have three questions to ask in this question,

  1. Is it possible to optimize HashMap memory using ArrayMap? Should I use comma separated positions in String instead of an array of Integer objects, or any way to use an int primitive array?
  2. I do not get animation in this result, how to get it? (I use notifyItemInserted not animation yet)
  3. How much data should be stored in the Hashmap, up to 2 characters or should it fit the size of the result list?
I would be happy to know if it is possible to do something better in this code besides these points.

In the code below, mList is used in the onBindViewHolder method. copyList always contains all the data (no installation or deletion is performed for this).

 class MyFilter extends Filter { /** * 1. check do we have search results available (check map has this key) * 2. if available, remove all rows and add only those which are value for that key (string) * 3. else check do we have any key available starting like this, s=har, already available -ha then it can be reused * * @param constraint */ @Override protected FilterResults performFiltering(CharSequence constraint) { //Here you have to implement filtering way final FilterResults results = new FilterResults(); if (!mSearchMap.containsKey(constraint.toString())) { String supersetKey = getSupersetIfAvailable(mSearchMap, constraint.toString()); if (supersetKey == null) { List<Integer> foundPositions = doFullSearch(copyList, constraint.toString()); mSearchMap.put(constraint.toString(), foundPositions); } else { List<Integer> foundPositions = filterFromSuperset(copyList, mSearchMap.get(supersetKey), constraint.toString()); mSearchMap.put(constraint.toString(), foundPositions); } } return results; } private String getSupersetIfAvailable(Map<String, List<Integer>> mSearchMap, String s) { Set<String> set = mSearchMap.keySet(); List<String> list = new ArrayList<>(set); Collections.sort(list); Collections.reverse(list); for (String c : list) { if (s.startsWith(c)) { return c; } } return null; } private List<Integer> filterFromSuperset(List<WeekWorkBean> list, List<Integer> supersetResults, String s) { List<Integer> results = new ArrayList<>(); String lowerS = s.toLowerCase(); for (int i = 0; i < supersetResults.size(); i++) { if (list.get(supersetResults.get(i)).getEmpName().toLowerCase().startsWith(lowerS)) { results.add(supersetResults.get(i)); } } return results; } private List<Integer> doFullSearch(List<WeekWorkBean> list, String s) { List<Integer> results = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getEmpName().toLowerCase().startsWith(s.toLowerCase())) { results.add(i); } } return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { // here you can use result - (fe set in in adapter list) mList.clear(); notifyDataSetChanged(); List<Integer> res = mSearchMap.get(constraint.toString()); int j = 0; for (Integer i : res) { mList.add(copyList.get(i)); notifyItemInserted(j++); } } } 
+6
source share
3 answers

Check it out https://medium.com/@iammert/using-diffutil-in-android-recyclerview-bdca8e4fbb00#.ehc0gaijt

DiffUtils is what you are looking for. You can use it in the Rx chain to move it from mainThread for big data. here is an example https://medium.com/@nullthemall/diffutil-is-a-must-797502bc1149#.yg35y9q9b

+3
source

To give an answer for your second point, you can try the following:

  notifyItemRangeChanged(pos, ItemList.size()); 
+1
source
  • HashMap is a map, there are also TreeMap, LinkedHashMap and Hashtable. Each of them has its own functions and the interface is Map, not Collection. You can also use other data structures such as Treeset, HashSet, ArrayList, LinkedList, etc. These structures are based on the Set and List interface, which extends to the Collection interface. You can use each of them.

  • If you are inserting an object into your collection, use notifyItemInserted(int position) if you remove any use of the notifyItemRemoved(int position) object if you update any use of the notifyDataSetChanged() object. Be careful with the equality of the length of your collection and the number of views of the adapter.

  • You can store parameters on maps as long as you want. There are no restrictions. But you have to choose the best collection for you, install, list or display.

+1
source

Source: https://habr.com/ru/post/1012831/


All Articles