【发布时间】:2019-03-04 07:32:08
【问题描述】:
我在HorizontalcsrollView 中动态加载了一些(1000)张图片。用户可以前后滚动HorizontalScrollView。
我需要的是,在滚动时找到屏幕中心的项目位置。 FYR,我已经从屏幕中心开始 HorizontalScrollView 并在中心结束。
我曾尝试使用HorizontalScrollview 中的addOnScrollChangedListener,但我无法获得中心项目的确切位置。
编辑:
滚动时尝试获取 x 位置和左侧位置,但始终返回 0。
请注意,我已经为我的HorizontalScrollView 设置了左右填充。
它总是从屏幕中心开始,到屏幕中心结束。
horizontalScrollView1.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int x1 = horizontalScrollView1.getLeft();
int getX=(int)horizontalScrollView1.getX();
}
});
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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);
}
【问题讨论】:
-
您正在使用带有水平滚动视图的回收器视图?
-
不,我只使用水平滚动视图。
-
你能发布你的xml吗?你是怎么用的
-
以 dp 为单位计算设备屏幕的宽度,然后将宽度除以 2 得到中心。然后将结果除以 dp 中的项目宽度的大小。结果将是屏幕中心的项目编号
-
让我尝试恢复 Muhammad Hassaan
标签: android horizontalscrollview