java - 如何从 Android 数组中获取 textView 的滚动位置?

标签 java android scroll textview

我创建了一个包含大量 TextView 的数组。我想自动向下滚动到 statistics[i] textView。我尝试使用 scrollView.scrollTo(0, stats[i].getBottom()); 但什么也没发生。有任何想法吗? 这是我的代码:

TextView[] statistics;

final LinearLayout mainLinearLayout = new LinearLayout(this);
this.setContentView(mainLinearLayout);

final RelativeLayout topRelativeLayout = new RelativeLayout(this);
mainLinearLayout.addView(topRelativeLayout);

final TextView headerTextView = new TextView(this);
headerTextView.setText("Statistics");
topRelativeLayout.addView(headerTextView);

final ScrollView scrollView = new ScrollView(this);
mainLinearLayout.addView(scrollView);

final LinearLayout linearLayout = new LinearLayout(this);
scrollView.addView(linearLayout);

JSONArray users = response.getJSONArray("statistics");
for (int i = 0; i < users.length(); i++) {
    JSONObject result = users.getJSONObject(i);

    String id = result.getString("id");
    String userName = result.getString("username");

    int lastInsertIdInt = Integer.parseInt(id);

    statistics = new TextView[users.length()];
    statistics[i] = new TextView(getApplicationContext());
    statistics[i].setText(id + userName);
    linearLayout.addView(statistics[i]);

    if(lastInsertIdInt == users.length()) {
        statistics[i].setTextColor(Color.RED);
        scrollView.scrollTo(0, statistics[i].getBottom()); // doesn't work
    }
}

谢谢!

最佳答案

我是这样解决这个问题的:

statistics[i].getParent().requestChildFocus(statistics[i], statistics[i]);

关于java - 如何从 Android 数组中获取 textView 的滚动位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33916185/

相关文章:

Android:从android gallery app中选择后从mediaStore获取路径

javascript - 如何使用 jQuery 创建动画向下滚动按钮

java - 使用 API 网关进行 Cognito 用户池身份验证和授权

java - 请求大小为 0 的 Android Java Sqlite 异常索引 0

android - 如何在Android底部栏中添加菜单按钮?

jquery - 滚动 - 单击无法在固定侧边栏内滚动自定义滚动条

python - 滚动 tkinter TreeView 时如何移动弹出窗口?

java - 在不使用文件的情况下从 JAXB 注释类生成 XSD

java - 打印每个 JAX-RS 服务的 JSON 请求/响应

android - 如何确保 finish() 销毁 Activity 并清除所有变量?