I have a HorizontalScrollView containing a LinearLayout. On the screen, I have a button that will add new views to LinearLayout at runtime, and I would like the scroll to scroll to the end of the list when adding a new view.
Almost everything works for me - except that it always scrolls one view to the last view. It seems that it scrolls without a preliminary calculation of the inclusion of a new view.
In my application, I use a custom View object, but I made a small test application that uses ImageView and has the same symptom. I tried various things, such as requestLayout () in both Layout and ScrollView, I tried scrollTo (Integer.MAX_VALUE) and it scrolled in netherverse :) Am I upsetting the UI thread or something like that?
======
public class Main extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b = (Button) findViewById(R.id.addButton); b.setOnClickListener(new AddListener()); add(); } private void add() { LinearLayout l = (LinearLayout) findViewById(R.id.list); HorizontalScrollView s = (HorizontalScrollView) findViewById(R.id.scroller); ImageView i = new ImageView(getApplicationContext()); i.setImageResource(android.R.drawable.btn_star_big_on); l.addView(i); s.fullScroll(HorizontalScrollView.FOCUS_RIGHT); } private class AddListener implements View.OnClickListener { @Override public void onClick(View v) { add(); } } }
XML format:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <HorizontalScrollView android:id="@+id/scroller" android:layout_width="wrap_content" android:layout_height="wrap_content" android:scrollbarSize="50px"> <LinearLayout android:id="@+id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4px"/> </HorizontalScrollView> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center"> <Button android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingLeft="80px" android:paddingRight="80px" android:paddingTop="40px" android:paddingBottom="40px" android:text="Add"/> </LinearLayout> </LinearLayout>
android user-interface horizontal-scrolling
Rick Barkhouse Jan 18 2018-11-11T00: 00Z
source share