2013-07-24 11 views
0

我想要實現一個HorizontalScrollView與四個TextView有文字說(「第一視圖」,「第二視圖」,「第三視圖」,「第四視圖」),使HorizontalScrollView將只顯示一箇中心TextView一次和何時用戶滾動/滑動,他/她將只能移動到下一個文本(不考慮滾動/滑動的速度),以便我可以顯示與當時在HorizontalScrollView中可見文本對應的不同圖像。如何控制Horizo​​ntalScrollView中的甩尾效果?

這意味着如果用戶在「第二視圖」並且想要看到「第四視圖」,他也必須看到「第三視圖」。

我是新來的Android。請幫助!

+0

那你試試這麼遠嗎? –

+0

我已經嘗試了繼承Horizo​​ntalScrollView來改變方法。但不知道如何控制滑動速度和最大滑動距離。 – neha

+0

看看這個http://android-developers.blogspot.com/2011/08/horizo​​ntal-view-swiping-with-viewpager.html –

回答

0

謝謝!!嘗試了很多之後。我來到了一個解決方案:我已經使用了圖庫小部件並對其進行了自定義以滿足我的目的

//定義庫類: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; 
} 

}

現在在主活動類添加以下的OnCreate中的功能代碼:

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

然後創建ImageAdapter類作爲:

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; 

} 

就是這樣! :)

2

在這個例子中,我重寫一扔()方法,將速度由4引起一扔要弱一些:

​​