android中自己写一个Fragment的应用,类似QQ的一个框架

来源:互联网 发布:平面设计和美工哪个好 编辑:程序博客网 时间:2024/05/22 03:14

这里写图片描述

MainActivity类

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {    Fragment[] fragments = new Fragment[4];    RadioGroup rg;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        rg = (RadioGroup) findViewById(R.id.rg);        rg.setOnCheckedChangeListener(this);        showFragment(0);        rg.check(R.id.rb1);    }    @Override    public void onCheckedChanged(RadioGroup group, int checkedId) {//        group.getChildAt(checkedId).getTag();        switch (checkedId) {            case R.id.rb1:                showFragment(0);                break;            case R.id.rb2:                showFragment(1);                break;            case R.id.rb3:                showFragment(2);                break;            case R.id.rb4:                showFragment(3);                break;        }    }    //上一次界面上显示的哪一个Fragment    int currIndex = -1; //代表第一次进来    //展示Fragment    public void showFragment(int index) {        //当前已经显示了这个界面        if (index == currIndex)            return;        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();        //如果用户已经打开过其中一个Fragment,在切换的时候,我需要将上一次的移除        if (currIndex != -1) {            //直接移除            ft.hide(fragments[currIndex]);        }        //显示新的Fragment        if (fragments[index] == null) {            //创建操作            createFragment(index);            //添加进去            ft.add(R.id.fl_content, fragments[index]);        } else {            //不是空的            ft.show(fragments[index]);        }        currIndex = index;        ft.commit();    }    //新建一个Fragment    private void createFragment(int index) {        switch (index) {            case 0:                fragments[0] = new ManagerFragment();                break;            case 1:                fragments[1] = new ContactsFragment();                break;            case 2:                fragments[2] = new NewsFragment();                break;            case 3:                fragments[3] = new SettingFragment();                break;        }    }}

下面是四个Fragment

//1.ContactsFragmentpublic class ContactsFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        TextView tv = new TextView(getActivity());        tv.setText("这是联系人界面");        tv.setTextSize(50);        return tv;    }}//2.ManagerFragment public class ManagerFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        TextView tv = new TextView(getActivity());        tv.setText("这是消息面");        tv.setTextSize(50);        return tv;    }}//3.NewsFragment public class NewsFragment extends Fragment{    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        TextView tv = new TextView(getActivity());        tv.setText("这是动态界面");        tv.setTextSize(50);        return tv;    }}//4.SettingFragment public class SettingFragment extends Fragment {    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        TextView tv = new TextView(getActivity());        tv.setText("这是设置界面");        tv.setTextSize(50);        return tv;    }}

自己写的一个color中的TextView的颜色选择

textselector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="#FFF" android:state_checked="true" />    <item android:color="#393737" android:state_checked="false" /></selector>

drawable中自己定义的几个图形

<--!  contacts_selector.xml--><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/contacts_selected" android:state_checked="true" />    <item android:drawable="@mipmap/contacts_unselected" android:state_checked="false" /></selector><--!  message_selector.xml--><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/message_selected" android:state_checked="true" />    <item android:drawable="@mipmap/message_unselected" android:state_checked="false" /></selector><--! news_selector.xml--><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/news_selected" android:state_checked="true" />    <item android:drawable="@mipmap/news_unselected" android:state_checked="false" /></selector><--! setting.xml--><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@mipmap/setting_selected" android:state_checked="true" />    <item android:drawable="@mipmap/setting_unselected" android:state_checked="false" /></selector>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.administrator.lesson10_fragment3.MainActivity">    <RadioGroup        android:id="@+id/rg"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:background="@drawable/tab_bg"        android:orientation="horizontal"        android:padding="5dp">        <RadioButton            android:id="@+id/rb1"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawablePadding="5dp"            android:drawableTop="@drawable/message_selector"            android:gravity="center"            android:tag="0"            android:text="消息"            android:textColor="@color/textselector" />        <RadioButton            android:id="@+id/rb2"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawablePadding="5dp"            android:drawableTop="@drawable/contacts_selector"            android:gravity="center"            android:tag="1"            android:text="联系人"            android:textColor="@color/textselector" />        <RadioButton            android:id="@+id/rb3"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawablePadding="5dp"            android:drawableTop="@drawable/news_selector"            android:gravity="center"            android:tag="2"            android:text="动态"            android:textColor="@color/textselector" />        <RadioButton            android:id="@+id/rb4"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:drawablePadding="5dp"            android:drawableTop="@drawable/setting"            android:gravity="center"            android:tag="3"            android:text="设置"            android:textColor="@color/textselector" />    </RadioGroup>    <FrameLayout        android:id="@+id/fl_content"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@id/rg" /></RelativeLayout>
0 0
原创粉丝点击