Android 禁用操作列表的滚动效果
我有一个像这样的Android 禁用操作列表的滚动效果,android,android-tv,leanback,Android,Android Tv,Leanback,我有一个像这样的GuidedStepSupportFragment片段 public class SampleStepFragment extends GuidedStepSupportFragment { @NonNull @Override public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { String title = "Title";
GuidedStepSupportFragment
片段
public class SampleStepFragment extends GuidedStepSupportFragment {
@NonNull
@Override
public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
String title = "Title";
String breadcrumb = "Breadcrumb";
String description = "Description";
Drawable icon = getActivity().getDrawable(R.drawable.ic_videocam_black_24dp);
return new GuidanceStylist.Guidance(title, description, breadcrumb, icon);
}
@Override
public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
addAction(actions, ACTION_CONTINUE, "Action1");
addAction(actions, ACTION_BACK, "Action2");
}
}
公共类SampleStepFragment扩展了GuidedStepSupportFragment{
@非空
@凌驾
public GuidanceStylist.guidanceoncreateguidance(Bundle savedInstanceState){
字符串title=“title”;
String breadcrumb=“breadcrumb”;
String description=“description”;
可绘制图标=getActivity().getDrawable(R.Drawable.ic_videocam_black_24dp);
返回新的GuidanceStylist.Guidance(标题、描述、面包屑、图标);
}
@凌驾
public void onCreateActions(@NonNull List actions,Bundle savedInstanceState){
添加行动(行动,行动,继续,“行动1”);
添加操作(操作,操作返回,“操作2”);
}
}
问题:当我滚动动作列表时,它显示如下
但是我想要这样的东西
如何在操作列表中禁用此效果
谢谢我做到了,这不容易弄清楚 没有支持的方法可以做到这一点,因为实际上实现这一点的API是包私有的,或者故意隐藏起来不供公众使用。(您可以自己完成,但最终只能从leanback库复制类。) 解决方案: 因此,由于API是隐藏的,我们只需使用反射来公开
setFocusScrollStrategy
方法,并将其设置为FOCUS\u SCROLL\u ITEM
但是我们不能立即执行此操作,因为如果没有默认的滚动设置,列表项将弹出到布局的顶部,并且不会保持居中。所以我加了500毫秒的延迟,这太可怕了。。。如果您设法找到触发此事件的最佳时间,请告诉我。示例中不清楚错误与预期效果之间的区别,或者至少我看不到。这是一个对齐问题吗?作为旁注:谢谢你提出了这个有趣的问题,在研究过程中,我修复了在我自己的电视应用程序中滚动的问题,原因是:)
@Override
public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
addAction(actions, GuidedAction.ACTION_ID_CONTINUE, "Action1");
addAction(actions, GuidedAction.ACTION_ID_CANCEL, "Action2");
// Run code delayed on mainThread (any other/better method can/should be used)
// It's delayed because if focus scroll is disabled, the list will stick to the top of the layout
new Handler(Looper.getMainLooper()).postDelayed(this::disableFocusScroll, 500);
}
private void disableFocusScroll() {
RecyclerView.LayoutManager layoutManager = SampleStepFragment.this.getGuidedActionsStylist().getActionsGridView().getLayoutManager();
try {
Method method = layoutManager.getClass().getMethod("setFocusScrollStrategy", int.class);
method.invoke(layoutManager, 1 /* FOCUS_SCROLL_ITEM */);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
Log.e(TAG, "disableFocusScroll: ", e);
}
}
/**
* Always keep focused item at a aligned position. Developer can use
* WINDOW_ALIGN_XXX and ITEM_ALIGN_XXX to define how focused item is aligned.
* In this mode, the last focused position will be remembered and restored when focus
* is back to the view.
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public final static int FOCUS_SCROLL_ALIGNED = 0;
/**
* Scroll to make the focused item inside client area.
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public final static int FOCUS_SCROLL_ITEM = 1;
/**
* Scroll a page of items when focusing to item outside the client area.
* The page size matches the client area size of RecyclerView.
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public final static int FOCUS_SCROLL_PAGE = 2;