gpt4 book ai didi

android - 禁用 Action 列表的滚动效果

转载 作者:行者123 更新时间:2023-12-04 13:37:20 25 4
gpt4 key购买 nike

我有一个 GuidedStepSupportFragment像这样的 fragment 。

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");

}
}

action1

问题:当我滚动操作列表时,它显示如下;

action1

但我想要这样的东西;

expected

如何在我的操作列表中禁用此效果?

谢谢

最佳答案

我设法做到了,这并不容易弄清楚。

没有支持的方法,因为实际上使这成为可能的 API 是包私有(private)的或故意隐藏不公开使用的。 (你可以自己做,但你最终只是从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);
}
}

Full example

说明:

一个 GuidedStepSupportFragment请求 GuidedActionsStylist它负责渲染右侧的列表项。 source
GuidedActionsStylist造型师夸大布局 lb_guidedactions.xml其中包含 VerticalGridView source
VerticalGridView扩展 BaseGridView并创建一个 GridLayoutManager作为其布局管理器。这个 GridLayoutManager可悲的是包私有(private)和最终......(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 毫秒的延迟,这太可怕了……如果您设法找出最好的触发时间,请告诉我。

关于android - 禁用 Action 列表的滚动效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61055651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com