每日总结

学习fragment和viewpage的联合开发。

业务逻辑:

1:在main.xml里设置viewpage和button的布局,button的布局单独报开一个xml文件做出来,做完后需要将button的布局加入到main.xml之中。

2:初始化viewpage,使得viewpage适配一个或多个fragment,用到fragment里面的实例化函数

public class MyFragmenPageAdapter extends FragmentStateAdapter {
    List<Fragment> fragmentList=new ArrayList<>();
    //构造器
    public MyFragmenPageAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, List<Fragment> fragments) {
        super(fragmentManager, lifecycle);
        fragmentList=fragments;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragmentList.get(position);
    }

    @Override
    public int getItemCount() {
        return fragmentList.size();
    }
}

 

 private void initPage() {
        viewPager2=findViewById(R.id.viewpage);

        ArrayList<Fragment> fragments=new ArrayList<>();
        //创建了四个Fragment的对象,调用BlankFragment里面的newInstance()函数将
        fragments.add(BlankFragment.newInstance("微信聊天"));
        fragments.add(BlankFragment.newInstance("通讯录"));
        fragments.add(BlankFragment.newInstance("朋友圈"));
        fragments.add(BlankFragment.newInstance("我"));
        //创建自定义的 viewpager的适配器
        MyFragmenPageAdapter myFragmenPageAdapter=new MyFragmenPageAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        //将fragment和viewpage进行适配
        viewPager2.setAdapter(myFragmenPageAdapter);
        //一次性响应fragment和按钮,滑动监听接口
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
            //获取选择的当前fragment的页面位置,获取位置后改变成按钮被按下的状态,调用函数改变
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

3:初始化按钮,对按钮的监听,按下后改变状态

private void initTableaView() {
        llchat=findViewById(R.id.id_weixin);
        llchat.setOnClickListener(this);

        llcontact=findViewById(R.id.id_contact);
        llcontact.setOnClickListener(this);

        llfind=findViewById(R.id.id_find);
        llfind.setOnClickListener(this);

        llprofil=findViewById(R.id.id_profil);
        llprofil.setOnClickListener(this);

        ivchat=findViewById(R.id.table__tv_wexin);
        ivcontact=findViewById(R.id.table__tv_contact);
        ivfind=findViewById(R.id.table__tv_find);
        ivprofil=findViewById(R.id.table__tv_profil);
        //设置界面按钮的初始点亮状态
        ivchat.setSelected(true);
        //图片类型的标记变量,找到当前的按钮位置,当页面滑动时,先将标记变量(即上一的按钮)只为你FALSE,当页面滑动后先将新的按钮设置为点亮,再重新赋值标记变量
        ivcurrent=ivchat;
    }

 

  private void changeTab(int position) {
        //上一次的按钮置为熄灭
        ivcurrent.setSelected(false);
        switch (position){
            //点击响应后联动相应的viewpage
            case R.id.id_weixin:
               viewPager2.setCurrentItem(0,false);
            case 0:
                ivchat.setSelected(true);
                ivcurrent=ivchat;
                break;

            case R.id.id_contact:
                viewPager2.setCurrentItem(1,false);
            case 1:
                ivcontact.setSelected(true);
                ivcurrent=ivcontact;
                break;

            case R.id.id_find:
                viewPager2.setCurrentItem(2,false);
            case 2:
                ivfind.setSelected(true);
                ivcurrent=ivfind;
                break;

            case R.id.id_profil:
                viewPager2.setCurrentItem(3,false);
            case 3:
                ivprofil.setSelected(true);
                ivcurrent=ivprofil;
                break;

        }
    }

Blankfragment的代码(删减后)

public class BlankFragment extends Fragment {
    private static final String ARG_TEXT = "param1";
   View root;
    private String mTextString;
    public BlankFragment() {
        // Required empty public constructor
    }


    public static BlankFragment newInstance(String param1) {
        //创建fragment对象
        BlankFragment fragment = new BlankFragment();
        //创建bundle对象
        Bundle args = new Bundle();
        //将传入的参数设置成string
        args.putString(ARG_TEXT, param1);
        //创建这个fragment.xml的格式
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTextString = getArguments().getString(ARG_TEXT);

        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       if(root==null){
           root=inflater.inflate(R.layout.fragment_blank,container,false);
       }
       initView();
       return  root;
    }
    private void initView() {
        TextView textView =root.findViewById(R.id.text);
        textView.setText(mTextString);
    }
}

 

posted @ 2023-02-25 20:10  cojames  阅读(19)  评论(0编辑  收藏  举报