是否可以从不属于该片段的片段中获取视图?
Is it possibile to get View from Fragment which is not a part of this Fragment?
我有以下问题。我想设置一个 TextView
以防搜索列表为空。我在 onCreateView
:
中这样做
View view = inflater.inflate(R.layout.fragment_list, container, false);
View empty = view.findViewById(R.id.fragment_list_empty);
this.mList.setEmptyView(empty);
我有很多 Fragments
,我必须在其中添加这个 EmptyView
,但是有什么方法可以避免在 .xml 每个片段:
<TextView
android:id="@+id/fragment_list_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/empty_list"
android:textColor="@color/main_white"/>
您可以在片段容器上方中央的 MainActivity 中添加视图。
//MainActivity
private TextView tvEmpty;
public void onCreate(...){
this.tvEmpty = (TextView) findViewById(R.id.NameOfEmptyTV);
}
public View getEmptyTv(){
return this.tvEmpty;
}
//FRAGMENT
public View onCreateView(...){
this.mList.setEmptyView(((MainActivity)getActivity()).getEmptyTv());
}
或者您可以创建自己的已实现此类方法的扩展片段
我有以下问题。我想设置一个 TextView
以防搜索列表为空。我在 onCreateView
:
View view = inflater.inflate(R.layout.fragment_list, container, false);
View empty = view.findViewById(R.id.fragment_list_empty);
this.mList.setEmptyView(empty);
我有很多 Fragments
,我必须在其中添加这个 EmptyView
,但是有什么方法可以避免在 .xml 每个片段:
<TextView
android:id="@+id/fragment_list_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/empty_list"
android:textColor="@color/main_white"/>
您可以在片段容器上方中央的 MainActivity 中添加视图。
//MainActivity
private TextView tvEmpty;
public void onCreate(...){
this.tvEmpty = (TextView) findViewById(R.id.NameOfEmptyTV);
}
public View getEmptyTv(){
return this.tvEmpty;
}
//FRAGMENT
public View onCreateView(...){
this.mList.setEmptyView(((MainActivity)getActivity()).getEmptyTv());
}
或者您可以创建自己的已实现此类方法的扩展片段