需要将单击的项目放置在屏幕中央的水平滚动视图中

时间:2018-08-24 06:52:09

标签: android horizontalscrollview

我以线性布局动态填充了图像,该图像放置在Horizo​​ntal Scrollview中。我启用了自动滚动水平滚动视图的功能,延迟了1分钟。我已经将左右填充设置为水平滚动视图,因为它是从屏幕中央出现的,并滚动到左侧并在屏幕中央结束。自动滚动效果很好。我的问题是,当我单击滚动视图中的图像时,需要将该图像移到屏幕中央,然后继续自动滚动。我已经从问题Android : Locating the item at center position of the screen after selecting it ,In the horizontalscrollview的答案中搜索并找到答案来放置单击的项目中心。但就我而言,点击的项目并没有居中。

final int duration = 1000;
        final int pixelsToMove = 50;
        private final Handler mHandler = new Handler(Looper.getMainLooper());
        private final Runnable SCROLLING_RUNNABLE = new Runnable() {
            @Override
            public void run() {            
                    horizontalScrollView1.smoothScrollBy(pixelsToMove, 0);               
                    mHandler.postDelayed(this, duration);            
            }
        };


    HorizontalScrollView horizontalScrollView1 = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
    LinearLayout layLandmarks = (LinearLayout) findViewById(R.id.layLandmarks);
    final ImageView[] dynamic_imageView = new ImageView[jsonArray.length()]; 
     for (int i = 0; i < jsonArray.length(); i++) {
     dynamic_imageView[i] = new ImageView(Activity.this);
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(70, 70);
                            dynamic_imageView[i].setLayoutParams(lp);
                            Glide.with(Activity.this)
                                    .load(imageUrl)
                                    .override(30, 30)
                                    .error(R.drawable.button_white_color1)
                                    .into(dynamic_imageView[i]);
                            dynamic_imageView[i].setId(i);
                            dynamic_imageView[i].setTag("landmarkName");
                              layLandmarks.addView(dynamic_imageView[i], i);
                               horizontalScrollView1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                                @Override
                                public void onGlobalLayout() {
                                    int padding = getResources().getDisplayMetrics().widthPixels / 2;
                                    horizontalScrollView1.setPadding(padding, 0, padding, 0);
                                    horizontalScrollView1.setClipToPadding(false);
                                }
                            });
                               Display display = getWindowManager().getDefaultDisplay();
                            Point size = new Point();
                            display.getSize(size);
                            final int width = size.x;
                            dynamic_imageView[i].setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                 int center = (width - dynamic_imageView[i].getWidth())/2;
                                    horizontalScrollView1.scrollTo(dynamic_imageView[i].getLeft() - center, dynamic_imageView[i].getTop());
                                    }
                            });
     }
       mHandler.postDelayed(SCROLLING_RUNNABLE, 100);

添加我的布局文件:

<?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"
    android:background="@drawable/black_transparent_background">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/btnBack"
        android:layout_width="15dp"
        android:layout_height="20dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="25dp"
        android:background="@drawable/back_arrow" />

    <TextView
        android:id="@+id/txtEventTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="25dp"
        android:layout_toRightOf="@id/btnBack"
        android:text="Santa Rosa Marathon"
        android:textColor="@color/white"
        android:textSize="@dimen/text_24" />

    <Button
        android:id="@+id/btnCamera"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_margin="20dp"
        android:background="@drawable/camera_icon_white" />


    <RelativeLayout

        android:id="@+id/laySeekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">


        <RelativeLayout
            android:id="@+id/seekBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="17dp"
            android:layout_marginRight="17dp"
            android:background="@drawable/seekbar_layout_background">


            <SeekBar

                android:id="@+id/seekBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:progressBackgroundTint="@color/white"
                android:thumb="@drawable/seekbar_thumb"
                android:visibility="gone"

                />


        </RelativeLayout>


        <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:fillViewport="true"
            android:scrollbars="none">


            <LinearLayout

                android:id="@+id/layLandmarks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:orientation="horizontal">


            </LinearLayout>
        </HorizontalScrollView>


    </RelativeLayout>


    <RelativeLayout
        android:id="@+id/laySeekbarText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnCircle"
        android:layout_centerInParent="true"
        android:layout_marginBottom="5dp"
        android:background="@drawable/seekbar_text_back">

        <TextView
            android:id="@+id/seekBarProgressValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:padding="2dp"
            android:text="0 mi"
            android:textColor="@color/black" />

        <TextView
            android:id="@+id/txtVideoElevationValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/seekBarProgressValue"
            android:layout_centerHorizontal="true"
            android:padding="2dp"
            android:text="0 sqft"
            android:textColor="@color/black" />
    </RelativeLayout>

    <Button

        android:id="@+id/btnCircle"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:background="@drawable/seekbar_thumb" />

    <Button
        android:id="@+id/btnStart"
        android:layout_width="20dp"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:background="@drawable/start_icon" />

    <ProgressBar
        android:id="@+id/videoProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />
</RelativeLayout>

0 个答案:

没有答案