0

I want to implement a HorizontalScrollView with four TextViews having text say("First view", "Second View", "Third View", "Fourth View") such that HorizontalScrollView will display only one center TextView at one time and when user scrolls/swipe, he/she will be able to move to the next text only(irrespective of the velocity of scroll/swipe) so that i can display a different image corresponding to the text visible in HorizontalScrollView at that time.

It means if user is at "Second View" and want to see the "Fourth view" he has to see the "Third View" also.

I am new to the Android. kindly help!

Onur A.
  • 3,007
  • 3
  • 22
  • 37
neha
  • 9
  • 2

2 Answers2

2

In this example I override the fling() method and dividing the velocity by 4 causing the Fling to be weaker:

@Override
    public void fling(int velocityX, int velocityY) {
        mTouchState = TOUCH_STATE_FLING;
        final int x = getScrollX();
        final int y = getScrollY();

        mScroller.fling(x, y, velocityX/4, velocityY/4, Integer.MIN_VALUE,Integer.MAX_VALUE, Integer.MIN_VALUE,Integer.MAX_VALUE);

        invalidate();
    }
Gal Rom
  • 6,221
  • 3
  • 41
  • 33
0

Thanks!! After trying a lot. I came to a solution: I have used Gallery widget and customize it to serve my purpose.

//Customized Gallery Class: SlowGallery.java

public class SlowGallery extends Gallery {

public SlowGallery(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}

public SlowGallery(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public SlowGallery(Context context)
{
    super(context);
}


@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{

    //limit the max speed in either direction
    if (velocityX > 400.0f)
    {
        velocityX = 400.0f;
    }
    else if(velocityX < 400.0f)
    {
        velocityX = -400.0f;
    }
    return super.onFling(e1, e2, velocityX, velocityY);


    //return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 
{
    return false;
}

}

now in your main Activity Class add following code in oncreate function:

 Gallery gallery = (Gallery) findViewById(R.id.gallery);
    gallery.setAdapter(new ImageAdapter(this));

then create ImageAdapter class as:

public class ImageAdapter extends BaseAdapter {

    int mGalleryItemBackground;
    private Context mContext;
    private String[] mText = {
            "View 1","View 2", "View 3", "View 4"
    };
    public ImageAdapter(Context c) {
        mContext = c;
     }


@Override
public int getCount() {
    // TODO Auto-generated method stub
     return mText.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
            TextView textview = new TextView(mContext);             textview.setTextColor(Color.WHITE);
    textview.setText(mText[position]);
    textview.setFocusable(true);
    textview.setTextSize(16);
    textview.setLayoutParams(new Gallery.LayoutParams(230, 100));
    textview.setBackgroundResource(mGalleryItemBackground);
         changePosition(position, textview);

    return textview;

}

Thats it!! :)

neha
  • 9
  • 2