简体   繁体   中英

How to get scroll position to a textView from an array in Android?

I have created an array which contains a lots of textViews. I want to scroll down automatically to the statistics[i] textView . I tried to use scrollView.scrollTo(0, statistics[i].getBottom()); but nothing happend. Any ideas? This is my code:

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
    }
}

Thanks!

我以这种方式解决了这个问题:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM