【问题标题】:Get Item position, at the center of the screen, while scrolling the HorizontalScrollview获取项目位置,在屏幕的中心,同时滚动 Horizo​​ntalScrollview
【发布时间】: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


【解决方案1】:

我已经在 Horizo​​ntal 中动态加载了一些(1000)图像 滚动视图。用户可以滚动水平滚动视图前面和 背部。我需要的是,找到在中心的项目位置 滚动时的屏幕。 FYR,我已经启动了水平滚动视图 从屏幕中心到中心结束。我试过了 Horizo​​ntalScrollview的addOnScrollChangedListener,但我不能 获取中心项目的确切位置。

您不能使用horizontal scrollview 来加载这么多图像,您将滞后于设备并且您的应用程序将停止响应。 Scollviews 用于加载简单的文本视图或少量低分辨率图像,例如图标。您应该做的是使用Recycler 视图并将方向设置为水平。

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

【讨论】:

    【解决方案2】:

    你可以像这样使用 Recylerview:

    mProductBrowserAdapter = new ProductsAdapterBrowser();
            mRcvLatestProducts.setNestedScrollingEnabled(true);
            mRcvLatestProducts.setHasFixedSize(false);
            StaggeredGridLayoutManager staggeredGridLayoutManager =
                    new StaggeredGridLayoutManager(1, LinearLayoutManager.HORIZONTAL);
            mRcvLatestProducts.setLayoutManager(staggeredGridLayoutManager);
            mRcvLatestProducts.addItemDecoratiiiiion(new GridSpacingItemDecoration(1, 0, false));
            mRcvLatestProducts.setAdapter(mProductBrowserAdapter);
    

    适配器的视图:

      <android.support.v7.widget.AppCompatImageView
                    android:id="@+id/item_product_image_imv"
                    android:layout_width="160dp"
                    android:layout_height="160dp"
                    android:adjustViewBounds="true"
                    android:background="@android:color/transparent"
                    android:scaleType="fitCenter"
                  />
    

    我推荐使用 Glide 来加载图片。

    dependencies {
      implementation 'com.github.bumptech.glide:glide:4.8.0'
      annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
    }
    

    https://github.com/bumptech/glide

    【讨论】:

      【解决方案3】:

      您可以将 PagerSnapHelper 与 recyclerview 一起使用,它会对您有所帮助 像这样

         binding.rvPosts.setLayoutManager(layoutManager);
         SnapHelper snapHelper = new PagerSnapHelper();
         snapHelper.attachToRecyclerView(binding.rvPosts);
         binding.rvPosts.setHasFixedSize(true);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-12
        • 2012-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多