0

在我的应用程序中,我有一个包含 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;  
                }
            });  
        } 
    }

问题:

一旦我移动搜索栏,水平滚动视图中的图片都消失了。

怎么了?提前感谢您的建议!

4

1 回答 1

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);
....

如果要查找视图的位置,可以使用getLocationInWindoworgetLocationOnScreen方法。

int[] location = new int[2];
yourView.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
于 2013-11-11T07:48:45.477 回答