2010-12-15 67 views
2

我正在嘗試創建一個自定義圖庫視圖。或者,也許我不明白我應該在這裏做什麼。我需要覆蓋onFling()方法Gallery類,但我不明白我該如何做到這一點,因爲我的主類必須從活動延伸。如何在android中製作自定義圖庫視圖

我已經嘗試做了一個名爲CustomGallery延伸畫廊類,但如果我嘗試和運行應用程序,我得到一個強制關閉。

如何覆蓋圖庫視圖的onFling()方法

謝謝!

編輯

我試圖但是,下面基督教的解決方案,這個類有遍佈它的錯誤。很明顯,我做錯了。建議?

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.Gallery; 

public class CustomGallery extends Gallery 
{ 
    public CustomGallery(Context context) 
    { 
     super(context); 
    } 

    @Override 
    public Gallery(Context context) 
    { 
     this(context, null); 
    } 

    @Override 
    public Gallery(Context context, AttributeSet attrs) 
    { 
     this(context, attrs, R.attr.galleryStyle); 
    } 

    @Override 
    public Gallery(Context context, AttributeSet attrs, int defStyle) 
    { 
     // 
    } 
} 

EDIT 2

確定該懂了工作,THX基督徒!

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.Gallery; 

public class CustomGallery extends Gallery 
{ 
    public CustomGallery(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 

    } 

} 

回答

5

一個人如何改寫爲畫廊查看onFling()方法?

  1. 創建一個類,擴展庫(例如CustomGallery,XD)
  2. 覆蓋的方法
  3. 使用這個類在你的佈局。

只是用它就像如果你使用的是Gallery

<LinearLayout> 
... 
<com.your.package.CustomGallery 
    android:layout_width="fill_parent" 
    the rest of the things here/> 
... 
</LinearLayout> 

確保重載構造方法:

public class CustomGallery extends Gallery{ 

    public CustomGallery(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // this could be empty, but must be here. 
     // since it's a view to use from XML, 
     // you must override this constructor 
     // (not only the one that receives a context) 
    } 
} 
+0

該死的基督教你在鍵盤上睡覺笑?感謝您的快速回復!我正在嘗試。只要我在這裏,在XML中,我會調用畫廊或CustomGallery(或任何我命名的類)? – Ribs 2010-12-15 23:58:12

+0

什麼是「Pepe」? - > public Pepe(Context context,AttributeSet attrs) – Ribs 2010-12-16 00:01:18

+0

編輯:只要我在這裏,在.java中,我會投到Gallery或CustomGallery(或任何我命名的類)? – Ribs 2010-12-16 00:05:05

相關問題