我需要在scrollchange事件上找到android水平scrollview的第一个可见子项。我已关注此链接How to get First Visible child item of android scrollview on scrollChange event
但找不到rowview是什么。
horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int scrollPos = horizontalScrollView1.getScrollX();
Log.e("scroll x:", scrollPos + "");
}
});
布局文件
<?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>
类文件:
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);
}
更新:
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 >>",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 < viewGroup.getChildCount(); i++) {
View view = viewGroup.getChildAt(i);
if (view.getX() + view.getWidth() >= scrollView.getScrollX()) {
return i; // if you want to receive the position, return i here
}
}
return 0;
}
最佳答案
我想可能会有帮助
private View getFirstVisibleItem(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 < viewGroup.getChildCount(); i++) {
View view = viewGroup.getChildAt(i);
if (view.getX() + view.getWidth() >= scrollView.getScrollX()) {
return view; // if you want to receive the position, return turn i here
}
}
return null;
}
想法是检查每个
view x position
和scroll x value