android-UI-切换fragment导航条---网易云探索(1)

来源:互联网 发布:淘宝内部优惠券微信号 编辑:程序博客网 时间:2024/04/28 10:21

今天查看了网易云音乐播放器的界面,想着如何实现的。现在尝试模仿一下。
这里写图片描述

参考文章:Fragment实例精讲——底部导航栏的实现

导航条实现

HorizontalScrollView+RadioButton
HorizontalScrollView是为了多个选项的设置,防止超出屏幕可以滚动。

activity.xml
<HorizontalScrollView        android:id="@+id/huadong"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:scrollbars="none">        <RadioGroup            android:id="@+id/rg_tab_bar"            android:layout_width="match_parent"            android:layout_height="56dp"            android:layout_alignParentBottom="true"            android:background="#cccccc"            android:orientation="horizontal">            <RadioButton                android:id="@+id/rb_channel"                style="@style/tab_menu_item"                android:background="#ff0000"                android:text="tab1" />            <RadioButton                android:id="@+id/rb_message"                style="@style/tab_menu_item"                android:background="#0000dd"                android:text="tab2" />            <RadioButton                android:id="@+id/rb_better"                style="@style/tab_menu_item"                android:background="#00ff00"                android:text="tab3" />            <RadioButton                android:id="@+id/rb_setting"                style="@style/tab_menu_item"                android:background="#ccff00"                android:text="tab4" />        </RadioGroup>    </HorizontalScrollView>

Fragment设置

发现原来参考文章有错误。

public MyFragment(String content) {        this.content = content;    }

是错误的:不能使用重载传参
使用fragment注意事项:
1. fragment一定要有一个无参构造函数
2. 如果要给fragment传参,一定要使用Bundle方式传参,而不重载构造函数传参,因为在fragment重新生成的使用不会执行这个带参构造函数,而是执行无参构造函数。

所以应该这样传参:
Activity.class

fg4 = new MyFragment();                    Bundle args = new Bundle();                    args.putString("userId", "第四个Fragment");                    fg4.setArguments(args);                    fTransaction.add(R.id.ly_content,fg4);

Fragment.class

@Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fg_content, container, false);        TextView txt_content = (TextView) view.findViewById(R.id.txt_content);        Bundle args = getArguments();        if (args != null) {            userId = args.getString("userId");        }        txt_content.setText(userId);        return view;    }

继续添加Fragment

<FrameLayout        android:id="@+id/ly_content"        android:layout_width="match_parent"        android:layout_height="match_parent" ></FrameLayout>

这里写图片描述

这里写图片描述

这里写图片描述

0 0