gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:50:54 24 4
gpt4 key购买 nike

我创建了一个包含大量 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com