Java Code Examples for androidx.recyclerview.widget.ItemTouchHelper#Callback

The following examples show how to use androidx.recyclerview.widget.ItemTouchHelper#Callback . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BookmarksActivity.java    From SimplicityBrowser with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    if(UserPreferences.getBoolean("dark_mode", false)){
        setTheme(R.style.BookMarksThemeDark);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bookmarks);
    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    mToolbar = findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        Drawable drawable = mToolbar.getNavigationIcon();
        if (drawable != null) {
            drawable.setColorFilter(ContextCompat.getColor(this, R.color.grey_color), PorterDuff.Mode.SRC_ATOP);
        }
    }
    listBookmarks = UserPreferences.getBookmarks();
    recyclerBookmarks = findViewById(R.id.recycler_bookmarks);
    recyclerBookmarks.setLayoutManager(new LinearLayoutManager(this));
    adapterBookmarks = new AdapterBookmarks(this, listBookmarks, this, this);
    recyclerBookmarks.setAdapter(adapterBookmarks);
    ItemTouchHelper.Callback callback = new TouchHelperCallback(adapterBookmarks);
    mItemTouchHelper = new ItemTouchHelper(callback);
    mItemTouchHelper.attachToRecyclerView(recyclerBookmarks);

    //show sort hint
    if (preferences.getBoolean("first_bookmarks", true) && !listBookmarks.isEmpty()) {
        Cardbar.snackBar(getApplicationContext(), getString(R.string.bookmarks_hint), true).show();
        preferences.edit().putBoolean("first_bookmarks", false).apply();
    }


}
 
Example 2
Source File: RecentlyWatchedFragment.java    From YouTube-In-Background with MIT License 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_recently_watched, container, false);

    recyclerView = rootView.findViewById(R.id.recently_played);
    recyclerView.setLayoutManager(getLayoutManager());
    recyclerView.addItemDecoration(getItemDecoration());

    recyclerView.getItemAnimator().setAddDuration(500);
    recyclerView.getItemAnimator().setChangeDuration(500);
    recyclerView.getItemAnimator().setMoveDuration(500);
    recyclerView.getItemAnimator().setRemoveDuration(500);

    videoListAdapter = getAdapter();
    videoListAdapter.setOnItemClickListener(this);
    videoListAdapter.setOnStartDragListener(this);
    recyclerView.setAdapter(videoListAdapter);

    ItemTouchHelper.Callback callback = new SimpleItemTouchCallback(videoListAdapter);
    itemTouchHelper = new ItemTouchHelper(callback);
    itemTouchHelper.attachToRecyclerView(recyclerView);

    return rootView;
}
 
Example 3
Source File: HistoryFragment.java    From mimi-reader with Apache License 2.0 5 votes vote down vote up
private ItemTouchHelper.Callback createTouchHelperCallback() {
    return new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT) {
        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }

        @Override
        public boolean isItemViewSwipeEnabled() {
            return false;
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder from, RecyclerView.ViewHolder to) {
            HistoryAdapter adapter;
            if (recyclerView.getAdapter() instanceof HistoryAdapter) {
                adapter = (HistoryAdapter) recyclerView.getAdapter();
                adapter.swapItems(from.getAdapterPosition(), to.getAdapterPosition());
                return true;
            }

            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int i) {

        }
    };
}
 
Example 4
Source File: RecyclerViewOverScrollDecorAdapter.java    From overscroll-decor with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void setUpTouchHelperCallback(final ItemTouchHelper.Callback itemTouchHelperCallback) {
    new ItemTouchHelper(new ItemTouchHelperCallbackWrapper(itemTouchHelperCallback) {
        @Override
        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
            mIsItemTouchInEffect = actionState != 0;
            super.onSelectedChanged(viewHolder, actionState);
        }
    }).attachToRecyclerView(mRecyclerView);
}
 
Example 5
Source File: OrderableVideosGridFragment.java    From SkyTube with GNU General Public License v3.0 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	View view = super.onCreateView(inflater, container, savedInstanceState);
	if(videoGridAdapter instanceof OrderableVideoGridAdapter) {

		ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback((OrderableVideoGridAdapter)videoGridAdapter);
		ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
		touchHelper.attachToRecyclerView(gridView);
	}
	return view;
}
 
Example 6
Source File: ApplicationsDialogPreferenceFragmentX.java    From PhoneProfilesPlus with Apache License 2.0 5 votes vote down vote up
@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);

    AppCompatImageButton addButton = view.findViewById(R.id.applications_pref_dlg_add);
    TooltipCompat.setTooltipText(addButton, getString(R.string.applications_pref_dlg_add_button_tooltip));

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    applicationsListView = view.findViewById(R.id.applications_pref_dlg_listview);
    //applicationsListView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
    applicationsListView.setLayoutManager(layoutManager);
    applicationsListView.setHasFixedSize(true);

    linlaProgress = view.findViewById(R.id.applications_pref_dlg_linla_progress);
    rellaDialog = view.findViewById(R.id.applications_pref_dlg_rella_dialog);

    listAdapter = new ApplicationsDialogPreferenceAdapterX(prefContext, preference, this);

    // added touch helper for drag and drop items
    ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(listAdapter, false, false);
    itemTouchHelper = new ItemTouchHelper(callback);
    itemTouchHelper.attachToRecyclerView(applicationsListView);

    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            preference.startEditor(null);
        }
    });

    refreshListView(false);
}
 
Example 7
Source File: ShadowRecyclerView.java    From materialistic with Apache License 2.0 4 votes vote down vote up
void setItemTouchHelperCallback(ItemTouchHelper.Callback callback) {
    itemTouchHelperCallback = callback;
}
 
Example 8
Source File: RefreshRecyclerView.java    From a with GNU General Public License v3.0 4 votes vote down vote up
public void setItemTouchHelperCallback(ItemTouchHelper.Callback callback) {
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
    itemTouchHelper.attachToRecyclerView(recyclerView);
}
 
Example 9
Source File: ShadowRecyclerView.java    From materialistic with Apache License 2.0 4 votes vote down vote up
public ItemTouchHelper.Callback getItemTouchHelperCallback() {
    return itemTouchHelperCallback;
}
 
Example 10
Source File: RecyclerViewOverScrollDecorAdapter.java    From overscroll-decor with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public RecyclerViewOverScrollDecorAdapter(RecyclerView recyclerView, ItemTouchHelper.Callback itemTouchHelperCallback) {
    this(recyclerView);
    setUpTouchHelperCallback(itemTouchHelperCallback);
}
 
Example 11
Source File: NotificationRulesAdapter.java    From revolution-irc with GNU General Public License v3.0 4 votes vote down vote up
public void enableDragDrop(RecyclerView recyclerView) {
    ItemTouchHelper.Callback callback = new MyItemTouchHelperCallback(recyclerView.getContext());
    mItemTouchHelper = new ItemTouchHelper(callback);
    mItemTouchHelper.attachToRecyclerView(recyclerView);
}
 
Example 12
Source File: RefreshRecyclerView.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
public void setItemTouchHelperCallback(ItemTouchHelper.Callback callback) {
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
    itemTouchHelper.attachToRecyclerView(recyclerView);
}
 
Example 13
Source File: RefreshRecyclerView.java    From MyBookshelf with GNU General Public License v3.0 4 votes vote down vote up
public void setItemTouchHelperCallback(ItemTouchHelper.Callback callback) {
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
    itemTouchHelper.attachToRecyclerView(recyclerView);
}
 
Example 14
Source File: EditorProfileListFragment.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
@Override
protected void onPostExecute(Void response) {
    super.onPostExecute(response);
    
    EditorProfileListFragment fragment = fragmentWeakRef.get();
    
    if ((fragment != null) && (fragment.isAdded())) {
        progressBarHandler.removeCallbacks(progressBarRunnable);
        fragment.progressBar.setVisibility(GONE);

        // get local profileList
        _dataWrapper.fillProfileList(true, applicationEditorPrefIndicator);
        // set local profile list into activity dataWrapper
        fragment.activityDataWrapper.copyProfileList(_dataWrapper);

        synchronized (fragment.activityDataWrapper.profileList) {
            if (fragment.activityDataWrapper.profileList.size() == 0)
                fragment.textViewNoData.setVisibility(View.VISIBLE);
        }

        fragment.profileListAdapter = new EditorProfileListAdapter(fragment, fragment.activityDataWrapper, _filterType, fragment);

        // added touch helper for drag and drop items
        ItemTouchHelper.Callback callback = new ItemTouchHelperCallback(fragment.profileListAdapter, false, false);
        fragment.itemTouchHelper = new ItemTouchHelper(callback);
        fragment.itemTouchHelper.attachToRecyclerView(fragment.listView);

        fragment.listView.setAdapter(fragment.profileListAdapter);

        // update activity for activated profile
        //fragment.listView.getRecycledViewPool().clear();

        Profile profile = fragment.activityDataWrapper.getActivatedProfile(true,
                        applicationEditorPrefIndicator);
        fragment.updateHeader(profile);
        fragment.profileListAdapter.notifyDataSetChanged(false);
        //if (!ApplicationPreferences.applicationEditorHeader(_dataWrapper.context))
        //    fragment.setProfileSelection(profile);

        if (defaultProfilesGenerated) {
            //PPApplication.logE("ActivateProfileHelper.updateGUI", "from EditorProfileListFragment.LoadProfileListAsyncTask");
            //PPApplication.logE("###### PPApplication.updateGUI", "from=EditorProfileListFragment.LoadProfileListAsyncTask.onPostExecute");
            PPApplication.updateGUI(true/*_dataWrapper.context, true, true*/);
            if ((fragment.getActivity() != null) && (!fragment.getActivity().isFinishing()))
                PPApplication.showToast(_dataWrapper.context.getApplicationContext(),
                        fragment.getResources().getString(R.string.toast_predefined_profiles_generated),
                        Toast.LENGTH_SHORT);
        }
        /*if (defaultEventsGenerated)
        {
            Toast msg = ToastCompat.makeText(_dataWrapper.context.getApplicationContext(),
                    fragment.getResources().getString(R.string.toast_predefined_events_generated),
                    Toast.LENGTH_SHORT);
            msg.show();
        }*/
    }
}
 
Example 15
Source File: AppKeysActivity.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_keys);
    mViewModel = new ViewModelProvider(this, mViewModelFactory).get(AppKeysViewModel.class);

    //Bind ui
    ButterKnife.bind(this);

    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //noinspection ConstantConditions
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final ExtendedFloatingActionButton fab = findViewById(R.id.fab_add);
    final RecyclerView appKeysRecyclerView = findViewById(R.id.recycler_view_keys);
    appKeysRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    final DividerItemDecoration dividerItemDecoration =
            new DividerItemDecoration(appKeysRecyclerView.getContext(), DividerItemDecoration.VERTICAL);
    appKeysRecyclerView.addItemDecoration(dividerItemDecoration);
    appKeysRecyclerView.setItemAnimator(new DefaultItemAnimator());

    final Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        switch (bundle.getInt(EXTRA_DATA)) {
            case MANAGE_APP_KEY:
                break;
            case ADD_APP_KEY:
                getSupportActionBar().setTitle(R.string.title_select_app_key);
                fab.hide();
                mAdapter = new ManageAppKeyAdapter(this, mViewModel.getNetworkLiveData());
                mAdapter.setOnItemClickListener(this);
                appKeysRecyclerView.setAdapter(mAdapter);
                setUpObserver();
                break;
            case BIND_APP_KEY:
            case PUBLICATION_APP_KEY:
                getSupportActionBar().setTitle(R.string.title_select_app_key);
                fab.hide();
                //Get selected mesh node
                final ProvisionedMeshNode node = mViewModel.getSelectedMeshNode().getValue();
                if (node != null) {
                    final List<NodeKey> applicationKeys = node.getAddedAppKeys();
                    if (!applicationKeys.isEmpty()) {
                        mAdapter = new ManageAppKeyAdapter(mViewModel.getNetworkLiveData().getAppKeys(), applicationKeys);
                        mAdapter.setOnItemClickListener(this);
                        appKeysRecyclerView.setAdapter(mAdapter);
                    } else {
                        final TextView textView = mEmptyView.findViewById(R.id.rationale);
                        textView.setText(R.string.no_added_app_keys_rationale);
                        mEmptyView.setVisibility(View.VISIBLE);
                    }
                }
                break;
        }
    } else {
        getSupportActionBar().setTitle(R.string.title_manage_app_keys);
        final ItemTouchHelper.Callback itemTouchHelperCallback = new RemovableItemTouchHelperCallback(this);
        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
        itemTouchHelper.attachToRecyclerView(appKeysRecyclerView);
        mAdapter = new ManageAppKeyAdapter(this, mViewModel.getNetworkLiveData());
        mAdapter.setOnItemClickListener(this);
        appKeysRecyclerView.setAdapter(mAdapter);
        setUpObserver();
    }


    fab.setOnClickListener(v -> {
        final Intent intent = new Intent(this, AddAppKeyActivity.class);
        startActivity(intent);
    });

    appKeysRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
            super.onScrolled(recyclerView, dx, dy);
            final LinearLayoutManager m = (LinearLayoutManager) recyclerView.getLayoutManager();
            if (m != null) {
                if (m.findFirstCompletelyVisibleItemPosition() == 0) {
                    fab.extend();
                } else {
                    fab.shrink();
                }
            }
        }
    });
}
 
Example 16
Source File: GroupsFragment.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
    @SuppressLint("InflateParams") final View rootView = inflater.inflate(R.layout.fragment_groups, null);
    mViewModel = new ViewModelProvider(requireActivity(), mViewModelFactory).get(SharedViewModel.class);
    ButterKnife.bind(this, rootView);

    final ExtendedFloatingActionButton fab = rootView.findViewById(R.id.fab_add_group);

    // Configure the recycler view
    final RecyclerView recyclerViewGroups = rootView.findViewById(R.id.recycler_view_groups);
    recyclerViewGroups.setLayoutManager(new LinearLayoutManager(requireContext()));
    final DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerViewGroups.getContext(), DividerItemDecoration.VERTICAL);
    recyclerViewGroups.addItemDecoration(dividerItemDecoration);
    final ItemTouchHelper.Callback itemTouchHelperCallback = new RemovableItemTouchHelperCallback(this);
    final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
    itemTouchHelper.attachToRecyclerView(recyclerViewGroups);
    final GroupAdapter adapter = new GroupAdapter(requireContext());
    adapter.setOnItemClickListener(this);
    recyclerViewGroups.setAdapter(adapter);

    mViewModel.getNetworkLiveData().observe(getViewLifecycleOwner(), meshNetworkLiveData -> {
        if (meshNetworkLiveData != null) {
            if (meshNetworkLiveData.getMeshNetwork().getGroups().isEmpty()) {
                mEmptyView.setVisibility(View.VISIBLE);
            } else {
                mEmptyView.setVisibility(View.INVISIBLE);
            }
        }
    });

    mViewModel.getGroups().observe(getViewLifecycleOwner(), groups -> {
        final MeshNetwork network = mViewModel.getNetworkLiveData().getMeshNetwork();
        adapter.updateAdapter(network, groups);
    });

    fab.setOnClickListener(v -> {
        DialogFragmentCreateGroup fragmentCreateGroup = DialogFragmentCreateGroup.newInstance();
        fragmentCreateGroup.show(getChildFragmentManager(), null);
    });

    recyclerViewGroups.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
            super.onScrolled(recyclerView, dx, dy);
            final LinearLayoutManager m = (LinearLayoutManager) recyclerView.getLayoutManager();
            if (m != null) {
                if (m.findFirstCompletelyVisibleItemPosition() == 0) {
                    fab.extend();
                } else {
                    fab.shrink();
                }
            }
        }
    });

    return rootView;

}
 
Example 17
Source File: BaseModelConfigurationActivity.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_model_configuration);
    ButterKnife.bind(this);
    mViewModel = new ViewModelProvider(this, mViewModelFactory).get(ModelConfigurationViewModel.class);
    mHandler = new Handler();

    final MeshModel meshModel = mViewModel.getSelectedModel().getValue();
    //noinspection ConstantConditions
    final String modelName = meshModel.getModelName();

    // Set up views
    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //noinspection ConstantConditions
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(modelName);
    final int modelId = meshModel.getModelId();
    getSupportActionBar().setSubtitle(getString(R.string.model_id, CompositionDataParser.formatModelIdentifier(modelId, true)));

    recyclerViewAddresses = findViewById(R.id.recycler_view_addresses);
    recyclerViewAddresses.setLayoutManager(new LinearLayoutManager(this));
    final ItemTouchHelper.Callback itemTouchHelperCallback = new RemovableItemTouchHelperCallback(this);
    final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
    itemTouchHelper.attachToRecyclerView(recyclerViewAddresses);
    mSubscriptionAdapter = new GroupAddressAdapter(this, mViewModel.getNetworkLiveData().getMeshNetwork(), mViewModel.getSelectedModel());
    recyclerViewAddresses.setAdapter(mSubscriptionAdapter);

    recyclerViewBoundKeys = findViewById(R.id.recycler_view_bound_keys);
    recyclerViewBoundKeys.setLayoutManager(new LinearLayoutManager(this));
    final ItemTouchHelper.Callback itemTouchHelperCallbackKeys = new RemovableItemTouchHelperCallback(this);
    final ItemTouchHelper itemTouchHelperKeys = new ItemTouchHelper(itemTouchHelperCallbackKeys);
    itemTouchHelperKeys.attachToRecyclerView(recyclerViewBoundKeys);
    mBoundAppKeyAdapter = new BoundAppKeysAdapter(this, mViewModel.getNetworkLiveData().getAppKeys(), mViewModel.getSelectedModel());
    recyclerViewBoundKeys.setAdapter(mBoundAppKeyAdapter);

    mActionBindAppKey.setOnClickListener(v -> {
        final ProvisionedMeshNode node = mViewModel.getSelectedMeshNode().getValue();
        if (node != null && !node.isExist(SigModelParser.CONFIGURATION_SERVER)) {
            return;
        }
        if (!checkConnectivity()) return;
        final Intent bindAppKeysIntent = new Intent(BaseModelConfigurationActivity.this, AppKeysActivity.class);
        bindAppKeysIntent.putExtra(EXTRA_DATA, BIND_APP_KEY);
        startActivityForResult(bindAppKeysIntent, SELECT_KEY);
    });

    mPublishAddressView.setText(R.string.none);
    mActionSetPublication.setOnClickListener(v -> navigateToPublication());

    mActionClearPublication.setOnClickListener(v -> clearPublication());

    mActionSubscribe.setOnClickListener(v -> {
        if (!checkConnectivity()) return;
        //noinspection ConstantConditions
        final ArrayList<Group> groups = new ArrayList<>(mViewModel.getGroups().getValue());
        final DialogFragmentGroupSubscription fragmentSubscriptionAddress = DialogFragmentGroupSubscription.newInstance(groups);
        fragmentSubscriptionAddress.show(getSupportFragmentManager(), null);
    });

    mViewModel.getSelectedModel().observe(this, model -> {
        if (model != null) {
            updateAppStatusUi(model);
            updatePublicationUi(model);
            updateSubscriptionUi(model);
        }
    });

    mViewModel.getTransactionStatus().observe(this, transactionStatus -> {
        if (transactionStatus != null) {
            hideProgressBar();
            final String message = getString(R.string.operation_timed_out);
            DialogFragmentTransactionStatus fragmentMessage = DialogFragmentTransactionStatus.newInstance("Transaction Failed", message);
            fragmentMessage.show(getSupportFragmentManager(), null);
        }
    });

    mViewModel.isConnectedToProxy().observe(this, isConnected -> {
        if (isConnected != null) {
            mIsConnected = isConnected;
            hideProgressBar();
            updateClickableViews();
        }
        invalidateOptionsMenu();
    });

    mViewModel.getMeshMessage().observe(this, this::updateMeshMessage);

    final Boolean isConnectedToNetwork = mViewModel.isConnectedToProxy().getValue();
    if (isConnectedToNetwork != null) {
        mIsConnected = isConnectedToNetwork;
    }
    invalidateOptionsMenu();
}
 
Example 18
Source File: NetworkFragment.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nullable
@Override
public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
    @SuppressLint("InflateParams") final View rootView = inflater.inflate(R.layout.fragment_network, null);
    mViewModel = new ViewModelProvider(requireActivity(), mViewModelFactory).get(SharedViewModel.class);
    ButterKnife.bind(this, rootView);

    final ExtendedFloatingActionButton fab = rootView.findViewById(R.id.fab_add_node);
    final View noNetworksConfiguredView = rootView.findViewById(R.id.no_networks_configured);

    // Configure the recycler view
    mNodeAdapter = new NodeAdapter(this, mViewModel.getNodes());
    mNodeAdapter.setOnItemClickListener(this);
    mRecyclerViewNodes.setLayoutManager(new LinearLayoutManager(getContext()));
    mRecyclerViewNodes.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));
    final ItemTouchHelper.Callback itemTouchHelperCallback = new RemovableItemTouchHelperCallback(this);
    final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
    itemTouchHelper.attachToRecyclerView(mRecyclerViewNodes);
    mRecyclerViewNodes.setAdapter(mNodeAdapter);

    // Create view model containing utility methods for scanning
    mViewModel.getNodes().observe(getViewLifecycleOwner(), nodes -> {
        if (nodes != null && !nodes.isEmpty()) {
            noNetworksConfiguredView.setVisibility(View.GONE);
        } else {
            noNetworksConfiguredView.setVisibility(View.VISIBLE);
        }
        requireActivity().invalidateOptionsMenu();
    });

    mViewModel.isConnectedToProxy().observe(getViewLifecycleOwner(), isConnected -> {
        if (isConnected != null) {
            requireActivity().invalidateOptionsMenu();
        }
    });

    mRecyclerViewNodes.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(@NonNull final RecyclerView recyclerView, final int dx, final int dy) {
            super.onScrolled(recyclerView, dx, dy);
            final LinearLayoutManager m = (LinearLayoutManager) recyclerView.getLayoutManager();
            if (m != null) {
                if (m.findFirstCompletelyVisibleItemPosition() == 0) {
                    fab.extend();
                } else {
                    fab.shrink();
                }
            }
        }
    });

    fab.setOnClickListener(v -> {
        final Intent intent = new Intent(requireContext(), ScannerActivity.class);
        intent.putExtra(Utils.EXTRA_DATA_PROVISIONING_SERVICE, true);
        startActivityForResult(intent, Utils.PROVISIONING_SUCCESS);
    });

    return rootView;
}
 
Example 19
Source File: RangesActivity.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@SuppressWarnings("ConstantConditions")
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranges);
    ButterKnife.bind(this);
    mViewModel = new ViewModelProvider(this, mViewModelFactory).get(RangesViewModel.class);
    mType = getIntent().getExtras().getInt(Utils.RANGE_TYPE);

    //Bind ui
    final Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    final RecyclerView recyclerView = findViewById(R.id.recycler_view);
    final View rangesContainer = findViewById(R.id.info_ranges);
    rangesContainer.findViewById(R.id.container).setVisibility(View.VISIBLE);
    mRangeView = rangesContainer.findViewById(R.id.range);
    final TextView startAddress = rangesContainer.findViewById(R.id.start_address);
    final TextView endAddress = rangesContainer.findViewById(R.id.end_address);
    final ExtendedFloatingActionButton fab_add = findViewById(R.id.fab_add);

    mProvisioner = mViewModel.getSelectedProvisioner().getValue();

    switch (mType) {
        case Utils.GROUP_RANGE:
            startAddress.setText(MeshAddress.formatAddress(MeshAddress.START_GROUP_ADDRESS, true));
            endAddress.setText(MeshAddress.formatAddress(MeshAddress.END_GROUP_ADDRESS, true));
            getSupportActionBar().setTitle(R.string.title_edit_group_ranges);
            mRangeAdapter = new RangeAdapter(this,
                    mProvisioner.getProvisionerUuid(),
                    mProvisioner.getAllocatedGroupRanges(),
                    mViewModel.getNetworkLiveData().getProvisioners());
            break;
        case Utils.SCENE_RANGE:
            startAddress.setText(MeshAddress.formatAddress(0x0000, true));
            endAddress.setText(MeshAddress.formatAddress(0xFFFF, true));
            getSupportActionBar().setTitle(R.string.title_edit_scene_ranges);
            mRangeAdapter = new RangeAdapter(this,
                    mProvisioner.getProvisionerUuid(),
                    mProvisioner.getAllocatedSceneRanges(),
                    mViewModel.getNetworkLiveData().getProvisioners());
            break;
        default:
        case Utils.UNICAST_RANGE:
            startAddress.setText(MeshAddress.formatAddress(MeshAddress.START_UNICAST_ADDRESS, true));
            endAddress.setText(MeshAddress.formatAddress(MeshAddress.END_UNICAST_ADDRESS, true));
            getSupportActionBar().setTitle(R.string.title_edit_unicast_ranges);
            mRangeAdapter = new RangeAdapter(this,
                    mProvisioner.getProvisionerUuid(),
                    mProvisioner.getAllocatedUnicastRanges(),
                    mViewModel.getNetworkLiveData().getProvisioners());
            break;
    }

    mRangeAdapter.setOnItemClickListener(this);

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    final DividerItemDecoration dividerItemDecoration =
            new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
    recyclerView.addItemDecoration(dividerItemDecoration);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mRangeAdapter);
    final ItemTouchHelper.Callback itemTouchHelperCallback = new RemovableItemTouchHelperCallback(this);
    final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
    itemTouchHelper.attachToRecyclerView(recyclerView);

    fab_add.setOnClickListener(v -> {
        switch (mType) {
            case Utils.GROUP_RANGE:
                final DialogFragmentGroupRange groupRange = DialogFragmentGroupRange.newInstance(null);
                groupRange.show(getSupportFragmentManager(), null);
                break;
            case Utils.SCENE_RANGE:
                final DialogFragmentSceneRange sceneRange = DialogFragmentSceneRange.newInstance(null);
                sceneRange.show(getSupportFragmentManager(), null);
                break;
            default:
            case Utils.UNICAST_RANGE:
                final DialogFragmentUnicastRange unicastRange = DialogFragmentUnicastRange.newInstance(null);
                unicastRange.show(getSupportFragmentManager(), null);
                break;
        }
    });

    mFabResolve.setOnClickListener(v -> resolveRanges());

    updateRanges();
    updateOtherRanges();
    updateEmptyView();
    updateResolveFab();
}
 
Example 20
Source File: RecyclerViewOverScrollDecorAdapter.java    From overscroll-decor with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public RecyclerViewOverScrollDecorAdapter(RecyclerView recyclerView, Impl impl, ItemTouchHelper.Callback itemTouchHelperCallback) {
    this(recyclerView, impl);
    setUpTouchHelperCallback(itemTouchHelperCallback);
}