2014-02-12 27 views
0

我有一個自定義視圖,我想在片段內使用。與該View交互的邏輯應該駐留在Fragment(或該片段擁有的對象)中。在Fragment的onCreateView方法中,我膨脹了片段佈局。緊接着,我試圖獲得對佈局中使用的BadgeButton的引用。如何從片段內檢索對自定義視圖的引用

我看到的問題是我無法獲得對自定義視圖的引用。將兩行註釋掉如下所示,活動將正確加載並適當地顯示BadgeButton。

當我從該示例中刪除片段並將其佈局合併到MainActivity的佈局中時,它能夠很好地獲得引用。這種行爲的原因是什麼?

MainActivity

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (savedInstanceState == null) { 
     getFragmentManager().beginTransaction() 
     .add(R.id.container, new MainFragment()) 
     .commit(); 
    } 
    } 
} 

MainFragment

public class MainFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
    // BadgeButton badger = (BadgeButton) this.getActivity().findViewById(R.id.badger); 
    // badger.setBadgeText("example"); // NullPointerException 
    return rootView; 
    } 
} 

BadgeButton

public class BadgeButton extends FrameLayout { 
    private TextView mBadgeTextView; 

    public BadgeButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
    } 

    public void setBadgeText(String value) { 
    mBadgeTextView.setText(value); 
    } 

    private void init(Context context) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View inflate = inflater.inflate(R.layout.badge_button, this, true); 
    mBadgeTextView = (TextView) getChildAt(1); 
    mBadgeTextView.setText("0"); 
    } 
} 

badge_button.xml

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top|left" 
    android:textSize="18sp" 
    android:text="123"/> 
</merge> 

fragment_main.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.badgebutton.MainFragment"> 

    <com.example.badgebutton.BadgeButton 
    android:id="@+id/badger" 
    android:layout_width="120dp" 
    android:layout_height="120dp"/> 

</LinearLayout> 

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.badgebutton.MainActivity" 
    tools:ignore="MergeRootFrame" /> 

如果你有關於這個問題或建議,對品質的任何輸入如何改善這個和未來的問題,請讓我知道(以任何方式是合適的)。我想問一些好問題,併爲社區做出有價值的貢獻。謝謝。

回答

1
BadgeButton badger = (BadgeButton) rootView.findViewById(R.id.badger); 
badger.setBadgeText("example"); 

使用此自視圖與Fragment不是Activity有關。

+0

在我嘗試過的幾個變化中,這不過是其中的一種。謝謝。 – jneander

+0

我正準備。我被告知需要等待1分鐘。 (嘆) – jneander