2014-02-12 25 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