title: How to get First Visible child item of android Horizontal scrollview on scrollChange event
body: <p>I need to find the first visible child item of android Horizontal scrollview on scrollChange event. I have followed this link <a href="https://stackoverflow.com/questions/24550862/how-to-get-first-visible-child-item-of-android-scrollview-on-scrollchange-event/24585563">How to get First Visible child item of android scrollview on scrollChange event</a>
  but couldn't find what rowView is.</p>
  
  <pre><code>    horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
      @Override
      public void onScrollChanged() {
          int scrollPos = horizontalScrollView1.getScrollX();
          Log.e("scroll x:", scrollPos + "");           
      }
  });
  </code></pre>
  
  <p>layout file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
      &lt;HorizontalScrollView
          android:id="@+id/horizontalScrollView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_gravity="center_horizontal"
          android:layout_marginBottom="15dp"
          android:fillViewport="true"
          android:scrollbars="none"&gt;
          &lt;RelativeLayout
          android:id="@+id/layLandmarks"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"&gt;
              &lt;View
                  android:id="@+id/viewWhite"
                  android:layout_width="match_parent"
                  android:layout_height="10dp"
                  android:layout_centerInParent="true"
                  android:background="@color/white" /&gt;
  
          &lt;/RelativeLayout&gt;
      &lt;/HorizontalScrollView&gt;   
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Class file:</p>
  
  <pre><code>    ImageView iv;
  RelativeLayout layLandmarks = (RelativeLayout) findViewById(R.id.layLandmarks);
  FrameLayout.LayoutParams lp1 = new FrameLayout.LayoutParams(2000, FrameLayout.LayoutParams.WRAP_CONTENT);
  layLandmarks.setLayoutParams(lp1);
  JSONObject landmarks = jsonObject1.getString("landmarks");
  JSONArray jsonArray1 = new JSONArray(landmarks);
  for (int j = 0; j &lt; jsonArray1.length(); j++) {
  JSONObject jsonObject2 = jsonArray1.getJSONObject(0);
  
  landmarkId = jsonObject2.getString("id");
  landmarkName = jsonObject2.getString("title");
  landmarkDesc = jsonObject2.getString("description");
  latitude = jsonObject2.getDouble("latitude");
  longitude = jsonObject2.getDouble("longitude");
  video_time = jsonObject2.getString("video_time_in_sec");
  //   Log.e("video_time_in_sec", video_time);
  landmarkIcon = jsonObject2.getString("image");
  iv = new ImageView(VideoPreviewWithRecycler.this);
  iv.setId(i);
  Picasso
          .with(VideoPreviewWithRecycler.this)
          .load(landmarkIcon)
          .resize(40, 45)
          .into(iv);
  params = new RelativeLayout.LayoutParams(40, 45);
  
  params.topMargin = 0;
  
  params.leftMargin = Integer.parseInt(video_time);
  params.addRule(RelativeLayout.RIGHT_OF, 0);
  
  layLandmarks.addView(iv, params);
  }
  </code></pre>
  
  <p>Update:</p>
  
  <pre><code> horizontalScrollView1.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                          @Override
                          public void onScrollChange(View v, final int scrollX, int scrollY, final int oldScrollX, int oldScrollY) {
  
                              horizontalScrollView1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                                  @Override
                                  public void onGlobalLayout() {
  
                                     int firstVisibleItemIndex= getFirstVisibleItem1(horizontalScrollView1);
                                     Log.e("firstVisibleItemIndex &gt;&gt;",firstVisibleItemIndex+"");
                                  }
  
                              });
                          }
                      });
  
  
   private int getFirstVisibleItem1(HorizontalScrollView scrollView) {
          ViewGroup viewGroup = (ViewGroup) scrollView.getChildAt(0); // cast to ViewGroup here because I think we only need to getFirstVisibleItem if ScrollView have multiple items
          for (int i = 0; i &lt; viewGroup.getChildCount(); i++) {
              View view = viewGroup.getChildAt(i);
              if (view.getX() + view.getWidth() &gt;= scrollView.getScrollX()) {
                  return i; // if you want to receive the position, return  i here
              }
          }
          return 0;
      }
  </code></pre>