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