如何滚动在android系统按钮点击一个Horizo​​ntalScrollView? [英] How to scroll a HorizontalScrollView on button click in android?

查看:180
本文介绍了如何滚动在android系统按钮点击一个Horizo​​ntalScrollView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的下一步和$ P $ Android应用水平滚动型pvious buttons.I要显示这些按钮只有当scollview需要scrolling.ie,滚动视图的内容宽度超过width.Also要隐藏$ P显示到达第一个和最后一个项目时$ pvious和下一步按钮respectively.How到下一个/ previous项目当点击这些按钮?

I have horizontal scrollview in my android app with Next and Previous buttons.I want to show the these buttons only when the scollview needs scrolling.ie,width of scrollview content exceeds display width.Also want to hide previous and Next buttons when reaching first and last items respectively.How to to next/previous items when click on these buttons?

的main.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 值不适合我是正确的。如何找到这个最大scrollvalue?
由于提前

The 'maxScrollX' value is not correct for me.How to find maximum scrollvalue for this? Thanks in Advance

推荐答案

devu

PLZ看看以下链接

1) HTTP://android-er.blogspot。在/ 2012/07 /实施图库 - like.html

2)http://androiddreamers.blogspot.in/2012/09/horizontal-scroll-view-example.html

3) HTTP://$c$c.google.com / p / mobyfactory-uiwidgets-的Andr​​oid /

让我知道如果u - [R遇到的任何问题。

Let me know if u r facing any issues

感谢

这篇关于如何滚动在android系统按钮点击一个Horizo​​ntalScrollView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆