6

AndroidでRecyclerViewアダプターのフィルターメソッドを最適化しようとしています。リストはArrayListとして使用されます。私はこれを見たpostしかし、彼らは元のリストから毎回フィルタリングされています。Android Recyclerビューアダプターフィルター(アニメーションあり)

例:文字列 'a'の結果が「m」の場合、「am」の結果は「a」結果のサブセットです(results.size()< = 10)。

この質問では3つの点があります。

  1. ArrayMapを使用してHashMapメモリを最適化することはできますか? Integerオブジェクト配列の代わりにStringのコンマ区切り位置を使うべきか、またはintプリミティブ配列を使う方法はありますか?
  2. 私はこの結果でアニメーションを取得していません、それを取得する方法は? (私はnotifyItemInsertedまだアニメーションを使用していません)
  3. 2文字まで、または結果リストのサイズに応じて、データをHashmapに保存する必要がありますか?
これらの点以外でこのコードで何か良いことができるかどうか知ってうれしいです。

以下のコードでは、mListがonBindViewHolderメソッドで使用されています。 copyListには常にすべてのデータが含まれます(挿入も削除も行われません)。

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 - (f.e. 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++); 
      } 
     } 
    } 
+0

は私の答えをチェックし、DiffUtils私はHashMapのメモリはArrayMapを使用することによって最適化することができ、あなたが –

+0

、これら2点を探しているものでしょうか? Integerオブジェクト配列の代わりにStringのコンマ区切り位置を使うべきか、またはintプリミティブ配列を使う方法はありますか? ハッシュマップに保持するデータ量は2文字までか、結果リストのサイズに従うべきですか? –

+0

@wojciech_maciejewskiあなたの答えは有用で、私はupvoteを与えました。それは完全な質問に答えるものではありません –

答えて

3

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

DiffUtilsこれをチェックしてくださいあなたが探しているものです。あなたはRxチェーンでそれを使ってmainThreadからそれを移動させ、大きなデータを得ることができます。ここ あなたはこれを試すことができ、あなたの第二のポイントのための答えを与えるために例 https://medium.com/@nullthemall/diffutil-is-a-must-797502bc1149#.yg35y9q9b

1

です:

notifyItemRangeChanged(pos, ItemList.size()); 
1
  1. HashMapのは地図で、TreeMapの、のLinkedHashMapとハッシュテーブルもあります。それぞれに独自の機能があり、インタフェースはMapであり、Collectionではありません。 Treeset、HashSet、ArrayList、LinkedListなどの他のデータ構造を使用することもできます。これらの構造体は、Collectionインタフェースに拡張されたSetおよびListインタフェースから得られます。あなたはそれぞれを使うことができます。

  2. あなたは任意のオブジェクトを使用notifyItemRemoved(int position)を削除する場合は、任意のオブジェクトを使用notifyDataSetChanged()を更新した場合、notifyItemInserted(int position)を使用し、あなたのコレクションに任意のオブジェクトを挿入した場合。コレクションの長さとアダプターのビュー数が等しくなるように注意してください。

  3. パラメータをマップに保存することができます。制限はありません。しかし、あなたのために最高のコレクションを選択して、セット、リスト、またはマップする必要があります。

関連する問題