TabLayout+ViewPager+FragmentPagerAdapter 实现懒加载(配合RecycleVIew+SmartRefreshLayout库的使用)

TabLayout+ViewPager+FragmentPagerAdapter 实现懒加载(配合RecycleVIew+SmartRefreshLayout库的使用)

先上效果图:

简单说明:做个简单的记录,代码会使用一些简单概述,详细代码后面再完善

1.库的使用及引用情况

使用到的库:

    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4'
    implementation 'com.yanzhenjie:recyclerview-swipe:1.0.3'
    implementation 'com.android.support:recyclerview-v7:29.0.0-rc02'
2.宿主Fragment中的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
     <!-- TODO: Update blank fragment layout -->
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorFullWidth="false"
        app:tabIndicatorColor="#FFFDDC4E"
        app:tabSelectedTextColor="@color/color_FF222222"
        app:tabTextAppearance="@style/TabLayoutTextStyle" />
        
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white"/>
</LinearLayout>

说明:android工程引用的是androidx的包

3.宿主Fragment的使用
public class HomeFragment extends Fragment{
	 private MyFragmentPagerAdapter adapter;
	 .....
	 public void initView(View rootView) {
        ViewPager viewPager = rootView.findViewById(R.id.viewPager);
        adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), getContext());
        viewPager.setAdapter(adapter);
        //这个地方:setOffscreenPageLimit传0和1的效果是一样的,(预加载的效果)
        viewPager.setOffscreenPageLimit(0);
        //TabLayout
        TabLayout tabLayout = rootView.findViewById(R.id.tablayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        //显示当前那个标签页
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);
    }
    .....
}
4.创建FragmentPagerAdapter适配器
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private String[] titles = new String[]{"0","1","2","3","4","5","6"};
    public int COUNT = titles.length;
    private Context context;
    
    public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return PageFragment.newInstance("0");
            case 1:
                return PageFragment.newInstance("1");
            case 2:
                return PageFragment.newInstance("2");
            case 3:
                return PageFragment.newInstance("3");
            case 4:
                return PageFragment.newInstance("4");
            case 5:
                return PageFragment.newInstance("5");
            case 6:
                return PageFragment.newInstance("6");
        }
        return null;
    }

    @Override
    public int getCount() {
        return COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }

	//一定要使用这个,记得注释掉super的方法
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
    }
}
5.构建最终显示数据的Fragment
public class PageFragment extends Fragment{

    private SmartRefreshLayout mSmartRefreshLayout;
    private RecyclerView mRecyclerView;

  public static PageFragment newInstance(String title) {
        PageFragment fragment = new PageFragment();
        Bundle ban = new Bundle();
        ban.putString("key", title);
        fragment.setArguments(ban);
        return fragment;
    }
 	@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        title = getArguments().getString("key");
        initView(view);
        initAdatper();
        //通过getUserVisibleHit()方法判断当前页面是否可见,来加载不同key页面要求的数据
        if (getUserVisibleHint()){
            initDatas();
        }
    }
private void initView(View view) {
        mSmartRefreshLayout = view.findViewById(R.id.fragment_page_srf);
        mRecyclerView = view.findViewById(R.id.fragment_page_rl);
        mRecyclerView.addItemDecoration(new MyDecoration());
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mSmartRefreshLayout.autoRefresh();
        mSmartRefreshLayout.setOnRefreshListener(refreshlayout -> {
  			initDatas();
        });
        mSmartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            initDatas();
        }); 
    }
	private void initAdatper() {
		//这里面需要处理的是adapter,这个很简单,不多说了。
	}
	private void initDatas(){
		//调用接口或者本地数据,加载数据到adapter中
	}
}
6.总结

开始的时候找了很多方法来实现,但是发现一些效果都不是很好用,然后就调试发现,可以使用如上方法来完成。日后会继续完善,不说了,撸代码了。。。。

你可能感兴趣的:(android的成长)