Sunday, 7 October 2018

How to get First Visible child item of android Horizontal scrollview on scrollChange event

I need to find the first visible child item of android Horizontal scrollview on scrollChange event. I have followed this link How to get First Visible child item of android scrollview on scrollChange event but couldn't find what rowView is.

    horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
    @Override
    public void onScrollChanged() {
        int scrollPos = horizontalScrollView1.getScrollX();
        Log.e("scroll x:", scrollPos + "");           
    }
});

layout file

<?xml version="1.0" encoding="utf-8"?>
<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">
    <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">
        <RelativeLayout
        android:id="@+id/layLandmarks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
            <View
                android:id="@+id/viewWhite"
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_centerInParent="true"
                android:background="@color/white" />

        </RelativeLayout>
    </HorizontalScrollView>   
</RelativeLayout>

Class file:

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



from How to get First Visible child item of android Horizontal scrollview on scrollChange event

No comments:

Post a Comment