用于在Android中的Horizo​​ntalScrollView中向左和向右滚动的按钮

时间:2014-04-30 17:01:56

标签: java android android-layout scroll horizontal-scrolling

我有一个' Horizo​​ntalScrollView'的子类。其中有可变大小的图像。我想在Horizo​​ntalScrollView下面左右两个按钮显示。单击左键时,horizo​​ntalscollView应向左滚动左侧第一个不可见图像的大小,现在此图像可见,右键也相同。如果在那一侧到达列表的末尾,我也希望相应的按钮消失。

有人可以帮我这么做吗

1 个答案:

答案 0 :(得分:0)

正确执行侦听器,如下所示: 此方法获取图像ID数组并滚动到数据中提供的数据 适当使用getLeft和getRight

        /**
             * handles button presses that requiere extra data to be passed along 
             * for dynamic created buttons
             * in this app it uses mainly for the navigation bar buttons 
             * @author new
             *
             */
            public class ButtonHandler implements OnClickListener,Runnable 
            {
                private int_data;//current left or right location
                private ArrayList<Integer> _resultId;//the id's of the imagess
                private Activity _activity;

                public ButtonHandler(String data,ArrayList<Integer> resultId,Activity activity)
                {
                    _data=data;
                    _resultId=resultId;
                    _activity=activity;
                }

                @Override
                public void run() 
                {
                     View currentView=_activity.findViewById(_resultId.get(_data));
                     ScrollView scrollView=(ScrollView) _activity.findViewById(R.id.resultScroll);
                     scrollView.scrollTo(0, currentView.getLeft());
//get new image for nextClick
                     Rect scrollBounds = new Rect();
                     scrollView.getHitRect(scrollBounds);
                    for (Int i:_resultId)
                    {
                        if (_resultId.getLocalVisibleRect(scrollBounds)) 
                        {// Any portion of the imageView, even a single pixel, is within the visible window
                            data=i;

                         }
                     }
                }

                @Override
                public void onClick(View v) 
                {
                    new Handler().post(this);

                }

            }