简体   繁体   中英

android horizontal scrollview working with seekbar

In my app I have a horizontal scrollview of 6 duck images, while the horizontal scrollview can work properly, now I would like to add a SeekBar underneath the horizontal scrollview such that moving the seekbar will positioning the horizontal scrollview to the proper item.

I have written the codes as follows:

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

Horizontal Scrollview:

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

Question:

Once I move the seekbar, the pictures in the horizontal Scrollview all gone.

What is happening? Thanks in advance for your advice!

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

scrollTo expects x,y coordinates to scroll to a new position. But you give it R.drawable.d_duck400_5 as x value. This will be just a random integer produced by compiler. It is not the x coordinate you want to scroll.

Try something like this:

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

If you want to find the position of a view, you can go with getLocationInWindow or getLocationOnScreen methods.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM