android水平scrollview使用seekbar

时间:2013-11-11 07:32:12

标签: android seekbar horizontalscrollview

在我的应用程序中,我有一个6个鸭子图像的水平滚动视图,而水平滚动视图可以正常工作,现在我想在水平滚动视图下面添加一个SeekBar,以便移动搜索栏将水平滚动视图定位到正确的项目

我编写的代码如下:

搜索条:

    seekBar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() 
    {           
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) 
        {
            // TODO Auto-generated method stub              
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) 
        {
            // TODO Auto-generated method stub          
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
        {
            seekBar.setProgress(progress);

            if (progress ==5)  {mLinearLayout.scrollTo(R.drawable.d_duck400_5, 0);}
            if (progress ==4)  {mLinearLayout.scrollTo(R.drawable.d_duck400_4, 0);}
            if (progress ==3)  {mLinearLayout.scrollTo(R.drawable.d_duck400_3, 0);}
            if (progress ==2)  {mLinearLayout.scrollTo(R.drawable.d_duck400_2, 0);}
            if (progress ==1)  {mLinearLayout.scrollTo(R.drawable.d_duck400_1, 0);}
            if (progress ==0)  {mLinearLayout.scrollTo(R.drawable.d_duck400_0, 0);}
        }
    });         
}

水平滚动视图:

    private void getDeviceWidth() 
    {  
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);  
        mWidth = dm.widthPixels;  
    }  

    private void initView() 
    {  
        int[] imageArray = { R.drawable.d_duck400_0, R.drawable.d_duck400_1, R.drawable.d_duck400_2, 
                R.drawable.d_duck400_3, R.drawable.d_duck400_4, R.drawable.d_duck400_5};

//        int[] imageArray = { R.drawable.d_duck1_400, R.drawable.d_duck1_400, R.drawable.d_duck1_400, 
//              R.drawable.d_duck1_400, R.drawable.d_duck1_400, R.drawable.d_duck1_400};

        mLinearLayout = (LinearLayout) findViewById(R.id.scrollview_layout);
        mLinearLayout.removeAllViews();
        for (int i = 0; i < 6; i++) 
        {// Place 6 items into horizontalscrollview
            int width = mWidth /1;  
            LinearLayout itemLayout = (LinearLayout) LinearLayout.inflate(Duck.this, R.layout.d_scrollview_item, null);
            itemLayout.setLayoutParams(new LayoutParams(width,  LayoutParams.MATCH_PARENT));// set the width as 1 screen showing 3 items  
            itemLayout.setGravity(Gravity.CENTER_VERTICAL);

            ImageView mImageView = (ImageView) itemLayout.findViewById(R.id.imageview);  
            TextView mTextView = (TextView) itemLayout.findViewById(R.id.textview);  

            final String page = "The" + (i + 1) + "th page";  

            mTextView.setText(page);  
            mImageView.setBackgroundResource(imageArray[i]);  

            mLinearLayout.addView(itemLayout);  

            itemLayout.setOnTouchListener(new OnTouchListener() 
            {  
                public boolean onTouch(View v, MotionEvent event) 
                {  
//                    Toast.makeText(Duck.this, "Click" + page, Toast.LENGTH_SHORT).show();  
                    return false;  
                }
            });  
        } 
    }

问题:

移动搜索栏后,水平Scrollview中的图片全部消失。

发生了什么事?提前感谢您的建议!

1 个答案:

答案 0 :(得分:0)

if (progress ==5)  {mLinearLayout.scrollTo(R.drawable.d_duck400_5, 0);

scrollTo期望x,y坐标滚动到新位置。但是你给它R.drawable.d_duck400_5作为x值。这只是编译器生成的随机整数。它不是您要滚动的x坐标。

尝试这样的事情:

if (progress ==5)  {mLinearLayout.scrollTo(200, 0);
if (progress ==5)  {mLinearLayout.scrollTo(400, 0);
....

如果您想查找视图的位置,可以使用getLocationInWindowgetLocationOnScreen方法。

int[] location = new int[2];
yourView.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
相关问题