package com.yun.viewpager;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import com.yun.viewpager.databinding.ActivityMainBinding;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding mBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
setRegularPager();
setCategoryPager();
setFragmentPager();
}
private void setRegularPager() {
List<String> dataList = new ArrayList<>();
for (int i = 0; i < 4; i++) {
dataList.add("这是我的第" + (i + 1) + "个Pager");
}
RegularPagerAdapter adapter = new RegularPagerAdapter(this, dataList);
mBinding.viewPager1.setAdapter(adapter);
}
private void setCategoryPager() {
ArrayList<ArrayList<ItemBean>> dataList = getCategoryPagerData(8);
CategoryPagerAdapter adapter = new CategoryPagerAdapter(this, dataList, 4, (page, position, bean) -> {
Toast.makeText(this, "点击:" + bean.getName() + ",位置:" + position, Toast.LENGTH_SHORT).show();
});
mBinding.viewPager2.setAdapter(adapter);
}
private ArrayList<ArrayList<ItemBean>> getCategoryPagerData(int pageCount) {
ArrayList<ArrayList<ItemBean>> dataList = new ArrayList<>();
ArrayList<ItemBean> list = new ArrayList<>();
list.add(new ItemBean("箱包", R.mipmap.icon_bag));
list.add(new ItemBean("美妆", R.mipmap.icon_beauty));
list.add(new ItemBean("图书", R.mipmap.icon_book));
list.add(new ItemBean("电子", R.mipmap.icon_computer));
list.add(new ItemBean("厨具", R.mipmap.icon_cooking));
list.add(new ItemBean("饮品", R.mipmap.icon_drink));
list.add(new ItemBean("水果", R.mipmap.icon_fruits));
list.add(new ItemBean("医药", R.mipmap.icon_medicine));
list.add(new ItemBean("母婴", R.mipmap.icon_mother_and_baby));
list.add(new ItemBean("户外", R.mipmap.icon_outdoors));
list.add(new ItemBean("鞋子", R.mipmap.icon_shoes));
list.add(new ItemBean("女装", R.mipmap.icon_skirt));
list.add(new ItemBean("小家电", R.mipmap.icon_small_appliances));
list.add(new ItemBean("零食", R.mipmap.icon_snacks));
list.add(new ItemBean("男装", R.mipmap.icon_t_shirt));
list.add(new ItemBean("玩具", R.mipmap.icon_toys));
list.add(new ItemBean("蔬菜", R.mipmap.icon_vegetables));
list.add(new ItemBean("风车", R.mipmap.icon_windmill));
int bSize = list.size();
int pageSize = bSize / pageCount + (bSize % pageCount == 0 ? 0 : 1);
for (int i = 0; i < pageSize; i++) {
ArrayList<ItemBean> itemBeans = new ArrayList<>();
int length = pageCount;
if (pageCount > bSize - i * pageCount) {
length = bSize - i * pageCount;
}
for (int j = i * pageCount; j < i * pageCount + length; j++) {
itemBeans.add(list.get(j));
}
dataList.add(itemBeans);
}
return dataList;
}
private PagerFragment fragment1;
private PagerFragment fragment2;
private PagerFragment fragment3;
private List<Fragment> fgList;
private void setFragmentPager() {
fragment1 = new PagerFragment(1);
fragment2 = new PagerFragment(2);
fragment3 = new PagerFragment(3);
fgList = new ArrayList<>();
fgList.add(fragment1);
fgList.add(fragment2);
fgList.add(fragment3);
mBinding.viewPager3.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fgList.get(position);
}
@Override
public int getCount() {
return fgList.size();
}
});
selectPage(0);
mBinding.rgPage.setOnCheckedChangeListener((radioGroup, checkedId) -> {
if (checkedId == mBinding.rbConfig.getId()) {
selectPage(0);
} else if (checkedId == mBinding.rbImage.getId()) {
selectPage(1);
} else if (checkedId == mBinding.rbVideo.getId()) {
selectPage(2);
}
});
}
private void selectPage(int index) {
mBinding.viewPager3.setCurrentItem(index, false);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android viewPager的三种使用方式
共85个文件
png:18个
xml:18个
sample:13个
需积分: 0 2 下载量 86 浏览量
2022-12-13
11:28:35
上传
评论
收藏 268KB RAR 举报
温馨提示
Android viewPager的三种使用方式:常规左右滑动viewPager、上下滑动viewPager结合网格布局recyclerview实现电商分类入口、无需滑动viewPager结合Fragment使用RadioGroup实现单选按钮展示不同View
资源推荐
资源详情
资源评论
收起资源包目录
viewPagerTest.rar (85个子文件)
viewPagerTest
gradle.properties 1KB
gradle
wrapper
gradle-wrapper.jar 58KB
gradle-wrapper.properties 236B
app
src
androidTest
java
com
yun
viewpager
ExampleInstrumentedTest.java 748B
test
java
com
yun
viewpager
ExampleUnitTest.java 378B
main
java
com
yun
viewpager
PagerFragment.java 938B
CategoryPagerAdapter.java 3KB
VerticalViewPager.java 2KB
ItemBean.java 496B
RegularPagerAdapter.java 1KB
ItemAdapter.java 2KB
MainActivity.java 5KB
NoScrollViewPager.java 2KB
res
mipmap-xxhdpi
ic_launcher_round.webp 6KB
ic_launcher.webp 3KB
mipmap-hdpi
ic_launcher_round.webp 3KB
ic_launcher.webp 1KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 272B
ic_launcher_round.xml 272B
values-night
themes.xml 815B
mipmap-mdpi
ic_launcher_round.webp 2KB
ic_launcher.webp 982B
mipmap-xxxhdpi
icon_drink.png 5KB
icon_t_shirt.png 7KB
icon_small_appliances.png 10KB
icon_beauty.png 14KB
icon_bag.png 12KB
icon_toys.png 16KB
icon_cooking.png 4KB
icon_skirt.png 7KB
icon_snacks.png 9KB
icon_book.png 6KB
ic_launcher_round.webp 8KB
icon_windmill.png 6KB
icon_outdoors.png 10KB
icon_vegetables.png 12KB
icon_computer.png 3KB
icon_mother_and_baby.png 5KB
icon_medicine.png 5KB
icon_fruits.png 10KB
icon_shoes.png 6KB
ic_launcher.webp 4KB
mipmap-xhdpi
ic_launcher_round.webp 4KB
ic_launcher.webp 2KB
xml
data_extraction_rules.xml 551B
backup_rules.xml 478B
values
colors.xml 477B
strings.xml 75B
themes.xml 815B
layout
fragment_pager.xml 730B
item_ad.xml 1KB
activity_main.xml 3KB
item_pager_category.xml 693B
item_pager_regular.xml 711B
drawable
ic_launcher_background.xml 5KB
text_button_checked.xml 289B
selector_check_btn.xml 333B
AndroidManifest.xml 1KB
proguard-rules.pro 750B
libs
build.gradle 1KB
.gitignore 6B
gradlew.bat 3KB
.git
HEAD 23B
refs
heads
tags
objects
pack
info
description 73B
info
exclude 240B
hooks
post-update.sample 189B
prepare-commit-msg.sample 1KB
commit-msg.sample 896B
pre-receive.sample 544B
update.sample 4KB
pre-commit.sample 2KB
pre-rebase.sample 5KB
applypatch-msg.sample 478B
fsmonitor-watchman.sample 5KB
push-to-checkout.sample 3KB
pre-applypatch.sample 424B
pre-push.sample 1KB
pre-merge-commit.sample 416B
config 130B
build.gradle 229B
settings.gradle 330B
local.properties 424B
gradlew 6KB
.gitignore 225B
共 85 条
- 1
资源评论
Ryfall
- 粉丝: 27
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功