Android 项目首页的Fragment切换实例(一)

来源:互联网 发布:mac传照片给iphone 编辑:程序博客网 时间:2024/05/16 23:32

一、首页的Activity的布局:

<?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">    <RadioGroup        android:id="@+id/rg_bottom"        android:layout_width="match_parent"        android:layout_height="50dp"        android:layout_alignParentBottom="true"        android:orientation="horizontal">        <RadioButton            android:id="@+id/rb_one"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:checked="true"            android:gravity="center_horizontal|center_vertical"            android:text="第一"            android:textColor="@drawable/slt_rb_text_color" />        <RadioButton            android:id="@+id/rb_two"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:gravity="center_horizontal|center_vertical"            android:text="第二"            android:textColor="@drawable/slt_rb_text_color" />        <RadioButton            android:id="@+id/rb_three"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:gravity="center_horizontal|center_vertical"            android:text="第三"            android:textColor="@drawable/slt_rb_text_color" />        <RadioButton            android:id="@+id/rb_four"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:gravity="center_horizontal|center_vertical"            android:text="第四"            android:textColor="@drawable/slt_rb_text_color" />        <RadioButton            android:id="@+id/rb_five"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:gravity="center_horizontal|center_vertical"            android:text="第五"            android:textColor="@drawable/slt_rb_text_color" />    </RadioGroup>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:layout_above="@+id/rg_bottom"        android:background="#AFAFAF" />        <android.support.v4.view.ViewPager        android:id="@+id/pager"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@+id/rg_bottom"        android:layout_marginTop="1dp" />    </RelativeLayout>

代码中使用到的资源文件slt_rb_text_color的代码内容是:

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


二、再Activity中进行控件的初始化和页面的集合等等的一些操作:

package com.switchfragment;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.view.ViewPager;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.RadioButton;import com.switchfragment.adapter.MyFragmentPagerAdapter;import com.switchfragment.fragment.CustomListFragment;import com.switchfragment.fragment.Fragment1;import com.switchfragment.fragment.Fragment2;import com.switchfragment.fragment.Fragment3;import com.switchfragment.fragment.Fragment4;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {    private ViewPager pager;    private List<Fragment> fragList;    private RadioButton rbOne;    private RadioButton rbTwo;    private RadioButton rbThree;    private RadioButton rbFour;    private RadioButton rbFive;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }    //初始化控件、数据    private void init(){        rbOne = (RadioButton) findViewById(R.id.rb_one);        rbOne.setOnClickListener(this);        rbTwo = (RadioButton) findViewById(R.id.rb_two);        rbTwo.setOnClickListener(this);        rbThree = (RadioButton) findViewById(R.id.rb_three);        rbThree.setOnClickListener(this);        rbFour = (RadioButton) findViewById(R.id.rb_four);        rbFour.setOnClickListener(this);        rbFive = (RadioButton) findViewById(R.id.rb_five);        rbFive.setOnClickListener(this);                pager = (ViewPager) findViewById(R.id.pager);        fragList = new ArrayList<>();        fragList.add(new Fragment1());        fragList.add(new Fragment2());        fragList.add(new Fragment3());        fragList.add(new Fragment4());        fragList.add(new CustomListFragment());        /**         * 使用getSupportFragmentManager()该Activity必须继承FragmentActivity         */        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragList);        pager.setAdapter(adapter);        pager.setOnPageChangeListener(this);        pager.setOffscreenPageLimit(1);    }    @Override    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {    }    @Override    public void onPageSelected(int position) {        switch (position) {            case 0:                rbOne.setChecked(true);                break;            case 1:                rbTwo.setChecked(true);                break;            case 2:                rbThree.setChecked(true);                break;            case 3:                rbFour.setChecked(true);                break;            case 4:                rbFive.setChecked(true);                break;        }    }    @Override    public void onPageScrollStateChanged(int state) {    }        @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.rb_one:                pager.setCurrentItem(0);                break;            case R.id.rb_two:                pager.setCurrentItem(1);                break;            case R.id.rb_three:                pager.setCurrentItem(2);                break;            case R.id.rb_four:                pager.setCurrentItem(3);                break;            case R.id.rb_five:                pager.setCurrentItem(4);                break;        }    }}

三、其中的适配器的MyFragmentPagerAdapter的代码是:

package com.switchfragment.adapter;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentStatePagerAdapter;import java.util.List;public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {    private List<Fragment> fragList;    public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> fragList) {        super(fm);        this.fragList = fragList;    }    @Override    public Fragment getItem(int position) {        return fragList.get(position);    }    @Override    public int getCount() {        return fragList.size();    }}

四、五个Fragment页面以及基类BaseFragment如下:

BaseFragment:

package com.switchfragment.fragment;import android.support.v4.app.Fragment;/** * Created by think on 2017/3/28. */public class BaseFragment extends Fragment {    /**     * 当前界面是否呈现给用户的状态标志     */    protected boolean isVisible;    /**     * 重写Fragment父类生命周期方法,在onCreate之前调用该方法,实现Fragment数据的缓加载.     * @param isVisibleToUser 当前是否已将界面显示给用户的状态     */    @Override    public void setUserVisibleHint(boolean isVisibleToUser) {        super.setUserVisibleHint(isVisibleToUser);        if(getUserVisibleHint()) {            isVisible = true;            onVisible();        } else {            isVisible = false;            onInvisible();        }    }    /**     * 当界面呈现给用户,即设置可见时执行,进行加载数据的方法     * 在用户可见时加载数据,而不在用户不可见的时候加载数据,是为了防止控件对象出现空指针异常     */    protected void onVisible(){        setlazyLoad();    }    /**     * 当界面还没呈现给用户,即设置不可见时执行     */    protected void onInvisible(){    }    /**     * 加载数据方法     */    protected void setlazyLoad(){    }}

Fragment1,Fragment2,Fragment3,Fragment4,Fragment5的页面代码相同:

public class Fragment1 extends BaseFragment {    private Context context;    private TextView tv1;    private boolean isPrepared;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.view1, container, false);        context = getActivity();        initView(view);        isPrepared = true;        setlazyLoad();        return view;    }    private void initView(View view) {        tv1 = (TextView) view.findViewById(R.id.tv_1);    }    @Override    protected void setlazyLoad() {        super.setlazyLoad();        if (!isPrepared || !isVisible) {            return;        }        /**         * 在这里进行的接口的访问和控件的适配。         */        tv1.setText("第一页");    }}

其中的五个Fragment的布局文件都省略了。