无法在ScrollView中的屏幕上看不到视图。始终返回true

时间:2014-05-17 12:06:42

标签: android scrollview

我一直在尝试确定是否可以在屏幕上看到TextView,如果不是,我想显示一个小文字说" Scroll To Read"。我尝试了this等问题以及其他许多方法的各种方法。在我的主 - 细节布局的细节片段中,我有一个片段,其中包含一个FrameLayout。这个framelayout后面有一个ScrollView,前面有一个textview,上面写着" Scroll To Read" (在使用scrollview中的新闻文章看不到文本视图之前,我会保持隐身状态)。例如,我不会显示" Scroll To Read" textview herehere,但会显示here。现在,它仅用于测试目的。

布局为here。滚动视图为scroller。当图像可用时viewstub 使用article_header_stub_import,它在文件的底部给出。我想检查TextView body是否可见。我在身体下方添加了wastedText仅供检查,但在所有情况下也显示出来。

我尝试过不同的解决方案,比如使用scrollingLinearLayoutscrollerdetailFrameLayout获取getHitRect,甚至使用详细信息片段。代码类似于我链接的问题,我只是使用Log.d(...)查看if-else案例的检查。

我正在使用Picasso加载图像并在毕加索的成功回调中进行编码,例如:

picassoInstance.load(mImageURL).into(mMainImageView, new Callback() {

            @Override
            public void onSuccess() {
                Log.d(TAG_ASYNC, "Image Loaded");
                mTestTextView.setVisibility(View.VISIBLE);

                Rect scrollBounds = new Rect();
                mScrollView.getHitRect(scrollBounds);
                if (mTestTextView.getLocalVisibleRect(scrollBounds)) {
                    // Any portion of the imageView, even a single pixel, is
                    // within the visible window
                    Log.d(TAG_ASYNC, "is visible");
                } else {
                    // NONE of the imageView is within the visible window
                    Log.d(TAG_ASYNC, "not visible");
                }

            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        });

我也尝试了getHeight,getTop,getBottom等,但都返回0。

1 个答案:

答案 0 :(得分:1)

我无法使用Rect。所以我搜索了#34;查看getheight总是0"。虽然我仍然不明白为什么高度返​​回0(尽管这发生在AsyncTask的onPostExecute和Picasso加载图像之后),我使用GlobalListener获取高度并与屏幕进行比较。

picassoInstance.load(mImageURL).into(mMainImageView, new Callback() {

@Override
public void onSuccess() {
    Log.d(TAG_ASYNC, "Image Loaded");

    mMainImageView.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {

                @SuppressWarnings("deprecation")
                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout() {

                    //Get Display metrics according to the SDK
                    Display display = getWindowManager().getDefaultDisplay();
                    Point screen = new Point();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                        display.getSize(screen);
                    } else {
                        screen.x = display.getWidth();
                        screen.y = display.getHeight();
                    }

                    //StatusBar Height
                    int statusBarHeight = 0;
                    int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
                    if (resId > 0) {
                        statusBarHeight = getResources().getDimensionPixelSize(resId);
                    }

                    //ActionBar Height
                    TypedValue tv = new TypedValue();
                    int actionBarHeight = 0;
                    if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
                        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                                getResources().getDisplayMetrics());
                    }

                    //Check
                    Log.d(TAG_ASYNC, "mSubtitleLayout " + mSubtitleLayout.getHeight());
                    Log.d(TAG_ASYNC, "mMainImageView " + mMainImageView.getHeight());
                    Log.d(TAG_ASYNC, "mArticleTextView " + mArticleTextView.getTop());
                    Log.d(TAG_ASYNC, "total height " + screen.y);
                    Log.d(TAG_ASYNC, "status bar " + statusBarHeight);
                    Log.d(TAG_ASYNC, "action bar " + actionBarHeight);

                    //Boolean to check if image+subtitle is large enough.
                    //If yes, then display "Scroll To Read"
                    displayScrollToRead = (screen.y - statusBarHeight - actionBarHeight) < (mArticleTextView
                            .getTop()) * 1.08;
                    Log.d(TAG_ASYNC, "displayScrollToRead " + displayScrollToRead);

                    if (displayScrollToRead) {
                        mScrollToReadLayout.setVisibility(View.VISIBLE);
                        Log.d(TAG_ASYNC, "Visiblity - " + mScrollToReadLayout.getVisibility());
                    }

                    //remove GlobalLayoutListener according to SDK
                    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                        mMainImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mMainImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }

                }

            });

}

@Override
public void onError() {

    Log.d(TAG_ASYNC, "IMAGE WAS NOT LOADED!!!");

}
});