samples/Support7Demos/src/main/java/com/example/android/supportv7/widget/selection/simple/SimpleSelectionDemoActivity.java
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.selection_demo_layout);
        RecyclerView recView = (RecyclerView) findViewById(R.id.list);

        // keyProvider depends on mAdapter.setHasStableIds(true).
        ItemKeyProvider<Long> keyProvider = new StableIdKeyProvider(recView);

        mLayout = new GridLayoutManager(this, mColumnCount);
        recView.setLayoutManager(mLayout);

        mAdapter = new SimpleSelectionDemoAdapter(this, keyProvider);
        // The adapter is paired with a key provider that supports
        // the native RecyclerView stableId. For this to work correctly
        // the adapter must report that it supports stable ids.
        mAdapter.setHasStableIds(true);

        recView.setAdapter(mAdapter);

        SelectionTracker.Builder<Long> builder = new SelectionTracker.Builder<>(
                "simple-demo",
                recView,
                keyProvider,
                new DemoDetailsLookup(recView),
                StorageStrategy.createLongStorage());

        // Override default behaviors and build in multi select mode.
        // Call .withSelectionPredicate(SelectionTracker.SelectionPredicate.SINGLE_ANYTHING)
        // for single selection mode.
        mSelectionTracker = builder
                .withSelectionPredicate(SelectionPredicates.createSelectAnything())
                .withOnDragInitiatedListener(new OnDragInitiatedListener(this))
                .withOnContextClickListener(new OnContextClickListener(this))
                .withOnItemActivatedListener(new OnItemActivatedListener(this))
                .withFocusDelegate(new FocusDelegate(this))
                .withBandOverlay(R.drawable.selection_demo_band_overlay)
                .build();

        // Lazily bind SelectionTracker. Allows us to defer initialization of the
        // SelectionTracker dependency until after the adapter is created.
        mAdapter.bindSelectionHelper(mSelectionTracker);

        // TODO: Glue selection to ActionMode, since that'll be a common practice.
        mSelectionTracker.addObserver(
                new SelectionObserver<Long>() {
                    @Override
                    public void onSelectionChanged() {
                        Log.i(TAG, "Selection changed to: " + mSelectionTracker.getSelection());
                    }
                });

        // Restore selection from saved state.
        updateFromSavedState(savedInstanceState);
    }
// This will get updated when layout changes.
This will get updated when layout changes.
This will get updated when layout changes .
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selection_demo_layout);
        RecyclerView recView = (RecyclerView) findViewById(R.id.list);
        ItemKeyProvider<Long> keyProvider = new StableIdKeyProvider(recView);
        mLayout = new GridLayoutManager(this, mColumnCount);
        recView.setLayoutManager(mLayout);
        mAdapter = new SimpleSelectionDemoAdapter(this, keyProvider);
        mAdapter.setHasStableIds(true);
        recView.setAdapter(mAdapter);
        SelectionTracker.Builder<Long> builder = new SelectionTracker.Builder<>(
                "simple-demo",
                recView,
                keyProvider,
                new DemoDetailsLookup(recView),
                StorageStrategy.createLongStorage());
        mSelectionTracker = builder
                .withSelectionPredicate(SelectionPredicates.createSelectAnything())
                .withOnDragInitiatedListener(new OnDragInitiatedListener(this))
                .withOnContextClickListener(new OnContextClickListener(this))
                .withOnItemActivatedListener(new OnItemActivatedListener(this))
                .withFocusDelegate(new FocusDelegate(this))
                .withBandOverlay(R.drawable.selection_demo_band_overlay)
                .build();
        mAdapter.bindSelectionHelper(mSelectionTracker);
        mSelectionTracker.addObserver(
                new SelectionObserver<Long>() {
                    @Override
                    public void onSelectionChanged() {
                        Log.i(TAG, "Selection changed to: " + mSelectionTracker.getSelection());
                    }
                });
        updateFromSavedState(savedInstanceState);
    }
@ Override protected void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; setContentView ( R . layout . selection_demo_layout ) ; RecyclerView recView = ( RecyclerView ) findViewById ( R . id . list ) ; ItemKeyProvider < Long > keyProvider = new StableIdKeyProvider ( recView ) ; mLayout = new GridLayoutManager ( this , mColumnCount ) ; recView . setLayoutManager ( mLayout ) ; mAdapter = new SimpleSelectionDemoAdapter ( this , keyProvider ) ; mAdapter . setHasStableIds ( true ) ; recView . setAdapter ( mAdapter ) ; SelectionTracker . Builder < Long > builder = new SelectionTracker . Builder < > ( "simple-demo" , recView , keyProvider , new DemoDetailsLookup ( recView ) , StorageStrategy . createLongStorage ( ) ) ; mSelectionTracker = builder . withSelectionPredicate ( SelectionPredicates . createSelectAnything ( ) ) . withOnDragInitiatedListener ( new OnDragInitiatedListener ( this ) ) . withOnContextClickListener ( new OnContextClickListener ( this ) ) . withOnItemActivatedListener ( new OnItemActivatedListener ( this ) ) . withFocusDelegate ( new FocusDelegate ( this ) ) . withBandOverlay ( R . drawable . selection_demo_band_overlay ) . build ( ) ; mAdapter . bindSelectionHelper ( mSelectionTracker ) ; mSelectionTracker . addObserver ( new SelectionObserver < Long > ( ) { @ Override public void onSelectionChanged ( ) { Log . i ( TAG , "Selection changed to: " + mSelectionTracker . getSelection ( ) ) ; } } ) ; updateFromSavedState ( savedInstanceState ) ; }
This will get updated when layout changes.
This will get updated when layout changes .
// keyProvider depends on mAdapter.setHasStableIds(true). // The adapter is paired with a key provider that supports // the native RecyclerView stableId. For this to work correctly // the adapter must report that it supports stable ids. // Override default behaviors and build in multi select mode. // Call .withSelectionPredicate(SelectionTracker.SelectionPredicate.SINGLE_ANYTHING) // for single selection mode. // Lazily bind SelectionTracker. Allows us to defer initialization of the // SelectionTracker dependency until after the adapter is created. // TODO: Glue selection to ActionMode, since that'll be a common practice. // Restore selection from saved state.