8

にいくつかのアイテムがありRecyclerView、各アイテムにはlong値が保存されています。のアダプターとしてFastAdapterを使用していRecyclerViewます。

RecyclerViewに long 値を持つ7 つの項目があるとします: 1112212321-988118870-88009、。3398-22113

だから、私がやりたいことは、このロジックを使用して、上記の長い値に基づいてアイテムをフィルタリングしたいということです:

if (l <= 1000) {
  // show items with long value <=1000
} else if (l > 1000) {
  // show items with long value >1000
}

いろいろ試してみましたが、何もうまくいきませんでした。

更新 1CardView : ここの項目は、 に保存され、 に表示される一種の異なるデータRecyclerViewです。各カードには異なるデータが含まれており、そのうちの 1 つが上記のlong値です。long上記のロジックに基づいて、各カードに保存されているこれらの値に基づいてデータをフィルター処理したいと考えています。

この問題を解決して、これを達成できるアルゴリズムまたはコードを提案してください。

4

5 に答える 5

6

l与えられた情報量では、内部に表示されるアイテムを制御する外部セレクター値であるとしか思えませんRecyclerView。そうでない場合は、以下にコメントしてください。答えを修正しようとします。

custom を実装し、それぞれのメソッドを使用してViewAdapterアイテムのリストとセレクター変数を送信することをお勧めします。l

public class ItemsAdapter extends 
    RecyclerView.Adapter<ItemsAdapter.ItemViewHolder> {

    private List<Long> mItemList;
    private List<Long> mDisplayItems;
    private boolean mAboveThousand = true;

    public void setItemList(List<Long> list) {
        mItemList = list;
        updateDisplayItems();
    }

    public void setSelectionType(boolean aboveThousand) {
        mAboveThousand = aboveThousand;
        updateDisplayItems();
    }

    private updateDisplayItems() {
        mDisplayItems.clear();

        for(Long item: mItemList) {
            if(/*check your contition*/) {
                mDisplayItems.add(item);
            }
        }

        notifyDataSetChanged(); //important
    }

    ...
    // Rest of implementation
}

また、FastAdapter を使用したことはありませんが、クラスを拡張する場合にオーバーライドするメソッドがいくつかあるはずです。

アップデート

の使用の基本を理解するのに問題があるため、ライブラリを使用する前にViewAdapterカスタムを学習して実装することをお勧めします。RecyclerView の実装方法に関する詳細なチュートリアルを次に示します。ViewAdapterViewAdapter

ViewAdapter を実装したら、コードを使用してカードを除外できます。基本的に、コードが行っていることは、必要なすべてのデータのリストを 内mItemListmDisplayList保存することですが、 は表示する項目を格納するリストであり、毎回更新され、mAboveThousand1000 以上または 1000 以下のユーザー設定を格納するように設定されています。これmDisplayListを使用して、RecyclerView 内のデータを膨張させる必要があります。

于 2017-01-01T09:13:09.803 に答える
4

そこにある非常に基本的なコードでさえ機能します。その範囲内のアイテムの数を数えて、その範囲内の数を返すことができます。フィルター値に基づいてデータを解析するというコア コンセプトは完全に堅固であるため、FastAdapter を使用せずにこれを実行することをお勧めします。ループを繰り返してそれらを数えることができ、ループを繰り返して n 番目のアイテムを返すことができます。

于 2017-01-01T09:54:32.203 に答える
4

If you do want to keep using FastAdapter, it has a built-in filter functionality (see point number 5 in the README of the project. Note that the filter method should be called after withFilterPredicate and not before as shown there).

EDIT - after you pointed out that I misunderstood you before - here is my updated proposed instructions:

You need to resolve the logics of which set you want to display (using the checkboxes in the dialog you mentioned in the comment) and pass that information onto the filter, for example:

boolean displayUnderThreshold = //put the logic here - true if you want <1000
fastAdapter.filter(Boolean.toString(displayUnderThreshold));

And where you set the adapter (before the above line is called) have:

final long threshold = 1000;
fastAdapter.withFilterPredicate(new IItemAdapter.Predicate<GRModeClass>() {
    @Override
    public boolean filter(GRModeClass item, CharSequence constraint) {
        boolean displayUnderThreshold = new Boolean(constraint.toString());
        return (displayUnderThreshold ^ (item.l<threshold)); //false to remove from list
    }
});

Old answer

From when I thought you wanted to filter the items according to their ms long values, using an external l long indicator:

In your code, assuming your app does get to the if you mentioned in the question when it should - remove the fastItemAdapter.clear(); and instead of the for loop with the if inside it write

fastItemAdapter.filter(Long.toString(l));

and somewhere before that, preferably where you set the adapter (most likely in the onCreate of MainActivity) add the following:

final long threshold = 1000;
fastAdapter.withFilterPredicate(new IItemAdapter.Predicate<GRModeClass>() {
    @Override
    public boolean filter(GRModeClass item, CharSequence constraint) {
        long indicator = new Long(constraint.toString());
        return (item.ms<threshold && indicator>=threshold) || (item.ms>=threshold && indicator<threshold) ;
    }
});

(Assuming here that GRModeClass is your items' class and that the long ms is the long you referred to that should determine whether the )

于 2017-01-03T08:54:19.557 に答える
3

私はあなたのクラスが似ていると思います

public Class ListItem {
    // .. Some other attributes
    public long l;
}

にフィルターを配置するときに呼び出される関数があることを願っていますRecyclerView。関数名を とするtoggleFilter

public void toggleFilter(long l) {
    if(l <= 1000) {
        fastAdapter.withFilterPredicate(new IItemAdapter.Predicate<Item>() {
            @Override
            public boolean filter(ListItem item, CharSequence constraint) {
                if(item.l <= 1000) return true;
                else return false; 
            }
        });

    } else if (l > 1000) {
        fastAdapter.withFilterPredicate(new IItemAdapter.Predicate<Item>() {
            @Override
            public boolean filter(ListItem item, CharSequence constraint) {
                if(item.l > 1000) return true;
                else return false; 
            }
        });
    }

    // Finally call notifyDataSetChanged        
    fastAdapter.notifyDataSetChanged();
}
于 2017-01-06T15:17:34.233 に答える