Advertisement
Guest User

GuidedStepSupportFragment no scroll effect example

a guest
Apr 15th, 2020
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package ch.andeo.dev.tvplayground;
  2.  
  3. import android.graphics.drawable.Drawable;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Looper;
  7. import android.util.Log;
  8.  
  9. import androidx.annotation.NonNull;
  10. import androidx.leanback.app.GuidedStepSupportFragment;
  11. import androidx.leanback.widget.GuidanceStylist;
  12. import androidx.leanback.widget.GuidedAction;
  13. import androidx.recyclerview.widget.RecyclerView;
  14.  
  15. import java.lang.reflect.InvocationTargetException;
  16. import java.lang.reflect.Method;
  17. import java.util.List;
  18.  
  19. public class SampleStepFragment extends GuidedStepSupportFragment {
  20.  
  21.     private static final String TAG = "SampleStepFragment";
  22.  
  23.     @NonNull
  24.     @Override
  25.     public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
  26.         String title = "Title";
  27.         String breadcrumb = "Breadcrumb";
  28.         String description = "Description";
  29.         Drawable icon = getActivity().getDrawable(R.drawable.app_icon_your_company);
  30.  
  31.         return new GuidanceStylist.Guidance(title, description, breadcrumb, icon);
  32.     }
  33.  
  34.     @Override
  35.     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
  36.  
  37.         addAction(actions, GuidedAction.ACTION_ID_CONTINUE, "Action1");
  38.         addAction(actions, GuidedAction.ACTION_ID_CANCEL, "Action2");
  39.  
  40.         // Run code delayed on mainThread (any other/better method can/should be used)
  41.         // It's delayed because if focus scroll is disabled, the list will stick to the top of the layout
  42.         new Handler(Looper.getMainLooper()).postDelayed(this::disableFocusScroll, 500);
  43.     }
  44.  
  45.     private void disableFocusScroll() {
  46.         RecyclerView.LayoutManager layoutManager = SampleStepFragment.this.getGuidedActionsStylist().getActionsGridView().getLayoutManager();
  47.         try {
  48.             Method method = layoutManager.getClass().getMethod("setFocusScrollStrategy", int.class);
  49.             method.invoke(layoutManager, 1 /* FOCUS_SCROLL_ITEM */);
  50.         } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
  51.             Log.e(TAG, "disableFocusScroll: ", e);
  52.         }
  53.     }
  54.  
  55.     private void addAction(List<GuidedAction> actions, long id, String text) {
  56.         actions.add(new GuidedAction.Builder(this.getContext()).title(text).clickAction(id).build());
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement