2012-06-20 56 views
2

此活動代碼像Iphone水平滾動一樣移動。Android:Horizo​​ntalScrollView像Iphone一樣移動

秒代碼是ScrollView庫。

更好的主意如果你有,評論請。

或批評歡迎。

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

而這個代碼爲Iphonish Horizo​​ntalScrollView庫。

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(); 
    } 
} 
+1

而你沒有問題嗎?你在尋找評論家嗎? –

+0

滾動目的是否正確?我找不到任何其他的實現。我認爲ViewGroup.addView,ViewGroup.removeView不適用於垃圾收集器或Arm Coretex。 –

+0

@AndroSelva是的,我打破規則在stackoverflow.com? –

回答

相關問題