我想使用片段更改片段(@ + id / content_frame) Radiobutton上的管理器(用作选项卡)但片段加载时ononCheckedChanged 我的单选按钮不亮了。
这是我的main.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioGroup
android:id="@+id/buttongroup1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<at.bookworm.widget.TabControlButton
android:id="@+id/opt_ct"
style="@style/Widget.Holo.SegmentedControl"
android:checked="true"
android:text="City Taxi"
android:textColor="@color/black"
android:textStyle="normal" />
<at.bookworm.widget.TabControlButton
android:id="@+id/opt_os"
style="@style/Widget.Holo.SegmentedControl"
android:text="Outstation"
android:textColor="@color/black"
android:textStyle="normal" />
<at.bookworm.widget.TabControlButton
android:id="@+id/opt_lo"
style="@style/Widget.Holo.SegmentedControl"
android:text="Local"
android:textColor="@color/black"
android:textStyle="normal" />
</RadioGroup>
<FrameLayout android:id="@+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
style="?android:attr/dividerHorizontal"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/silver_two"
android:choiceMode="singleChoice" >
<!--
<LinearLayout
android:id="@+id/left_drawer"
android:orientation="vertical"
android:layout_width="240dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/my_account"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Test"/>
</LinearLayout>
-->
</ListView>
</android.support.v4.widget.DrawerLayout>
我在MainActivity中使用片段管理器替换片段 这是代码
公共类MainActivity扩展了Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.buttongroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.content_frame, mainFragment).commit();
}
});
}
无线电小组骚扰后继续
public class MainFragment extends Fragment{
static View rootView;
------
----
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.route_fragment, container, false);
} catch (InflateException e) {
}
return rootView;
}
答案 0 :(得分:0)
请检查您的片段中是否正在执行此类操作:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
而不是setContentView
这是一个很好的资源:
http://developer.android.com/training/basics/fragments/creating.html