禁用动作列表的滚动效果
disable the scroll effect of the action list
我有一个 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");
}
}
问题:当我滚动动作列表时,它显示如下;
但我想要这样的东西;
如何在我的动作列表中禁用此效果?
谢谢
我做到了,但并不容易弄明白。
没有 支持的 方法,因为实际上使这成为可能的 API 是私有包或隐藏包 public 故意使用. (你可以自己做,但你最终只是从 leanback 库中复制 类。)
解决方法:
@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);
}
}
解释:
A GuidedStepSupportFragment
请求 GuidedActionsStylist
负责在右侧呈现列表项。 source
GuidedActionsStylist
设计师扩充了包含 VerticalGridView
source
的布局 lb_guidedactions.xml
VerticalGridView
扩展了 BaseGridView
并创建了一个 GridLayoutManager
作为它的布局管理器。遗憾的是,这个 GridLayoutManager
是私有包和最终包...(android 为什么...?)。它有方法 setFocusScrollStrategy
用于确定滚动行为。source
查看不同的焦点滚动策略:
/**
* 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;
所以既然API是隐藏的,我们就用反射把setFocusScrollStrategy
方法暴露出来,设置为FOCUS_SCROLL_ITEM
.
不过我们不能立即执行此操作,因为如果没有默认的滚动设置,列表项将弹出到布局的顶部并且不会保持居中。所以我添加了 500 毫秒的延迟,这太可怕了......如果你设法找出什么时候最好触发它,请告诉我。
事实证明,有一个更优雅、更简单的解决方案。在继承guideStepSupportFragment
windowAlignment 选项的class中的onViewCreated
的body中添加即可(还有其他对齐选项):
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// ...
val gridView = guidedActionsStylist.actionsGridView
gridView.windowAlignment = VerticalGridView.WINDOW_ALIGN_BOTH_EDGE
// ...
super.onViewCreated(view, savedInstanceState)
}
这是禁用 VerticalGridView 滚动的简单方法
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
VerticalGridView gridView = getGuidedActionsStylist().getActionsGridView();
gridView.setScrollEnabled(false);
super.onViewCreated(view, savedInstanceState);
}
我有一个 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");
}
}
问题:当我滚动动作列表时,它显示如下;
但我想要这样的东西;
如何在我的动作列表中禁用此效果?
谢谢
我做到了,但并不容易弄明白。
没有 支持的 方法,因为实际上使这成为可能的 API 是私有包或隐藏包 public 故意使用. (你可以自己做,但你最终只是从 leanback 库中复制 类。)
解决方法:
@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);
}
}
解释:
A GuidedStepSupportFragment
请求 GuidedActionsStylist
负责在右侧呈现列表项。 source
GuidedActionsStylist
设计师扩充了包含 VerticalGridView
source
lb_guidedactions.xml
VerticalGridView
扩展了 BaseGridView
并创建了一个 GridLayoutManager
作为它的布局管理器。遗憾的是,这个 GridLayoutManager
是私有包和最终包...(android 为什么...?)。它有方法 setFocusScrollStrategy
用于确定滚动行为。source
查看不同的焦点滚动策略:
/**
* 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;
所以既然API是隐藏的,我们就用反射把setFocusScrollStrategy
方法暴露出来,设置为FOCUS_SCROLL_ITEM
.
不过我们不能立即执行此操作,因为如果没有默认的滚动设置,列表项将弹出到布局的顶部并且不会保持居中。所以我添加了 500 毫秒的延迟,这太可怕了......如果你设法找出什么时候最好触发它,请告诉我。
事实证明,有一个更优雅、更简单的解决方案。在继承guideStepSupportFragment
windowAlignment 选项的class中的onViewCreated
的body中添加即可(还有其他对齐选项):
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// ...
val gridView = guidedActionsStylist.actionsGridView
gridView.windowAlignment = VerticalGridView.WINDOW_ALIGN_BOTH_EDGE
// ...
super.onViewCreated(view, savedInstanceState)
}
这是禁用 VerticalGridView 滚动的简单方法
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
VerticalGridView gridView = getGuidedActionsStylist().getActionsGridView();
gridView.setScrollEnabled(false);
super.onViewCreated(view, savedInstanceState);
}