【问题标题】:Android Java Vertical scroll view sizeAndroid Java 垂直滚动视图大小
【发布时间】:2020-10-23 10:30:57
【问题描述】:

我有一个带有垂直滚动视图的非常简单的活动(mainscreen.xml 下面)。

这个垂直滚动视图有一个线性布局子视图。

我动态地将文本视图作为子视图添加到此线性布局(下面的populateLinlayWithTextViews())。

稍后,在添加了这些文本视图子视图之后,我可以滚动垂直滚动视图(下面的onScrollChange())。

而且我可以测量垂直滚动视图的可见部分 (getHeight())。

我的问题是,我需要在滚动之前知道垂直滚动视图可见部分的最大高度是多少。

mainscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <LinearLayout
            android:id="@+id/linlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>
</LinearLayout>

MyActivity.java

protected void onCreate(@Nullable Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.mainscreen);

    Resources resources = getResources();
    int n_statusBarHeight = resources.getIdentifier( "status_bar_height", "dimen", "android"); // 36px

    populateLinlayWithTextViews();

    // HERE
    ScrollView sv = sv = findViewById(R.id.scrollview);
    int n_svVisiblePartHeight = sv.getHeight(); // 0px

    View.OnScrollChangeListener onScrollChangedListener
    = new View.OnScrollChangeListener()
    {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY)
        {
            ScrollView sv = sv = findViewById(R.id.scrollview);
            int n_svVisiblePartHeight = sv.getHeight(); // 680px
        }
    }

    sv = findViewById(R.id.scrollview);
    sv.setOnScrollChangeListener(onScrollChangedListener);
}

public void populateLinlayWithTextViews()
{
    LinearLayout linlay = findViewById(R.id.linlay);
    int n_viewHeight = 81; // px

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            n_viewHeight);

    for(int n_i=0 ; n_i<20 ; n_i++)
    {
        TextView tvNew = new TextView(m_app);
        tvNew.setText(n_i + " - test");
        tvNew.setTextColor(Color.rgb(0, 0, 0)); // black
        tvNew.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
        tvNew.setLayoutParams(params);

        linlay.addView(tvNew, linlay.getChildCount());
    }
}

当我滚动垂直滚动视图时,我可以看到n_svVisiblePartHeight 的值为680px

但我需要早点知道这个值// HERE

但是// HERE,我得到的值是0px

你能帮帮我吗?

编辑 1

我需要知道这个高度,因为:

  • 示例 1:我最初想知道填充后将位于(可见)屏幕底部的文本视图的索引 带有文本视图的线性布局;
  • 示例 2:给定这个高度 X 我正在寻找,假设我希望 10 文本视图最初可见,我想确定 X/10 这将是文本的高度视图,以便我可以将其动态放入 n_viewHeight

编辑 2

我只需要知道下面红色箭头的高度,尽快在活动的生命周期中,最好是在我将文本视图添加到线性布局之前或之后 (// HERE):

编辑 3

我不知道理论上是不是这样:

n_scrollViewVisibleHeightInPx =
    n_displayHeightInPx
  - n_actionBarHeight
  - n_navigationBarHeightInPx
  + n_statusBarHeightInPx;

...但实际上n_scrollViewVisibleHeightInPx 是上图中红色箭头指定的空间的高度。

为了获得这些不同的高度,以下是我使用的方法:

private int returnStatusBarHeightInPx()
{
int       n_statusBarHeightInPx = -1;
Resources resources             = null;
int       n_idStatusBarHeight   = -1;

    resources = getResources();

    n_idStatusBarHeight = resources.getIdentifier( "status_bar_height", "dimen", "android");
    if(n_idStatusBarHeight > 0)
    {
        n_statusBarHeightInPx = getResources().getDimensionPixelSize(n_idStatusBarHeight);
    }
    else
    {
        n_statusBarHeightInPx = 0;
    }
    return n_statusBarHeightInPx;
}

private int returnNavigationBarHeightInPx()
{
int       n_navigationBarHeightInPx = -1;
Resources resources                 = null;
int       n_idNavigationBarHeight   = -1;

    resources = getResources();
    n_idNavigationBarHeight = resources.getIdentifier("navigation_bar_height", "dimen", "android");

    if(n_idNavigationBarHeight > 0)
    {
        n_navigationBarHeightInPx = getResources().getDimensionPixelSize(n_idNavigationBarHeight);
    }
    else
    {
        n_navigationBarHeightInPx = 0;
    }
    return n_navigationBarHeightInPx;
}

private int returnActionBarHeightInPx()
{
TypedValue typedValue            = null;
int        n_actionBarHeightInPx = -1;

    typedValue = new TypedValue();

    if(getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true))
    {
        n_actionBarHeightInPx =
                TypedValue.complexToDimensionPixelSize(
                            typedValue.data,
                            getResources().getDisplayMetrics());
    }

    return n_actionBarHeightInPx;
}

private int returnDisplayHeightInPx()
{
DisplayMetrics displayMetrics      = null;
int            n_displayHeightInPx = -1;

    displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    n_displayHeightInPx = displayMetrics.heightPixels;

    return n_displayHeightInPx;
}

【问题讨论】:

    标签: java android size scrollview screen


    【解决方案1】:

    为什么要在“//这里”了解它。您不在那里使用该变量。您的代码没有解释为什么不能在滚动时间上定义变量。

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2015-12-21
      相关资源
      最近更新 更多