首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >递归方式不显示Android ShowcaseView

递归方式不显示Android ShowcaseView
EN

Stack Overflow用户
提问于 2016-08-04 09:56:45
回答 1查看 241关注 0票数 0

开门见山吧。我正在尝试使用amlcurran的ShowcaseView库来展示coachmark系列:https://github.com/amlcurran/ShowcaseView

我看不到马车标记了。问题是runCoachMark()方法总是返回null。runCoachMark()方法在mMainAdapter上紧跟在notifyDatasetChaned()之后运行。

我尝试过使用Handler()、.postDelayed()和Thread.sleep(),但都没有成功。

任何人都可以解释为什么会发生这种情况,以及这个问题的一些解决方案。谢谢。

代码语言:javascript
复制
private static final int COACHMARK_A = -3;
private static final int COACHMARK_B = -2;
private static final int COACHMARK_C = -1;
// ... some other coachmark type

private static final int END_COACHMARK = 0;

private static final int SCROLL_OFFSET = 56;
private static final long COACHMARK_DELAY = 200L;

private void runCoachMark(int type) {
        if (type == END_COACHMARK) {
            return;
        }
        View v = getCoachMarkView(type);
        if (v == null) {
            return;
        }
    showCoachMark(getActivity(), v, type);
}

@Nullable
private View getCoachMarkView(final int type) {
    safeScrollTo(getPos(type), SCROLL_OFFSET);

    return mMainList.getChildAt(0).findViewById(getCoachMarkId(type));
}

private void safeScrollTo(final int pos, final int offset) {
        mMainList.setLayoutFrozen(true);
        mLayoutManager.scrollToPositionWithOffset(pos, offset);
        mMainList.setLayoutFrozen(false);
}

private int getCoachMarkId(int type) {
    int id;
    switch (type) {
        case COACHMARK_A:
            id = R.id.A;
            break;
        case COACHMARK_B:
            id = R.id.B;
            break;
        case COACHMARK_C:
            id = R.id.C;
            break;
        // ... some other types
        default:
            id = 0;
            break;
    return id;
}

private int getPos(int type) {
        int pos;
        switch (type) {
            case COACHMARK_A:
                pos = 1;
                break;
            case COACHMARK_B:
                pos = 4;
                break;
            case COACHMARK_C:
                pos = 5;
                break;
// ... some other cases
            default:
                pos = 0;
                break;
        }
        return pos;
}

private void showCoachMark(final Context context, final View v, final int type) {
        new ShowcaseView.Builder(getActivity())
                        .setTarget(new ViewTarget(v.getId(), (Activity) context))
                        .setContentTitle(getTitle(type))
                        .setContentText(getDescription(type))
                        .setShowcaseDrawer(new CustomShowcaseView(v))
                        .setShowcaseEventListener(new OnShowcaseEventListener() {
                            @Override
                            public void onShowcaseViewHide(ShowcaseView showcaseView) {

                            }

                            @Override
                            public void onShowcaseViewDidHide(ShowcaseView showcaseView) {
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        runCoachMark(type++);
                                    }
                                }, COACHMARK_DELAY);
                            }

                            @Override
                            public void onShowcaseViewShow(ShowcaseView showcaseView) {

                            }

                            @Override
                            public void onShowcaseViewTouchBlocked(MotionEvent motionEvent) {

                            }
                        })
                        .build();
            }
}
EN

回答 1

Stack Overflow用户

发布于 2016-10-21 19:36:15

我将与您分享一种使用递归方法动态使用showcaseview的新方法

1)创建您的目标,例如4个ViewTargets (您可以创建n个目标),并将目标放入Activity类中声明为私有属性的数组中:私有ViewTarget[]目标;

代码语言:javascript
复制
    final ViewTarget target1 = new ViewTarget(textview1);
    final ViewTarget target2 = new ViewTarget(textview2);
    final ViewTarget target3 = new ViewTarget(textview3);
    final ViewTarget target4 = new ViewTarget(textview4);
    targets = new ViewTarget[] { target1, target2,target3,target4};

2)创建递归方法:

代码语言:javascript
复制
private void toStep(final String title, final String description,Target target, final int i) {
        final ShowcaseView.Builder showCaseBuilder = new ShowcaseView.Builder(ShowCaseActivity.this);
        showCaseBuilder.setTarget(targets[i]);
        showCaseBuilder.setContentTitle(title.replace("[i]", ""+i));
        showCaseBuilder.setContentText(description.replace("[i]", ""+i));
        showCaseBuilder.setShowcaseEventListener(new OnShowcaseEventListener() {
            @Override
            public void onShowcaseViewTouchBlocked(MotionEvent motionEvent) {
            }

            @Override
            public void onShowcaseViewShow(ShowcaseView showcaseView) {
            }

            @Override
            public void onShowcaseViewHide(ShowcaseView showcaseView) {
                try {
                    toStep(title, description, targets[i+1], i+1);
                } catch(Exception ex) {

                }
            }
            @Override
            public void onShowcaseViewDidHide(ShowcaseView showcaseView) {
            }
        });
        showCaseBuilder.build();
    }

3)最后,您可以调用显示Showcase的方法:

代码语言:javascript
复制
toStep("Title for showcase [i]", " Description for showcase [i] ",targets[0], 0);

我希望这将对您有所帮助:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38757290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档