【领券联盟】笔记:视频31-首页标题已及ViewPager的预加载
破小布的小花狗
发表于
2020-03-31 08:36
1364
课堂笔记
课堂笔记
首页标题
- 创建首页标题布局 include_home_pager_title_part
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<View
android:layout_width="10dp"
android:layout_height="4dp"
android:background="@drawable/shape_title_flag" />
<TextView
android:id="@+id/home_pager_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:textSize="14sp"
android:textStyle="bold"
tools:text="推荐" />
<View
android:layout_width="10dp"
android:layout_height="4dp"
android:background="@drawable/shape_title_flag" />
</LinearLayout>
- 用 include 把标题布局加到 fragment_home_pager 布局中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorViewPagerBg"
android:gravity="center"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/looper_pager"
android:layout_width="match_parent"
android:layout_height="125dp"
android:layout_marginBottom="14dp"
android:overScrollMode="never" />
<!-- 标题布局 -->
<include layout="@layout/include_home_pager_title_part" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/home_pager_content_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="11dp"
android:overScrollMode="never" />
</LinearLayout>
- 在 HomePagerFragment 中绑定标题
@BindView(R.id.home_pager_title)
public TextView currentCategoryTitleTv;
- 给标题设置数据
@Override
protected void loadData() {
Bundle arguments = getArguments();
String title = arguments.getString(Constant.KEY_HOME_PAGER_TITLE);
mMaterialId = arguments.getInt(Constant.KEY_HOME_PAGER_MATERIAL_ID);
if (mCategoryPagerPresent != null) {
mCategoryPagerPresent.getContentByCategoryId(mMaterialId);
}
if (currentCategoryTitleTv != null) {
//给title设置数据
currentCategoryTitleTv.setText(title);
}
}
ViewPager的预加载
- 在 HomeFragment 中,给 ViewPager 的 屏幕外页数限制 设置成页面个数的总数量,这样应用启动时就会加载所有页面的数据
@Override
public void onCategoryLoaded(Categories categories) {
setUpState(State.SUCCESS);
if (mHomePagerAdapter != null) {
mHomePagerAdapter.setCategoriesData(categories);
//设置 屏幕外页数限制
homePager.setOffscreenPageLimit(categories.getData().size());
}
}
- Google 了一下
- 观看视频