android - 在滚动 Horizo​​ntalScrollview 的同时获取屏幕中心的项目位置

标签 android horizontalscrollview

我在 Horizo​​ntalcsrollView 中动态加载了一些(1000)张图片。用户可以前后滚动 Horizo​​ntalScrollView
我需要的是在滚动时找到屏幕中心的项目位置。 FYR,我已经从屏幕中心开始 Horizo​​ntalScrollView 并在中心结束。
我已经尝试使用 Horizo​​ntalScrollviewaddOnScrollChangedListener,但是我无法获得中心项目的确切位置。

编辑: 滚动时尝试获取 x 位置和左侧位置,但始终返回 0。
请注意,我已经为我的 Horizo​​ntalScrollView 设置了左右填充。
它始终从屏幕中心开始并在屏幕中心结束。

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

最佳答案

I have dynamically loaded some(1000) images inside the Horizontal scrollview. The user can scroll the Horizontal scrollview front and back. What I need is, to find the item position at the center of the screen while scrolling. FYR, I have start the Horizontal scrollview from the center of the screen and end at the center. I have tried with addOnScrollChangedListener of the HorizontalScrollview, but I couldn't get the exact position of the center item.

您不能使用水平 ScrollView 来加载那么多图像,否则您会滞后设备并且您的应用程序将停止响应。 Scollviews 用于加载简单的 TextView 或少量低分辨率图像(如图标)。您应该做的是使用 Recycler View 并将方向设置为水平。

 LinearLayoutManager listManager = new LinearLayoutManager( getActivity(),LinearLayoutManager.HORIZONTAL, false);
    horizontalRecyclerView.setHasFixedSize(false);
    horizontalRecyclerView.setLayoutManager(listManager);
    horizontalRecyclerView.setAdapter(new ContentFileterAdapter());

然后您可以添加一个 viewholder 和一个保存图像的 adapter。您还应该使用缓存库,例如 UIL加载那么多图像

    public static class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView mImageView;
            public ViewHolder(View itemView) {
                super(itemView);
                mImageView=itemView.findViewById(R.id.yourImageView);
            }
        }
private class ImagesAdapter extends RecyclerView.Adapter<ViewHolder>{
        private String[] mImagesPaths;
        public ImagesAdapter(String[] mImagesPaths){
            this.mImagesPaths = mImagesPaths;
          }
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup container, int viewType) {

            return new ViewHolder(LayoutInflater.from(container.getContext()).inflate(R.layout.yourAdapterItemLayout, container, false));
        }

        @Override
        public int getItemViewType(int position) {

            return 0;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
ImageLoader.getInstance().displayImage(Uri.decode(mImagesPaths[position]), holder.mImageView);}

        @Override
        public int getItemCount() {
            return mImagesPaths.length;
        }
    }

然后要找到您的中心 view 或位置,您只需调用 LinearLayoutManager

int lastindex = listManager.findLastCompletelyVisibleItemPosition();
int firstindex = listManager.findFirstCompletelyVisibleItemPosition();

//The middle visible position
int centerindex = firstindex/2 + lastindex/2 + (firstindex%2 + lastindex%2)/2;

//The middle visible view
View   view=(View)listManager.findViewByPosition(centerindex );

关于android - 在滚动 Horizo​​ntalScrollview 的同时获取屏幕中心的项目位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52553004/

相关文章:

android - 在android.mk系统应用android中包含.so

android - 如何禁用 Horizo​​ntalScrollView 中的滚动?

android - 如何像 Gallery 一样实现 Horizo​​ntalScrollView?

android - Espresso : How do I scroll to an item in a HorizontalScrollView by index?

Android - Canvas onDraw 不触发

java - 服务器到 Firebase HTTP POST 结果为响应消息 200

android - Android NDK 中的 FFMPEG - 无法加载库 "libavfilter.so"

android - 每隔一段时间在后台执行网络服务

android - 如何将 StreamHandler 与无法访问的对象的监听器连接起来?

android - Horizo​​ntalScrollView 将 GridView 缩小为细线