android九宫格可分页加载控件使用详解
作者:poorSir 时间:2022-03-04 08:14:02
本文实例为大家分享了android九宫格可分页加载控件的具体实现代码,供大家参考,具体内容如下
github地址
基本思路是viewpager+gridview,每一页viewpager视图有一个gridview,gridview加载九格。所以要保证数据的准确性。数据以List< List < T>>的形式。控件实现了高度自适应,根据控件的个数,计算控件的高度。内部的viewpager和gridview视图都以动态创建,无需新建一个xml文件,整个控件只需要一个item的布局文件。
使用方法
使用时可以先请求一个接口,需初始化数据,数据初始化需要根据总个数初始化所有数据,在每次分页加载的时候,再通过请求当页的数据,更改数据,刷新页面。
viewPager = findViewById(R.id.ninePages);
//数据初始化
List<List<TestEntity>> lists = new ArrayList<>();
for (int i = 0; i < 6; i++) {//页数
List<TestEntity> list1 = new ArrayList<>();
for (int j = 0; j < 9; j++) {//每页个数
TestEntity entity = new TestEntity();
entity.setText("j=" + j + "/i=" + i);
list1.add(entity);
}
lists.add(list1);
}
//设置不每页重复加载
viewPager.setEveryRequest(false);
//设置分页加载
viewPager.setPagingLoad(true);
viewPager.setListener(new RequestDataListener<TestEntity>() {
@Override
public void request(int position) {//分页加载
List<TestEntity> changeList =new ArrayList<>();
for (int i = 0; i < 9; i++) {
TestEntity testEntity = new TestEntity();
testEntity.setText("change" + i);
changeList.add(testEntity);
}
//数据刷新
viewPager.dataInvalidate(position,changeList);
}
@Override
public int gridViewColumn() {//gridview列数
return 3;
}
@Override
public View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent, List<TestEntity> list) {
ViewHolder viewHolder = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.item_gridview,parent,false);
viewHolder = new ViewHolder();
viewHolder.textView = convertView.findViewById(R.id.textview);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView.setText(list.get(position).getText());
return convertView;
}
});
viewPager.setList(lists);
class ViewHolder{
private TextView textView;
}
注意:viewPager.setList(lists);要在最后调用
视图控件:
public class NinePagesPagination<T> extends LinearLayout {
private View mView;
private ViewPager viewPager;
private Context context;
private List<List<T>> list = new ArrayList<>();
//已经请求过的,不在重复请求
private List<Integer> hasAdded = new ArrayList<>();
//是否每次请求
private boolean isEveryRequest;
//是否进行分页加载
private boolean isPagingLoad;
private RequestDataListener listener;
private MyViewPagerAdapter adapter;
private ViewPager.OnPageChangeListener pageChangeListener;
public NinePagesPagination(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
this.context = context;
viewPager = new ViewPager(context);
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
viewPager.setLayoutParams(params);
mView = viewPager;
addView(mView);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(listener!=null && listener.gridViewColumn() != 0){
int column = listener.gridViewColumn();
int row = (int) Math.ceil((double) list.get(0).size()/column);
int heightMeasureSpec2 = MeasureSpec.makeMeasureSpec(row*getWidth()/column,
MeasureSpec.AT_MOST);
setMeasuredDimension(widthMeasureSpec,heightMeasureSpec2);
}
}
public void setListener(RequestDataListener listener) {
this.listener = listener;
}
public void setPageChangeListener(ViewPager.OnPageChangeListener pageChangeListener) {
this.pageChangeListener = pageChangeListener;
}
public void setEveryRequest(boolean everyRequest) {
isEveryRequest = everyRequest;
}
public void setPagingLoad(boolean pagingLoad) {
isPagingLoad = pagingLoad;
}
public void setList(List<List<T>> list) {
this.list = list;
initView();
invalidate();
}
private void initView() {
adapter = new MyViewPagerAdapter(context, list,listener);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(pageChangeListener != null){
pageChangeListener.onPageScrolled(position,positionOffset,positionOffsetPixels);
}
}
@Override
public void onPageSelected(int position) {
if(pageChangeListener != null){
pageChangeListener.onPageSelected(position);
}
if (isPagingLoad) {//分页加载
if (isEveryRequest) {//每次都请求
adapter.setCurrentPosition(position);
listener.request(position);
} else {//请求过一次将不在重复请求
if (!hasAdded.contains(position)) {
hasAdded.add(position);
adapter.setCurrentPosition(position);
listener.request(position);
}
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
if(pageChangeListener != null){
pageChangeListener.onPageScrollStateChanged(state);
}
}
});
}
//数据刷新
public void dataInvalidate(int position,List<T> changeList){
list.set(position, changeList);
adapter.notifyDataSetChanged();
}
}
viewpager适配器:
public class MyViewPagerAdapter<T> extends PagerAdapter {
private Context context;
private List<List<T>> list;
private LayoutInflater inflater;
private MyGridViewAdapter adapter;
private int currentPosition;
private RequestDataListener listener;
public MyViewPagerAdapter(Context context, List<List<T>> list,RequestDataListener listener) {
this.context = context;
this.list = list;
this.listener = listener;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
GridView view = new GridView(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(params);
view.setTag(position);
if(listener!=null && listener.gridViewColumn() != 0){
view.setNumColumns(listener.gridViewColumn());
}
adapter = new MyGridViewAdapter(context, list.get(position),listener);
view.setAdapter(adapter);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
@Override
public int getItemPosition(@NonNull Object object) {
//viewpager立即刷新页面
if (list != null && list.size() == 0) {
return POSITION_NONE;
}
View view = (View) object;
if (currentPosition == (Integer) view.getTag()) {
return POSITION_NONE;
} else {
return POSITION_UNCHANGED;
}
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
if(adapter != null){
adapter.notifyDataSetChanged();
}
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
}
gridview适配器
public class MyGridViewAdapter<T> extends BaseAdapter{
private Context context;
private List<T> list;
private LayoutInflater inflater;
private RequestDataListener listener;
public MyGridViewAdapter(Context context, List<T> list,RequestDataListener listener){
this.context = context;
this.list = list;
this.listener = listener;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = listener.getView(inflater,position,convertView,parent,list);
int width;
//计算item的宽
if(listener != null &listener.gridViewColumn() != 0){
width = parent.getWidth()/listener.gridViewColumn();
}else{
width = parent.getWidth()/3;
}
//设置item的宽高相等
convertView.setLayoutParams(new AbsListView.LayoutParams(width,width));
return convertView;
}
}
暴露的接口
public interface RequestDataListener<T> {
void request(int position);
//gridView列数
int gridViewColumn();
View getView(LayoutInflater inflater, int position, View convertView, ViewGroup parent, List<T> list);
}
来源:https://blog.csdn.net/qq_31433525/article/details/82252232
标签:android,九宫格,分页加载
0
投稿
猜你喜欢
浅谈Java变量的初始化顺序详解
2023-07-26 03:23:17
springboot项目中使用Swagger的简单示例
2023-01-14 05:18:24
通过OpenGL ES混合模式缩放视频缓冲区来适应显示尺寸
2023-11-07 15:56:41
android 获取视频,图片缩略图的具体实现
2023-07-28 00:20:39
Spring Cloud Gateway重试机制的实现
2023-11-06 01:32:59
Android车载多媒体开发MediaSession框架示例详解
2022-08-10 19:49:20
C# 利用PdfSharp生成Pdf文件的示例
2022-01-18 17:31:30
Android使用ListView实现下拉刷新及上拉显示更多的方法
2023-01-10 04:29:45
ShardingSphere jdbc集成多数据源的实现步骤
2023-11-25 07:54:56
浅谈一下SpringCloud中Hystrix服务熔断和降级原理
2021-10-02 08:46:41
java利用udp实现发送数据
2023-03-21 18:30:49
android操作XML的几种方法总结
2021-08-10 06:06:16
C#编程中设置程序只可被运行一次的方法
2022-08-09 08:36:32
C#基于简单工厂模式实现的计算器功能示例
2023-06-19 12:40:10
Java 回调函数详解及使用
2023-11-03 02:32:35
基于java实现人机猜拳游戏
2021-06-09 08:50:32
Java用递归方法解决汉诺塔问题详解
2022-11-23 03:11:40
SpringBoot整合Elasticsearch游标查询的示例代码(scroll)
2022-02-11 02:02:13
Java身份证号码校验工具类详解
2022-09-03 18:06:27
一篇文章带你搞懂Java线程池实现原理
2021-10-23 05:45:36