使用FragmentTabHost,实现页签与Fragment联动

来源:互联网 发布:mac版华彩人生1点通 编辑:程序博客网 时间:2024/06/05 05:03

布局代码:

<FrameLayout        android:id="@+id/fl_show_layout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_weight="1"/><android.support.v4.app.FragmentTabHost        android:id="@+id/tab_main_layout"        android:layout_width="match_parent"        android:layout_height="50dp"/>

页签显示

private void init() {//绑定显示区域        tabMainLayout.setup(this, getSupportFragmentManager(), R.id.fl_show_layout);//    for (int i = 0; i < 5; i++) {        TabHost.TabSpec tabSpe = tabMainLayout.newTabSpec("text" + i);        //页签布局        View view = getLayoutInflater().inflate(R.layout.items, null);        tabSpe.setIndicator(view);        // 传递数据到Fragment中,发送数据        Bundle bundle = new Bundle();        bundle.putString("text", "text" + i);        tabMainLayout.addTab(tabSpe, fragment1.class, bundle);        }    }

Fragment

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        //接收数据        Bundle bundle = getArguments();        String text = bundle.getString("text");        TextView tv = new TextView(getContext());        tv.setText(text);        return tv;    }
0 0