scrollTo() for horizontalScrollView not working

react-native android scrollto not working
scrollview ref not working
this refs scrollview scrollto is not a function
null is not an object (evaluating this scrollview scrollto)
react native scrollview scrolltoend not working
react native scrollview not working on android
react native scrollview not scrolling
scrollview react native

I inflate another layout to appear below some view in my current layout.

This is done like this:

       LayoutInflater vi = (LayoutInflater) getActivity().getApplicationContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rootView = vi.inflate(R.layout.horizontal_scroll_view, null);

    horizontalScrollView = (HorizontalScrollView) rootView.findViewById(R.id.hsv_suggestions_scroll_view);
    LinearLayout suggestionsContainer = (LinearLayout) horizontalScrollView.findViewById(R.id.ll_suggestions_container);

and I can confirm that it appears in the right place since I add some Views in it after a while and they all appear.

The layout I inflate is :

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hsv_suggestions_scroll_view"
android:scrollbars="none" android:layout_gravity="center_horizontal"
android:paddingTop="16dp" android:fillViewport="true"
android:layout_width="match_parent" android:layout_height="wrap_content">

    <LinearLayout
      android:id="@+id/ll_suggestions_container"
      android:gravity="center_horizontal"    android:orientation="horizontal"
      android:layout_width="wrap_content" android:layout_height="wrap_content">

</LinearLayout>

just a HorizontalScrollView with a LinearLayout as a child. The Views I add later are all of them TextViews.

Now after a user action (write some text on an editText) I'm trying to scroll to that View and highlight it. Highlight works. What does not work is scroll.

I have tried :

 horizontalScrollView.postDelayed(new Runnable() {
   @Override
   public void run() {

   horizontalScrollView.smoothScrollTo(scrollTo, 0);
   }
}, 300);

where variable scrollTo is what I get when I apply getLeft() to the View I wanna scroll to. I can confirm that it takes various values.

Anyone can help me with that ?

Switch to using a RecyclerView with a LinearLayoutManager with orientation set to Horizontal. Like this:

scroll_view.xml

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/scrollView"
    android:scrollbars="none"
    android:layout_gravity="center_horizontal"
    android:paddingTop="16dp"
    android:fillViewport="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
    android:orientation="horizontal">
    <LinearLayout
        android:id="@+id/scrollContainer"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</android.support.v7.widget.RecyclerView>

Note: the layoutManager tag is required here for the layout to inflate. After it's inflated, we're going to set it programatically as well because otherwise we'll get an exception because the RecyclerView basically disposes of it before it's done with it.

OptionAdapter.java

public class OptionAdapter extends RecyclerView.Adapter<OptionAdapter.OptionHolder> {

    private Context context;
    private LayoutInflater inflater;

    private ArrayList<String> options;

    public OptionAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);

        options = new ArrayList<>();
    }

    @Override
    public OptionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new OptionHolder(inflater.inflate(R.layout.textview, parent, false));
    }

    @Override
    public void onBindViewHolder(OptionHolder holder, int position) {
        String option = options.get(position);

        ((TextView) holder.itemView).setText(option);
    }

    @Override
    public int getItemCount() {
        return options != null ? options.size() : 0;
    }

    public ArrayList<String> getOptions() {
        return options;
    }

    public void addOption(String option, Integer index) {
        if (index != null && index <= options.size()) {
            options.add(index, option);
        } else {
            options.add(option);
        }

        notifyDataSetChanged();
    }

    public class OptionHolder extends RecyclerView.ViewHolder {

        public OptionHolder(View itemView) {
            super(itemView);
        }
    }
}

text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />

Setting everything up

final LayoutInflater inflater = LayoutInflater.from(this);
final RecyclerView scrollView = (RecyclerView) inflater.inflate(R.layout.scroll_view, container, false);
scrollView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));

final OptionAdapter adapter = new OptionAdapter(this);
scrollView.setAdapter(adapter);

container here is whatever view is going to be holding the RecyclerView. In my code, I've got it in a LinearLayout.

Adding a view to the list

adapter.addOption("The Added One", null);

Or if you want to add it to a specific position in the list.

adapter.addOption("The Added One", position);

Scrolling to a specific position

scrollView.smoothScrollToPosition(position);

Scrolling to a specific item in the list

scrollView.smoothScrollToPosition(adapter.getOptions().indexOf("ItemText"));

Hope it works for you!

ScrollTo not working on Horizontal ScrollView · Issue #1173 , ScrollTo not working on Horizontal ScrollView How to reproduce Reproducible Demo Steps to reproduce: Add a horizontal prop to ScrollView  I got my code to work by simply putting the scrollTo in a 100ms timeout. This indicates that the scrollview is not quite mounted at the first try. I'm actually fine with the timeout for my use case, but I want to solve this anyway because I don't like hacky solutions.

Instead of smoothScrollTo(), try using scrollTo(). Make sure that your getLeft() is really returning a value > 0;

android.widget.HorizontalScrollView.smoothScrollTo java code , smoothScrollTo (Showing top 20 results out of 315) postDelayed(new Runnable() { public void run() { horizontalScrollView. smoothScrollTo(left, 0); } else { // Scroll to menuWidth so menu isn't on screen. int left = menuWidth; scrollView. I tried using jQuery 1.11.0 and jQuery.scrollTo(2.1.2) but it doesn't work correctly. Earlier we had the scrollTo call on document.ready(), so the page would scroll to the right div. But now (after updating jQuery and jQuery.scrollTo) it scrolls to the right div and then continues to the point I was on the page when I refreshed the page.

Got a similar issue.

I guess the root cause is that the UI was not yet rendered at that moment, causing the scroll not to work properly.

I just had to wrap my call to smoothScrollTo into .post as follows:

mScrollView.post(new Runnable() {
    @Override
    public void run() {
        mScrollView.smoothScrollTo(xxx, yyy);
    }
});

Notice that I do a post directly on the scrollView to make sure it is executed after it is being rendered. For example, doing a getActivity().runOnUIThread() would not work in my case.

HorizontalScrollView ScrollTo(x,y) Not Working?, I have a HorizontalScrollView and I am trying to set the init offset x. After all the content is loaded and the OnLayout has ran to get the width,  just a HorizontalScrollView with a LinearLayout as a child. The Views I add later are all of them TextViews. Now after a user action (write some text on an editText) I'm trying to scroll to that View and highlight it. Highlight works. What does not work is scroll. I have tried :

HorizontalScrollView, HorizontalScrollView only supports horizontal scrolling. For vertical scrolling Like scrollTo(int, int) , but scroll smoothly instead of immediately. The scrollTo () method scrolls the document to the specified coordinates. Tip: Use the scrollBy () method to scroll a specified distance multiple times. Browser Support. window.scrollTo ( xpos, ypos) Parameter Values. Required. The coordinate to scroll to, along the x-axis (horizontal), in pixels. Required. The coordinate to scroll to, along

scrollTo() for horizontalScrollView not working, The TextView class also takes care of its own scrolling, so does not require a HorizontalScrollView only supports horizontal scrolling. scrollTo(x: Int, y: Int). Window.scrollTo() scrolls to a particular set of coordinates in the document. window.scrollTo(x-coord, y-coord) window.scrollTo(options) x-coord is the pixel along the horizontal axis of the document that you want displayed in the upper left. y-coord is the pixel along the vertical axis of the document that you want displayed in the upper left.

RecyclerView within NestedScrollView Scrolling Issue, not work is scroll. I have tried : horizontalScrollView.postDelayed(new Runnable() { #Override public void run() { horizontalScrollView.smoothScrollTo(​scrollTo  I'm desperatly trying to make the scrollTo plugin work on my website, and so far, no luck So, that's what I have in the head tags : <script type="text/javascript" jquery scrollTo not working - SOLVED ! - jQuery Forum

Comments
  • I just threw together this same situation, and it's working just fine for me. Have you tried putting a random number into your smoothScrollTo() to see of it will scroll for anything?
  • @idratherbeintheair yes I have tried adding some random numbers but didn't help
  • So it didn't scroll when you used random numbers either?
  • nope. That's why I wrote "not working" in the title. Maybe something with parent's View width or height ?
  • So basically the user types something, then you add that text to a TextView then add that to the container inside the ScrollView, then you want to scroll to that view, right? If that's the case, it's possible you're not waiting long enough after adding it to scroll to it. Try increasing your 300 millisecond wait on the postDelayed?
  • Works as expected! Thanks a lot ! Think it was some problem with the inflation of the horizontalScrollView but RecyclerView is great for those cases too.
  • smoothScrollTo() is working for me, and it gives a smoother UX.
  • I have tried both smoothScrollTo() and scrollTo(). I can confirm that getLeft() gives me values > 0
  • use debugger and check that it is entering the runnable. Also see your logcat when debugger pass over the smoothScrollTo() function.