美文网首页
Android fragment的切换+底部导航栏

Android fragment的切换+底部导航栏

作者: Zebraaa | 来源:发表于2018-07-19 10:41 被阅读0次

    新建一个加载他们的activity:
    activity_mainxml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:layout_alignParentBottom="true"
            android:background="#f0f0f0"
            android:orientation="horizontal">
            <RadioButton
                android:id="@+id/rb_news"
                android:checked="true"
                android:text="新闻"
                android:drawableTop="@drawable/rb_news_selector"
                style="@style/style_RadioButton"/>
            <RadioButton
                android:id="@+id/rb_thing"
                android:text="装备库"
                android:drawableTop="@drawable/rb_thing_selector"
                style="@style/style_RadioThingButton"/>
            <RadioButton
                android:id="@+id/rb_me"
                android:text="我"
                android:drawableTop="@drawable/rb_me_selector"
                style="@style/style_RadioButton"/>
        </RadioGroup>
    
        <com.example.chen.briupnes.util.CustomViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/radioGroup" />
    </RelativeLayout>
    

    自定义ViewPager禁止左右滑动

    public class CustomViewPager extends ViewPager {
    
        private boolean isCanScroll = true;
    
        public CustomViewPager(Context context) {
            super(context);
        }
    
        public CustomViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        /**
         * 设置其是否能滑动换页
         * @param isCanScroll false 不能换页, true 可以滑动换页
         */
        public void setScanScroll(boolean isCanScroll) {
            this.isCanScroll = isCanScroll;
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            return isCanScroll && super.onInterceptTouchEvent(ev);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return isCanScroll && super.onTouchEvent(ev);
    
        }
    }
    

    MainActivity:

     private void initView() {
           
            rb_news = (RadioButton) findViewById(R.id.rb_news);
            rb_thing = (RadioButton) findViewById(R.id.rb_thing);
            rb_me = (RadioButton) findViewById(R.id.rb_me);
            radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
            viewPager = (CustomViewPager) findViewById(R.id.viewPager);
            viewPager.setScanScroll(false);
                radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch (checkedId){
                            case R.id.rb_news:
                                /**
                                 * setCurrentItem第二个参数控制页面切换动画
                                 * true:打开/false:关闭
                                 */
                                viewPager.setCurrentItem(0, false);
                                break;
                            case R.id.rb_thing:
                                /**
                                 * setCurrentItem第二个参数控制页面切换动画
                                 * true:打开/false:关闭
                                 */
                                viewPager.setCurrentItem(1, false);
                                break;
                            case R.id.rb_me:
                                /**
                                 * setCurrentItem第二个参数控制页面切换动画
                                 * true:打开/false:关闭
                                 */      
                                    viewPager.setCurrentItem(2, false);                    
                                break;
                        }
                    }
                });
                MeFragment meFragment = new MeFragment();
                NewsFragment newsFragment = new NewsFragment();
                ThingFragment thingFragment = new ThingFragment();
                List<Fragment> alFragment = new ArrayList<Fragment>();
                alFragment.add(newsFragment);
                alFragment.add(thingFragment);
                alFragment.add(meFragment);
                viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),alFragment));
                //ViewPager显示第一个Fragment
                viewPager.setCurrentItem(0);
           /* //页面的切换
            viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                }
    
                @Override
                public void onPageSelected(int position) {
                    switch (position) {
                        case 0:
                            radioGroup.check(R.id.rb_news);
                            break;
                        case 1:
                            radioGroup.check(R.id.rb_thing);
                            break;
                        case 2:
                            radioGroup.check(R.id.rb_me);
                            break;
    
                    }
    
                }
    
                @Override
                public void onPageScrollStateChanged(int state) {
    
                }
            });*/
    
    
    
    
            }
    

    fragmentadapter

    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> list;
    
        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }
    
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }
    
        @Override
        public int getCount() {
            return list.size();
        }
    }
    

    viewpager的预加载:
    我的是当点击到fragment时进行网络请求:
    BaseFragment:

    public abstract class BaseFragment extends Fragment {
    
        private static final String TAG = BaseFragment.class.getSimpleName();
    
        private boolean isFragmentVisible;
        private boolean isReuseView;
        private boolean isFirstVisible;
        private View rootView;
    
    
        //setUserVisibleHint()在Fragment创建时会先被调用一次,传入isVisibleToUser = false
        //如果当前Fragment可见,那么setUserVisibleHint()会再次被调用一次,传入isVisibleToUser = true
        //如果Fragment从可见->不可见,那么setUserVisibleHint()也会被调用,传入isVisibleToUser = false
        //总结:setUserVisibleHint()除了Fragment的可见状态发生变化时会被回调外,在new Fragment()时也会被回调
        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            //setUserVisibleHint()有可能在fragment的生命周期外被调用
            if (rootView == null) {
                return;
            }
            if (isFirstVisible && isVisibleToUser) {
                onFragmentFirstVisible();
                isFirstVisible = false;
            }
            if (isVisibleToUser) {
                onFragmentVisibleChange(true);
                isFragmentVisible = true;
                return;
            }
            if (isFragmentVisible) {
                isFragmentVisible = false;
                onFragmentVisibleChange(false);
            }
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            initVariable();
        }
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            //如果setUserVisibleHint()在rootView创建前调用时,那么
            //就等到rootView创建完后才回调onFragmentVisibleChange(true)
            //保证onFragmentVisibleChange()的回调发生在rootView创建完成之后,以便支持ui操作
            if (rootView == null) {
                rootView = view;
                if (getUserVisibleHint()) {
                    if (isFirstVisible) {
                        onFragmentFirstVisible();
                        isFirstVisible = false;
                    }
                    onFragmentVisibleChange(true);
                    isFragmentVisible = true;
                }
            }
            super.onViewCreated(isReuseView && rootView != null ? rootView : view, savedInstanceState);
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            initVariable();
        }
    
        private void initVariable() {
            isFirstVisible = true;
            isFragmentVisible = false;
            rootView = null;
            isReuseView = true;
        }
    
        /**
         *
         * @param isReuse
         */
        protected void reuseView(boolean isReuse) {
            isReuseView = isReuse;
        }
    
        /**
         * 去除setUserVisibleHint()多余的回调场景,保证只有当fragment可见状态发生变化时才回调
         * 回调时机在view创建完后,所以支持ui操作,解决在setUserVisibleHint()里进行ui操作有可能报null异常的问题
         *
         * 可在该回调方法里进行一些ui显示与隐藏
         *
         * @param isVisible true  不可见 -> 可见
         *                  false 可见  -> 不可见
         */
        protected void onFragmentVisibleChange(boolean isVisible) {
    
        }
    
        /**
         * 在fragment首次可见时回调,可用于加载数据,防止每次进入都重复加载数据
         */
        protected void onFragmentFirstVisible() {
    
        }
    
        protected boolean isFragmentVisible() {
            return isFragmentVisible;
        }
    }
    
    

    在Fragment中的调用例子:

    public class NewsFragment extends BaseFragment {
    
        private View rootView;
        private ListView lv_news;
        private NewsAdapter adapter;
        private  List<News> list = new ArrayList<News>();
        private final String TAG="NewsFragment";
    
        public NewsFragment() {
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            if(rootView == null){
                rootView = inflater.inflate(R.layout.fragment_news,container,false);
                initView(rootView);
            }
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
            return rootView;
        }
    
        private void initView(View view) {
            lv_news = (ListView) view.findViewById(R.id.lv_news);
        }
    
        private void setNewsData() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    OkHttpUtils.get()
                            .url("http://10.0.3.2:9999/ssh/data")
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, final Exception e, int id) {
                                    getActivity().runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(getContext(),"数据加载失败,请检查网络连接",Toast.LENGTH_SHORT).show();
                                        }
                                    });
    
                                }
    
                                @Override
                                public void onResponse(String response, int id) {
                                    if(response.toString().contains("error")){
                                        getActivity().runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(getContext(),"数据获取失败",Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                    }else{
                                        Gson gson = new Gson();
                                        Type type = new TypeToken<ArrayList<News>>(){}.getType();
                                        list = gson.fromJson(response.toString(), type);
                                        Log.e(TAG,list.toString());
                                        adapter = new NewsAdapter(list,getContext());
                                        lv_news.setAdapter(adapter);
                                    }
                                }
                            });
    
                }
            }).start();
        }
    
        @Override
        protected void onFragmentVisibleChange(boolean isVisible) {
            super.onFragmentVisibleChange(isVisible);
            if(isVisible){
               //模拟加载其他的数据
               setNewsData();
            }else{
    
            }
        }
    
        @Override
        protected void onFragmentFirstVisible() {
            super.onFragmentFirstVisible();
            setNewsData();
        }
    }
    

    下方图标设置:
    values 中 新建一个styles.xml

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="android:colorPrimary">@color/white</item>
        </style>
        <style name="style_RadioButton">
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:button">@null</item>
            <item name="android:background">@null</item>
            <item name="android:layout_weight">1</item>
            <item name="android:gravity">center</item>
            <item name="android:layout_gravity">center</item>
            <item name="android:textColor">@drawable/rb_focus_color</item>
            <item name="android:textSize">12sp</item>
        </style>
        <style name="style_RadioThingButton">
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:button">@null</item>
            <item name="android:background">@null</item>
            <item name="android:layout_weight">1</item>
            <item name="android:gravity">center</item>
            <item name="android:layout_gravity">center</item>
            <item name="android:textColor">@drawable/rb_focus_thing_color</item>
            <item name="android:textSize">12sp</item>
        </style>
    
    </resources>
    
    

    调用时见activity_main.xml中

    相关文章

      网友评论

          本文标题:Android fragment的切换+底部导航栏

          本文链接:https://www.haomeiwen.com/subject/tvizpftx.html