2

This Activity Code move Like a Iphone Horizontal Scroll.

second Code is ScrollView Library.

The Better Idea If you have, Comment Please.

Or Critics Welcome.

public class HorizontalScrollActivity extends Activity {
    private StickyHorizontalScrollView scrollView;
    private ArrayList<View> viewList = new ArrayList<View>();
    private ViewGroup mLayout;
    private ViewGroup mLayout2;
    private ViewGroup mLayout3;
    private LinearLayout mLayoutContainer;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        scrollView = new StickyHorizontalScrollView(this);
        scrollView.setHorizontalScrollBarEnabled(false);
        scrollView.setFadingEdgeLength(0);

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        DisplayMetrics mDisplayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
        int deviceWidth = mDisplayMetrics.widthPixels;
        int deviceHeight = mDisplayMetrics.heightPixels;

        mLayout = (ViewGroup)inflater.inflate(R.layout.main, null);
        mLayout.setLayoutParams(new LayoutParams(deviceWidth, deviceHeight));
        mLayout2 = (ViewGroup)inflater.inflate(R.layout.main2, null);
        mLayout2.setLayoutParams(new LayoutParams(deviceWidth, deviceHeight));
        mLayout3 = (ViewGroup)inflater.inflate(R.layout.main3, null);
        mLayoutContainer = (LinearLayout)mLayout3.findViewById(R.id.bodyLayout);
        mLayout3.removeView(mLayoutContainer);

        viewList.add(mLayout);
        viewList.add(mLayout2);

        int i = 0;
        for(View v : viewList){
            mLayoutContainer.addView(v);
            scrollView.addStickyPosition(i++ * deviceWidth);
        }
        scrollView.addView(mLayoutContainer);
        setContentView(scrollView);
    }
}

And This Code for Iphonish HorizontalScrollView Library.

Library Page

public class StickyHorizontalScrollView extends HorizontalScrollView {
    protected ArrayList<Integer> stickyPositions = new ArrayList<Integer>();
    private boolean flinged = false;
    private final String TAG = "StickyHorizontalScrollView ==========";

    public StickyHorizontalScrollView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void fling(int velocityX) {
        // TODO Auto-generated method stub
        Log.d(TAG, "FLING");
        flinged = true;

        int lastSmaller = 0;

        for(int sticky : stickyPositions){
            if(velocityX > 0 && sticky > getScrollX()){
                smoothScrollTo(sticky, getScrollY());
                break;
            }else if(velocityX < 0){
                if(sticky > getScrollX()){
                    smoothScrollTo(lastSmaller, getScrollY());
                    break;
                }else{
                    lastSmaller = sticky;
                }
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        Log.d(TAG, "TOUCH");
        flinged = false;
        boolean res = super.onTouchEvent(ev);

        if(ev.getAction() == MotionEvent.ACTION_UP && !flinged){
            sanitizeScrollPosition();
        }
        return res;
    }

    public void sanitizeScrollPosition(){
        Log.d(TAG, "SANITIZE");
        Integer minDistance = 0;
        Integer minStickyPosition = 0;

        for(int sticky : stickyPositions){
            int distance = Math.abs(getScrollX() - sticky);
            if(minDistance == null || minDistance > distance){
                minStickyPosition = sticky;
                minDistance = distance;
            }
        }
        smoothScrollTo(minStickyPosition, getScrollY());
    }

    public void addStickyPosition(int x){
        Log.d(TAG, "ADD STICKY POSITION");
        stickyPositions.add(x);
        Collections.sort(stickyPositions);
    }

    public void clearStickyPositions(){
        Log.d(TAG, "CLEAR STICKY POSITION");
        stickyPositions.clear();
    }
}
reinhard.lee
  • 503
  • 4
  • 10
  • 24

1 Answers1

0

I know you asked this question a year ago but for those who are still looking for that I found this amazing example and it made my day :

https://github.com/ysamlan/horizontalpager

hope you find it useful as well :)

Rudi
  • 4,304
  • 4
  • 34
  • 44