android 9.0 Launcher3修改PageIndicator

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

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

...

        
        
        

        

        
        

   ...


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

        
        

然后修改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
        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();
    }

你可能感兴趣的:(android 9.0 Launcher3修改PageIndicator)