代码之家  ›  专栏  ›  技术社区  ›  Manikandan

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

  •  0
  • Manikandan  · 技术社区  · 8 年前

    我已经在一个线性布局中动态填充图像,这个布局放置在水平滚动视图中。我已经启用了自动滚动水平滚动视图,延迟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 回复  |  直到 7 年前
    推荐文章