public abstract class

BaseLeanbackPreferenceFragmentCompat

extends PreferenceFragmentCompat

 java.lang.Object

androidx.fragment.app.Fragment

androidx.preference.PreferenceFragmentCompat

↳androidx.leanback.preference.BaseLeanbackPreferenceFragmentCompat

Subclasses:

LeanbackPreferenceFragmentCompat

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback-preference', version: '1.2.0-alpha04'

  • groupId: androidx.leanback
  • artifactId: leanback-preference
  • version: 1.2.0-alpha04

Artifact androidx.leanback:leanback-preference:1.2.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback-preference com.android.support:preference-leanback-v17

Overview

This fragment provides a preference fragment with leanback-style behavior, suitable for embedding into broader UI elements.

Summary

Fields
from PreferenceFragmentCompatARG_PREFERENCE_ROOT
from FragmentmPreviousWho
Constructors
publicBaseLeanbackPreferenceFragmentCompat()

Methods
public FragmentgetCallbackFragment()

A wrapper for getParentFragment which is v17+.

public ContextgetContext()

Return the this fragment is currently associated with.

public RecyclerViewonCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)

Creates the RecyclerView used to display the preferences.

from PreferenceFragmentCompataddPreferencesFromResource, findPreference, getListView, getPreferenceManager, getPreferenceScreen, onBindPreferences, onCreate, onCreateAdapter, onCreateLayoutManager, onCreatePreferences, onCreateView, onDestroyView, onDisplayPreferenceDialog, onNavigateToScreen, onPreferenceTreeClick, onSaveInstanceState, onStart, onStop, onUnbindPreferences, onViewCreated, scrollToPreference, scrollToPreference, setDivider, setDividerHeight, setPreferenceScreen, setPreferencesFromResource
from Fragmentdump, equals, getActivity, getAllowEnterTransitionOverlap, getAllowReturnTransitionOverlap, getArguments, getChildFragmentManager, getDefaultViewModelCreationExtras, getDefaultViewModelProviderFactory, getEnterTransition, getExitTransition, getFragmentManager, getHost, getId, getLayoutInflater, getLayoutInflater, getLifecycle, getLoaderManager, getParentFragment, getParentFragmentManager, getReenterTransition, getResources, getRetainInstance, getReturnTransition, getSavedStateRegistry, getSharedElementEnterTransition, getSharedElementReturnTransition, getString, getString, getTag, getTargetFragment, getTargetRequestCode, getText, getUserVisibleHint, getView, getViewLifecycleOwner, getViewLifecycleOwnerLiveData, getViewModelStore, hashCode, hasOptionsMenu, instantiate, instantiate, isAdded, isDetached, isHidden, isInLayout, isMenuVisible, isRemoving, isResumed, isStateSaved, isVisible, onActivityCreated, onActivityResult, onAttach, onAttach, onAttachFragment, onConfigurationChanged, onContextItemSelected, onCreateAnimation, onCreateAnimator, onCreateContextMenu, onCreateOptionsMenu, onDestroy, onDestroyOptionsMenu, onDetach, onGetLayoutInflater, onHiddenChanged, onInflate, onInflate, onLowMemory, onMultiWindowModeChanged, onOptionsItemSelected, onOptionsMenuClosed, onPause, onPictureInPictureModeChanged, onPrepareOptionsMenu, onPrimaryNavigationFragmentChanged, onRequestPermissionsResult, onResume, onViewStateRestored, postponeEnterTransition, postponeEnterTransition, registerForActivityResult, registerForActivityResult, registerForContextMenu, requestPermissions, requireActivity, requireArguments, requireContext, requireFragmentManager, requireHost, requireParentFragment, requireView, setAllowEnterTransitionOverlap, setAllowReturnTransitionOverlap, setArguments, setEnterSharedElementCallback, setEnterTransition, setExitSharedElementCallback, setExitTransition, setHasOptionsMenu, setInitialSavedState, setMenuVisibility, setReenterTransition, setRetainInstance, setReturnTransition, setSharedElementEnterTransition, setSharedElementReturnTransition, setTargetFragment, setUserVisibleHint, shouldShowRequestPermissionRationale, startActivity, startActivity, startActivityForResult, startActivityForResult, startIntentSenderForResult, startPostponedEnterTransition, toString, unregisterForContextMenu
from java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, wait

Constructors

public BaseLeanbackPreferenceFragmentCompat()

Methods

public Context getContext()

Return the this fragment is currently associated with.

See also: Fragment.requireContext()

public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)

Creates the RecyclerView used to display the preferences. Subclasses may override this to return a customized RecyclerView.

Parameters:

inflater: The LayoutInflater object that can be used to inflate the RecyclerView.
parent: The parent ViewGroup that the RecyclerView will be attached to. This method should not add the view itself, but this can be used to generate the layout params of the view.
savedInstanceState: If non-null, this view is being re-constructed from a previous saved state as given here.

Returns:

A new RecyclerView object to be placed into the view hierarchy

public Fragment getCallbackFragment()

A wrapper for getParentFragment which is v17+. Used by the leanback preference lib.

Returns:

The Fragment to possibly use as a callback

Source

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.leanback.preference;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;

import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.fragment.app.Fragment;
import androidx.leanback.widget.VerticalGridView;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceRecyclerViewAccessibilityDelegate;
import androidx.recyclerview.widget.RecyclerView;

/**
 * This fragment provides a preference fragment with leanback-style behavior, suitable for
 * embedding into broader UI elements.
 */
public abstract class BaseLeanbackPreferenceFragmentCompat extends PreferenceFragmentCompat {

    private Context mThemedContext;

    @Nullable
    @Override
    public Context getContext() {
        if (mThemedContext == null && getActivity() != null) {
            final TypedValue tv = new TypedValue();
            getActivity().getTheme().resolveAttribute(
                    androidx.preference.R.attr.preferenceTheme, tv, true);
            int theme = tv.resourceId;
            if (theme == 0) {
                // Fallback to default theme.
                theme = R.style.PreferenceThemeOverlayLeanback;
            }
            // aosp/821989 has forced PreferenceFragment to use the theme of activity and only
            // override theme attribute value when it's not defined in activity theme.
            // However, a side panel preference fragment can use different values than main content.
            // So a ContextThemeWrapper is required, overrides getContext() before
            // super.onCreate() call to use the ContextThemeWrapper in creating PreferenceManager
            // and onCreateView().
            // super.onCreate() will apply() the theme to activity in non-force way, which shouldn't
            // affect activity as the theme attributes of PreferenceThemeOverlayLeanback is already
            // in the activity's theme (in framework)
            mThemedContext = new ContextThemeWrapper(super.getContext(), theme);
        }
        return mThemedContext;
    }

    @Override
    public RecyclerView onCreateRecyclerView(LayoutInflater inflater, ViewGroup parent,
            Bundle savedInstanceState) {
        VerticalGridView verticalGridView = (VerticalGridView) inflater
                .inflate(R.layout.leanback_preferences_list, parent, false);
        verticalGridView.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_BOTH_EDGE);
        verticalGridView.setFocusScrollStrategy(VerticalGridView.FOCUS_SCROLL_ALIGNED);
        verticalGridView.setAccessibilityDelegateCompat(
                new PreferenceRecyclerViewAccessibilityDelegate(verticalGridView));
        return verticalGridView;
    }

    /**
     */
    @RestrictTo(LIBRARY_GROUP_PREFIX)
    @Override
    public Fragment getCallbackFragment() {
        return getParentFragment();
    }
}