謝謝!!嘗試了很多之後。我來到了一個解決方案:我已經使用了圖庫小部件並對其進行了自定義以滿足我的目的
//定義庫類: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;
}
就是這樣! :)
那你試試這麼遠嗎? –
我已經嘗試了繼承HorizontalScrollView來改變方法。但不知道如何控制滑動速度和最大滑動距離。 – neha
看看這個http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html –