android 9.0 Launcher3修改PageIndicator

android 9.0 Launcher3的PageIndicator 是横线,需要把它改成小圆点

先看Launcher3的xml
res/layout/launcher.xml

...

        <!-- The workspace contains 5 screens of cells -->
        <!-- DO NOT CHANGE THE ID -->
        <com.android.launcher3.Workspace
            android:id="@+id/workspace"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:theme="@style/HomeScreenElementTheme"
            launcher:pageIndicator="@+id/page_indicator" />

        <include
            android:id="@+id/overview_panel_container"
            layout="@layout/overview_panel"
            android:visibility="gone" />

        <!-- Keep these behind the workspace so that they are not visible when
         we go into AllApps -->
        <com.android.launcher3.pageindicators.WorkspacePageIndicator
            android:id="@+id/page_indicator"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_gravity="bottom|center_horizontal"
            android:theme="@style/HomeScreenElementTheme" />

   ...
</com.android.launcher3.LauncherRootView>

这个WorkspacePageIndicator 就是主界面的PageIndicator了
在src/com.android.launcher3.pageindicators下发现还有个PageIndicatorDots
这不就是小圆点的吗
看了下它在Folder里有被调用,所以在桌面上创建了个文件夹,发现这个小圆点的PageIndicator 还不错
所以我就把WorkspacePageIndicator给替换成PageIndicatorDots

        <!-- Keep these behind the workspace so that they are not visible when
         we go into AllApps -->
        <com.android.launcher3.pageindicators.PageIndicatorDots
            android:id="@+id/page_indicator"
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:layout_gravity="bottom|center_horizontal"
            android:theme="@style/HomeScreenElementTheme" />

然后修改src/com.android.launcher3.Workspace,extends PagedView改为PagedView

/**
 * The workspace is a wide area with a wallpaper and a finite number of pages.
 * Each page contains a number of icons, folders or widgets the user can
 * interact with. A workspace is meant to be used with a fixed width only.
 */
public class Workspace extends PagedView<PageIndicatorDots>
        implements DropTarget, DragSource, View.OnTouchListener,
        DragController.DragListener, Insettable, LauncherStateManager.StateHandler {
    private static final String TAG = "Launcher.Workspace";

发现src/com.android.launcher3.states.SpringLoadedState报错了,设置自动隐藏的 直接注释掉

    @Override
    public void onStateEnabled(Launcher launcher) {
        Workspace ws = launcher.getWorkspace();
        ws.showPageIndicatorAtCurrentScroll();
            //报错点  设置自动隐藏的 直接注释掉
//        ws.getPageIndicator().setShouldAutoHide(false);

        // Prevent any Un/InstallShortcutReceivers from updating the db while we are
        // in spring loaded mode
        InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_DRAG_AND_DROP);
        launcher.getRotationHelper().setCurrentStateRequest(REQUEST_LOCK);
    }

    @Override
    public float getWorkspaceScrimAlpha(Launcher launcher) {
        return 0.3f;
    }

    @Override
    public void onStateDisabled(final Launcher launcher) {
    //报错点  设置自动隐藏的 直接注释掉
//        launcher.getWorkspace().getPageIndicator().setShouldAutoHide(true);

        // Re-enable any Un/InstallShortcutReceiver and now process any queued items
        InstallShortcutReceiver.disableAndFlushInstallQueue(
                InstallShortcutReceiver.FLAG_DRAG_AND_DROP, launcher);
    }

调试看的时候发现PageIndicator跑到HotSeat下面去了,这是要在HotSeat上面的

对比看了下WorkspacePageIndicator和PageIndicatorDots
然后发现让PageIndicatorDots implements Insettable 就可以实现在HotSeat上面了

先implements Insettable

public class PageIndicatorDots extends View implements Insettable, PageIndicator {

然后直接把WorkspacePageIndicator 的setInsets copy过来

    @Override
    public void setInsets(Rect insets) {
        DeviceProfile grid = mLauncher.getDeviceProfile();
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();

        if (grid.isVerticalBarLayout()) {
            Rect padding = grid.workspacePadding;
            lp.leftMargin = padding.left + grid.workspaceCellPaddingXPx;
            lp.rightMargin = padding.right + grid.workspaceCellPaddingXPx;
            lp.bottomMargin = padding.bottom;
        } else {
            lp.leftMargin = lp.rightMargin = 0;
            lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
            lp.bottomMargin = grid.hotseatBarSizePx + insets.bottom;
        }
        setLayoutParams(lp);
    }

最后在构造方法初始化mLauncher

    public PageIndicatorDots(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mLauncher = Launcher.getLauncher(context);
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

2018-12-04 09:06 更新
在设置里将Launcher3的缓存清掉后,有时会发生奔溃问题
检查发现PageIndicatorDots setScroll 的totalScroll有时会为0导致scrollPerPage会有分母为0的情况

  @Override
    public void setScroll(int currentScroll, int totalScroll) {
        if (mNumPages > 1) {
            if (mIsRtl) {
                currentScroll = totalScroll - currentScroll;
            }
            int scrollPerPage = totalScroll / (mNumPages - 1);
            //add by yy
            if(scrollPerPage == 0)
                return;
            //end add by yy
            int pageToLeft = currentScroll / scrollPerPage;
            int pageToLeftScroll = pageToLeft * scrollPerPage;
            int pageToRightScroll = pageToLeftScroll + scrollPerPage;

            float scrollThreshold = SHIFT_THRESHOLD * scrollPerPage;
            if (currentScroll < pageToLeftScroll + scrollThreshold) {
                // scroll is within the left page's threshold
                animateToPosition(pageToLeft);
            } else if (currentScroll > pageToRightScroll - scrollThreshold) {

2018-12-12 16:16 更新
WorkspacePageIndicator 里有两个方法其他地方可能会被调用,是用来操作动画的
在PageIndicatorDots 可以加上

    /**
     * Pauses all currently running animations.
     */
    public void pauseAnimations() {
        stopAllAnimations();
    }

    /**
     * Force-ends all currently running or paused animations.
     */
    public void skipAnimationsToEnd() {
        stopAllAnimations();
    }

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值