• 安卓滑动布局


    0、目录结构

    1、页面布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/activity_second"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <android.support.v4.view.ViewPager
            android:id="@+id/vp"
            android:layout_above="@+id/radioGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        </android.support.v4.view.ViewPager>
    
        <RadioGroup
            android:id="@+id/radioGroup"
            android:orientation="horizontal"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="52dp">
            <RadioButton
                android:id="@+id/btn1"
                android:button="@null"
                android:text="消息"
                android:gravity="center"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />
            <RadioButton
                android:id="@+id/btn2"
                android:button="@null"
                android:text="联系人"
                android:gravity="center"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />
            <RadioButton
                android:id="@+id/btn3"
                android:button="@null"
                android:text="动态"
                android:gravity="center"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />
        </RadioGroup>
    
    </RelativeLayout>

    2、activity代码

    public class SecondActivity extends Activity implements RadioGroup.OnCheckedChangeListener{
    
        private ViewPager viewPager;
        private ArrayList<View> pageViews;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            initView();
            initFragment();
        }
    
        private void initFragment() {
            MessageFragment messageFragment=new MessageFragment();
            ContactsFragment contactsFragment=new ContactsFragment();
            TrendsFragment trendsFragment=new TrendsFragment();
            ArrayList<Fragment> fragments=new ArrayList<>();
            fragments.add(messageFragment);
            fragments.add(contactsFragment);
            fragments.add(trendsFragment);
            viewPager.setAdapter(new mPagerAdapter());
            //默认为消息页面
            viewPager.setCurrentItem(0);
        }
    
        private void initView() {
            viewPager = (ViewPager) findViewById(R.id.vp);
            RadioGroup radioGroup= (RadioGroup) findViewById(R.id.radioGroup);
            radioGroup.setOnCheckedChangeListener(this);
            //LayoutInflater获取View 与activity关联
            LayoutInflater inflater=getLayoutInflater();
            View message=inflater.inflate(R.layout.layout_message,null);
            View contacts=inflater.inflate(R.layout.layout_contacts,null);
            View trends=inflater.inflate(R.layout.layout_trends,null);
    
            pageViews = new ArrayList<>();
            pageViews.add(message);
            pageViews.add(contacts);
            pageViews.add(trends);
        }
    
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId){
                case R.id.btn1:
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.btn2:
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.btn3:
                    viewPager.setCurrentItem(2);
                    break;
            }
        }
    
        private class mPagerAdapter extends PagerAdapter {
            @Override
            public int getCount() {
                return pageViews.size();
            }
    
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }
            @Override
            //使从ViewGroup中移出当前View
            public void destroyItem(View arg0, int arg1, Object arg2) {
                ((ViewPager) arg0).removeView(pageViews.get(arg1));
            }
    
            @Override
            //返回一个对象,这个对象表明了PagerAdapter适配器选择哪个对象放在当前的ViewPager中
            public Object instantiateItem(View arg0, int arg1) {
                ((ViewPager) arg0).addView(pageViews.get(arg1));
                return pageViews.get(arg1);
        }
    }}

    3、BaseFragment代码

    public abstract class BaseFragment extends Fragment {
        public Activity mActivity;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mActivity=getActivity();
        }
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=initView();
            return view;
    
        }
    
        /**
            初始化布局 子类必须实现
         */
        public abstract View initView();
    
        @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            initData();
        }
    
        public void initData() {
        }
    }

    4、fragment代码

    public class ContactsFragment extends BaseFragment {
        @Override
        public View initView() {
            View view=View.inflate(mActivity, R.layout.layout_contacts,null);
            return view;
        }
    }
    
    public class MessageFragment extends BaseFragment {
        @Override
        public View initView() {
            View view=View.inflate(mActivity, R.layout.layout_message,null);
            return view;
        }
    }
    
    public class TrendsFragment extends BaseFragment {
        @Override
        public View initView() {
            View view=View.inflate(mActivity, R.layout.layout_trends,null);
            return view;
        }
    }
  • 相关阅读:
    Hadoop 最讨厌的报错:运行VirtualBox提示0x00000000错误“0x00000000指令引用的0x00000000内存该内存不能为written?
    linux下面/usr/local和opt目录有何区别
    Hadoop是不是必须在linux上运行?(根本原因是操作系统Linux的权限开放优势)
    Hadoop主要配置文件的作用
    RPC模式
    Hadoop安装最后一步~Hadoop伪分布式配置
    word 使用中遇到的小细节(按空格键后面字不见;从编译器粘贴的代码出现乱码,word标题内容折叠效果实现)
    在VS Code下配置Julia
    使用pandas读取csv文件和写入文件
    ModuleNotFoundError: No module named 'sklearn.cross_validation'
  • 原文地址:https://www.cnblogs.com/lljh/p/7658478.html
Copyright © 2020-2023  润新知