Android 片段中的垂直制表符 - 错误找不到方法
Android vertical tabs in fragment - Error could not find method
我正在尝试在 android 项目中创建一组垂直选项卡,我一直在查看有关 SO 的许多教程和帖子,但似乎无法将它们拼凑在一起。
我用一个片段创建了一个新项目,所以我有一个 main.xml 和一个片段_main.xml,一个主 activity class 和一个主片段 class.
main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.android.MainScreenActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
片段_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.2">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/tab_btn_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/patient_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/relations_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/providers_tab_btn"
android:onClick="tabHandler"/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.8"/>
</LinearLayout>
主要 activity 除了默认设置外,我没有添加任何内容,因为我认为我应该使用片段,因为 google sunshine 应用程序在主屏幕上使用片段。
主要片段class
private FragmentTabHost mTabHost;
Button patientButton, relationsButton, providersButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tab_btn_container);
patientButton = (Button)rootView.findViewById(R.id.patient_tab_btn);
relationsButton = (Button)rootView.findViewById(R.id.relations_tab_btn);
providersButton = (Button)rootView.findViewById(R.id.providers_tab_btn);
return rootView;
}
public void tabHandler(View target){
Log.d(LOG_TAG, "Calling tabHandler() method");
patientButton.setSelected(false);
relationsButton.setSelected(false);
providersButton.setSelected(false);
if(target.getId() == R.id.patient_tab_btn){
//mTabHost.setCurrentTab(0);
patientButton.setSelected(true);
} else if(target.getId() == R.id.relations_tab_btn){
//mTabHost.setCurrentTab(1);
relationsButton.setSelected(true);
} else if(target.getId() == R.id.providers_tab_btn){
//mTabHost.setCurrentTab(2);
providersButton.setSelected(true);
}
}
所以我添加了 tabHandler 按钮并且 xml 在 fragment main 中膨胀 class 但我在按下任何选项卡按钮时遇到错误:
java.lang.IllegalStateException: Could not find method tabHandler(View) in a parent or ancestor Context for android:onClick attribute
我也不确定如何更新 tabcontent 窗格,因为所有示例似乎都使用 Activity 并且 activity class 从 Tab[=42= 扩展] 但是使用片段我的 class 已经扩展了片段。
如何使它与包含选项卡的片段一起使用?
您在 android:onClick
(tabHandler
) 中指定的方法应位于 Activity 中,而不是片段中。这个 SO 答案有更多信息:
我正在尝试在 android 项目中创建一组垂直选项卡,我一直在查看有关 SO 的许多教程和帖子,但似乎无法将它们拼凑在一起。
我用一个片段创建了一个新项目,所以我有一个 main.xml 和一个片段_main.xml,一个主 activity class 和一个主片段 class.
main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.android.MainScreenActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
片段_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.2">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/tab_btn_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/patient_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/relations_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/providers_tab_btn"
android:onClick="tabHandler"/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.8"/>
</LinearLayout>
主要 activity 除了默认设置外,我没有添加任何内容,因为我认为我应该使用片段,因为 google sunshine 应用程序在主屏幕上使用片段。
主要片段class
private FragmentTabHost mTabHost;
Button patientButton, relationsButton, providersButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tab_btn_container);
patientButton = (Button)rootView.findViewById(R.id.patient_tab_btn);
relationsButton = (Button)rootView.findViewById(R.id.relations_tab_btn);
providersButton = (Button)rootView.findViewById(R.id.providers_tab_btn);
return rootView;
}
public void tabHandler(View target){
Log.d(LOG_TAG, "Calling tabHandler() method");
patientButton.setSelected(false);
relationsButton.setSelected(false);
providersButton.setSelected(false);
if(target.getId() == R.id.patient_tab_btn){
//mTabHost.setCurrentTab(0);
patientButton.setSelected(true);
} else if(target.getId() == R.id.relations_tab_btn){
//mTabHost.setCurrentTab(1);
relationsButton.setSelected(true);
} else if(target.getId() == R.id.providers_tab_btn){
//mTabHost.setCurrentTab(2);
providersButton.setSelected(true);
}
}
所以我添加了 tabHandler 按钮并且 xml 在 fragment main 中膨胀 class 但我在按下任何选项卡按钮时遇到错误:
java.lang.IllegalStateException: Could not find method tabHandler(View) in a parent or ancestor Context for android:onClick attribute
我也不确定如何更新 tabcontent 窗格,因为所有示例似乎都使用 Activity 并且 activity class 从 Tab[=42= 扩展] 但是使用片段我的 class 已经扩展了片段。
如何使它与包含选项卡的片段一起使用?
您在 android:onClick
(tabHandler
) 中指定的方法应位于 Activity 中,而不是片段中。这个 SO 答案有更多信息: