【问题标题】:How to scroll a HorizontalScrollView on button click in android?如何在android中单击按钮时滚动Horizo​​ntalScrollView?
【发布时间】:2013-02-15 05:28:15
【问题描述】:

我的 android 应用程序中有水平滚动视图,带有下一个和上一个按钮。我只想在 scollview 需要滚动时显示这些按钮。即,滚动视图内容的宽度超过显示宽度。还想隐藏上一个和下一个按钮时分别到达第一个和最后一个项目。单击这些按钮时如何到达下一个/上一个项目?

ma​​in.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mainLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff" >

        <Button
            android:id="@+id/btnPrevoius"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:text="Previous"
            android:visibility="gone" />

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_toLeftOf="@+id/btnNext"
            android:layout_toRightOf="@+id/btnPrevoius"
            android:fillViewport="true" >

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
            </LinearLayout>
        </HorizontalScrollView>

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="Next"
            android:visibility="gone" />

    </RelativeLayout>

活动

 public class SampleActivity extends Activity {
            private static LinearLayout linearLayout;
            private static HorizontalScrollView horizontalScrollView;
            private static Button btnPrevious;
            private static Button btnNext;
            private static int displayWidth = 0;
            private static int arrowWidth = 0;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
                linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
                btnPrevious = (Button) findViewById(R.id.btnPrevoius);
                btnNext = (Button) findViewById(R.id.btnNext);
                for (int i = 0; i < 15; i++) {
                    Button button = new Button(this);
                    button.setTag(i);
                    button.setText("---");
                    linearLayout.addView(button);
                }
                ViewTreeObserver vto = linearLayout.getViewTreeObserver();
                vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        ViewTreeObserver obs = linearLayout.getViewTreeObserver();
                        obs.removeGlobalOnLayoutListener(this);
                        Display display = getWindowManager().getDefaultDisplay();
                        displayWidth = display.getWidth();
                        if (linearLayout.getMeasuredWidth() > (displayWidth - 40)) {
                            btnPrevious.setVisibility(View.VISIBLE);
                            btnNext.setVisibility(View.VISIBLE);
                        }
                    }

                });
                btnPrevious.setOnClickListener(listnerLeftArrowButton);
                horizontalScrollView.setOnTouchListener(listenerScrollViewTouch);
            }

            private OnTouchListener listenerScrollViewTouch = new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    showHideViews();
                    return false;
                }
            };

            private OnClickListener listnerLeftArrowButton = new OnClickListener() {

                @Override
                public void onClick(View v) {
                    horizontalScrollView.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(0, 0));
                }
            };


        public static void showHideViews() {
            int maxScrollX = horizontalScrollView.getChildAt(0).getMeasuredWidth()- displayWidth;
            Log.e("TestProjectActivity", "scroll X = " +horizontalScrollView.getScrollX() );
            Log.i("TestProjectActivity", "scroll Width = " +horizontalScrollView.getMeasuredWidth() );
            Log.d("TestProjectActivity", "Max scroll X = " + maxScrollX);

            if (horizontalScrollView.getScrollX() == 0) {
                hideLeftArrow();
            } else {
                showLeftArrow();
            }
            if (horizontalScrollView.getScrollX() == maxScrollX) {
                showRightArrow();
            } else {
                //hideRightArrow();
            }
        }

        private static void hideLeftArrow() {
            btnPrevious.setVisibility(View.GONE);
        }

        private static void showLeftArrow() {
            btnPrevious.setVisibility(View.VISIBLE);
        }

        private static void hideRightArrow() {
            btnNext.setVisibility(View.GONE);
        }

        private static void showRightArrow() {
            btnNext.setVisibility(View.VISIBLE);
        }
    }

'maxScrollX' 值对我来说不正确。如何找到最大滚动值? 提前致谢

【问题讨论】:

    标签: android horizontal-scrolling horizontalscrollview


    【解决方案1】:

    这可能来得有点晚,但对于那些将面临这个问题的人,我建议其他解决方案。

    首先,使用与 Horizo​​ntalScrollView 不同的组件。以下是选项:

    选项 1: Horizontal ListView - 将此类添加到您的项目中(创建一个单独的包,例如 com.yourproject.widgets)。您还需要创建自定义适配器,请参阅example 中的操作方法。我建议您创建单独的适配器类(exp.Horizo​​ntalListViewAdapter)并将其放入已创建的 com.yourproject.widgets 包中。

    • 将此小部件添加到 xml 中的布局中(将其放在需要模拟滚动行为的按钮之间),您需要添加如下内容:
    <com.yourproject.widgets.HorizontalListView
                android:id="@+id/hList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
    • 在使用小部件的 Activity/Fragment 中引用这个(连同按钮)
    HorizontalListView mHList = (HorizontalListView) findViewById (R.id.hList); 
    Button bPrevoius = (Button) findViewById (R.id.btnPrevoius);
    Button bNext = (Button) findViewById (R.id.btnNext);
    
    • 将 onClickListeners 添加到按钮。使用 Horizo​​ntalListView 小部件中预定义的 scrollTo() 函数。正如您在代码中看到的,滚动需要 int dp 值。如果要向右滚动(下一个),请添加正值,如果要向左滚动(上一个),请使用负值:

      bPrevoius.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              //value 500 is arbitrarily given. if you want to achieve 
              //element-by-element scroll you should get the width of the 
              //previous element dynamically or if the elements of the 
              //list have uniform width just put that value instead
               mHList.scrollTo(-500);
              //if it's the first/last element you can bPrevoius.setEnabled(false)
      
          }
      });
      
      
      bNext.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              mHList.scrollTo(500);
      
          }
      });
      

    选项 2: 此问题的最新解决方案可能是 Android L 中引入的新小部件 RecyclerView(添加 android:scrollbars="vertical" 似乎可以解决问题;除此之外应该具有常规的 ListView 行为)。有关更多信息,请查看官方文档。

    【讨论】:

      【解决方案2】:

      【讨论】:

      • 对不起。我不明白这些帖子如何解决我的问题。
      • @DevuSoman 你看过我发布的链接吗,水平滚动视图以这种方式工作。另外,如果你想隐藏这些按钮,你已经将第一个条件的启用设置为 false,反之亦然。对于执行下一个前一个你必须在相应的按钮上应用 onclick 监听器,并根据你的选择做你想做的事......
      • @DevuSoman 你到底想要什么......你必须设置填充和小的布局调整。你尝试过我发布的链接吗?只需在按钮上应用点击侦听器..根据您的选择。您想在点击时做什么?
      • 按钮点击工作正常。但是当滚动视图滚动到最后一项时我无法隐藏下一个按钮
      【解决方案3】:

      在钛应用加速器中,您可以使用 scrollableView 来做到这一点

      var scrollableView = Ti.UI.createScrollableView({
                  showPagingControl:true,
                  scrollingEnabled: true,     
                  top: 360                             
      });
      

      然后,您可以循环运行所有图像或您拥有的任何内容,并将它们添加到此视图中。

      for(循环) {
      eval("var view"+i+"=Ti.UI.createView();");

      profile_image = Ti.UI.createImageView({
                      image: result[0]['profile_image'],
                      left:15,
                      width:82,
                      height:104,
                      top: 0
                  });
      
      eval("view"+i+".add(profile_image);");
      
      eval("scrollableView.addView(view"+i+");");
      

      }

      mywin.add(scrollableView);

      【讨论】:

        猜你喜欢
        • 2014-06-17
        • 1970-01-01
        • 2013-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多