Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android ViewPager的MVP架构

Android ViewPager的MVP架构搭建过程

作者:xiaoduyyy

本文主要介绍了ViewPager在Android中的作用以及使用场景,如引导页、图片浏览器、新闻或文章内容的多标签页等,同时,还详细阐述了如何通过MVP架构来搭建ViewPager,将视图和逻辑进行解耦,提高代码的可测试性、可复用性,使代码结构更清晰且易于扩展功能

ViewPager作用和使用场景

ViewPager 是 Android 中的一种滑动组件,允许用户左右滑动页面,提供了类似翻页的效果,通常用于实现多页面视图切换。它能够支持手势滑动,也可以通过编程来切换页面,使得内容展示更为流畅、直观。

常见的使用场景包括:

实现ViewPager2

新建FragmentAdapter继承FragmentStateAdapter:

public class FragmentAdapter extends FragmentStateAdapter {
    private String[] titles;
    private List<Fragment> fragmentList;
    public FragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle, String[] titles, List<Fragment> fragmentList) {
        super(fragmentManager, lifecycle);
        this.titles = titles;
        this.fragmentList = fragmentList;
    }
    @NonNull
    @Override
    public Fragment createFragment(int position) {
        return fragmentList == null ? null : fragmentList.get(position);
    }
    @Override
    public int getItemCount() {
        return fragmentList == null ? 0:fragmentList.size();
    }
}

我们获取了fragmentList和titles,下来简单创建ViewPager2里的Fragment,HomePagerFragment:

public class HomePagerFragment extends Fragment {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_items_fragment, container, false);
        return view;
    }
}

HomeFragment:

public class HomeFragment extends BaseFragment<HomePresenter> {
    private SwipeRefreshLayout swipeRefreshLayout;
    private Handler handler;
    private ViewPager2 viewPager;
    private List<Fragment> fragmentList;
    private FragmentAdapter fragmentAdapter;
    private String[] titles;
    @Override
    protected void initListener() {
    }
    @Override
    protected void initView(View view) {
        viewPager = view.findViewById(R.id.view_pager);
        initData();
        fragmentAdapter = new FragmentAdapter(getChildFragmentManager(), getLifecycle(), titles, fragmentList);
        viewPager.setAdapter(fragmentAdapter);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.home_fragment;
    }
    @Override
    public HomePresenter getPresenterInstance() {
        return new HomePresenter();
    }
    private void initData() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new HomePagerFragment());
        fragmentList.add(new HomePagerFragment());
        fragmentList.add(new HomePagerFragment());
        titles = new String[]{"1", "2", "3"};
    }
}

实现了ViewPager的绑定。

使用MVP架构搭建

新建HomeView接口,定义setupViewPager方法:

public interface HomeView {
    void setupViewPager(List<Fragment> fragmentList, String[] titles);
}

定义了一个方法 setupViewPager,该方法的主要作用是配置 ViewPager 的内容。

HomeFragment:

public class HomeFragment extends BaseFragment<HomePresenter> implements HomeView{
    private SwipeRefreshLayout swipeRefreshLayout;
    private ViewPager2 viewPager;
    private FragmentAdapter fragmentAdapter;
    private Handler handler;
    @Override
    protected void initListener() {
    }
    @Override
    protected void initView(View view) {
        viewPager = view.findViewById(R.id.view_pager);
        // 初始化数据,通过Presenter获取数据
        mPresenter.loadFragments();
    }
    @Override
    protected int getLayoutId() {
        return R.layout.home_fragment;
    }
    @Override
    public HomePresenter getPresenterInstance() {
        return new HomePresenter();
    }
    // 更新ViewPager的数据
    public void setupViewPager(List<Fragment> fragmentList, String[] titles) {
        fragmentAdapter = new FragmentAdapter(getChildFragmentManager(), getLifecycle(), titles, fragmentList);
        viewPager.setAdapter(fragmentAdapter);
    }
}

setupViewPager::

HomeView 接口中定义的方法,用于配置 ViewPager2

HomeModel:

public class HomeModel extends BaseModel<HomePresenter> {
    public HomeModel(HomePresenter presenter) {
        super(presenter);
    }
    public List<Fragment> getFragmentList() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomePagerFragment());
        fragmentList.add(new HomePagerFragment());
        fragmentList.add(new HomePagerFragment());
        return fragmentList;
    }
    public String[] getTitles() {
        return new String[]{"Tab1", "Tab2", "Tab3"};
    }
}

HomeModel主要作用是提供数据,这里提供了fragmentList和titles。

HomePresenter:

public class HomePresenter extends BasePresenter<HomeModel, HomeFragment> {
    @Override
    public HomeModel getModelInstance() {
        return new HomeModel(this);
    }
    public void loadFragments() {
        List<Fragment> fragmentList = mModel.getFragmentList();
        String[] titles = mModel.getTitles();
        if (getmView() != null) {
            getmView().setupViewPager(fragmentList, titles);
        }
    }
}

HomePresenter 负责处理 HomeFragment 的业务逻辑,尤其是加载和配置 ViewPager 中的 Fragment

loadFragments() 方法:

使用MVP搭建的优点

解耦视图和逻辑,便于维护

更好的可测试性

提高可复用性

代码结构更清晰

易于扩展功能

已经到底啦!!

到此这篇关于 Android ViewPager的MVP架构搭建的文章就介绍到这了,更多相关 Android ViewPager的MVP架构内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文