Android实现九宫格横向左右滑动
作者:zuo_er_lyf 发布时间:2022-03-06 13:09:47
标签:Android,九宫格,滑动
项目中大多都会有很多的分类,且左右滑动,如美团首页(下图):
不难发现包含2部分内容:1.左右滑动的页面,2.指示器。
大度一般都会想到,viewPager+GridView,这里介绍另外的的一种方法,也做下记录;
GridViewPager+MagicIndicator(万能指示器)
一、引入build.gradle
compile 'com.yhy:gvp:1.1.0'
compile 'com.github.hackware1993:MagicIndicator:1.5.0'
如果报错,在项目build.gradle中加入:
repositories {
...
maven {
url "https://jitpack.io"
}
}
二、布局代码
<LinearLayout
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.yhy.gvp.widget.GridViewPager
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:id="@+id/grid_viewpager"
android:layout_width="match_parent"
android:layout_height="150dp"
app:num_columns="5"
app:page_size="10"></com.yhy.gvp.widget.GridViewPager>
<net.lucode.hackware.magicindicator.MagicIndicator
android:id="@+id/indicator_container"
android:layout_width="wrap_content"
android:layout_height="30dp">
</net.lucode.hackware.magicindicator.MagicIndicator>
</LinearLayout>
属性说明:
三、关键代码:
@InjectView(R.id.grid_viewpager)
GridViewPager gridViewpager;
@InjectView(R.id.indicator_container)
MagicIndicator indicatorContainer; 使用ButterKnife这里不多介绍
indexTypeAdapter=new IndexTypeAdapter(getActivity(),R.layout.item_index_type,typeDatas);//页面内容适配器
gridViewpager.setGVPAdapter(indexTypeAdapter);
Log.i("datas",(int) Math.ceil(typeDatas.size() / 10)+"");
CommonNavigator commonNavigator = new CommonNavigator(context);//指示器
commonNavigator.setAdapter(new CommonNavigatorAdapter() {
@Override
public int getCount() {
int num=typeDatas.size()/10;
if(typeDatas.size() % 10>0){
num++;
}
return typeDatas==null?0:num;
}
@Override
public IPagerTitleView getTitleView(Context mContext, final int i) {
CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
View view=View.inflate(context,R.layout.single_image_layout,null);
final ImageView iv_image=view.findViewById(R.id.iv_image);
iv_image.setImageResource(R.drawable.point_unfocused);
commonPagerTitleView.setContentView(view);//指示器引入外部布局,可知指示器内容可根据需求设置,多样化
commonPagerTitleView.setOnPagerTitleChangeListener(new CommonPagerTitleView.OnPagerTitleChangeListener() {
@Override
public void onSelected(int i, int i1) {
iv_image.setImageResource(R.drawable.point_focused);
}
@Override
public void onDeselected(int i, int i1) {
iv_image.setImageResource(R.drawable.point_unfocused);
}
@Override
public void onLeave(int i, int i1, float v, boolean b) {
}
@Override
public void onEnter(int i, int i1, float v, boolean b) {
}
});
return commonPagerTitleView;
}
@Override
public IPagerIndicator getIndicator(Context context) {
return null;
}
});
indicatorContainer.setNavigator(commonNavigator);
ViewPagerHelper.bind(indicatorContainer, gridViewpager);//页面内容与指示器关联
四、左右滑动页面内容适配器adapter
public class IndexTypeAdapter extends GVPAdapter<IndexAllTypeBean.TypeListBean> {
private Context context;
public IndexTypeAdapter(Context context,int layoutResId, @Nullable List<IndexAllTypeBean.TypeListBean> data) {
super(layoutResId, data);
this.context=context;
}
@Override
public void bind(View item, int position, IndexAllTypeBean.TypeListBean data) {
CircleImageView iv_image=item.findViewById(R.id.iv_image);
TextView tv_type_name=item.findViewById(R.id.tv_type_name);
Picasso.with(context).load(data.getImageUrl()).into(iv_image);
tv_type_name.setText(data.getName());
}
}
//IndexAllTypeBean.TypeListBean 为数据内容实体类,不做介绍
五、内容item布局
<LinearLayout
android:orientation="vertical"
android:paddingBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
//自定义圆形图片,可用ImageView 替代
<com.example.administrator.takeout.ui.widght.CircleImageView
android:id="@+id/iv_image"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:id="@+id/tv_type_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
来源:https://blog.csdn.net/zuo_er_lyf/article/details/81740630


猜你喜欢
- 值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容
- 文章目录 简介增量构建自定义inputs和outputs运行时API隐式依赖输入校验自定义缓存方法输入归一化其他使用技巧简介在我们使用的各种
- 1、HttpClient:代码复杂,还得操心资源回收等。代码很复杂,冗余代码多,不建议直接使用。2、RestTemplate: 是 Spri
- 用java实现的数组创建二叉树以及递归先序遍历,递归中序遍历,递归后序遍历,非递归前序遍历,非递归中序遍历,非递归后序遍历,深度优先遍历,广
- Spring Boot 自动装配最重要的注解@SpringBootApplication@Target(ElementType.TYPE)@
- 前言业务开发中搜索框和列表的组合页面应该是比较常见的场景,那么有什么坑呢?最近在开发过程就遇到了一个问题,输入搜索关键词查询接口返回数据后,
- 注意是maven的webapp:选择maven下一步下一步。maven下载过慢在setting中加入镜像。 我也有疑问这是什么鬼格式,但是证
- 本文实例讲述了Android编程处理窗口控件大小,形状,像素等UI元素工具类。分享给大家供大家参考,具体如下:/*** 处理窗口控件大小,形
- 最近项目中用到了Elasticsearch5.4(ES)是比较新的一个版本,使用的过程中出现了很多的问题,很是头疼,但是问题最终还是解决掉了
- 在项目中有事需要对值为NULL的对象中Field不做序列化输入配置方式如下:[配置类型]:源码包中的枚举类:public static en
- 最近一直在对接接口,上游返回的都是 JSON 数据,我们需要将这些数据进行保存,我们可以解析成 Map 通过 key 的方式进行获取,然后
- DataSource在数据库应用中,客户端与数据库服务端建立的连接对象(Connection)是宝贵的资源,每次请求数据库都创建连接,使用完
- 前言;Apache common-pool对象池介绍:对象生命周期、Config详解、代码说明对象生命周期Config详解maxActive
- 使用百度地图出现闪退一般情况下出现闪退是在AndroidManifest.xml文件中未在application标签中配置<meta-
- 前一段时间,在做摄像头拍照上传,摄像头拍的照片为base64编码格式的字符串,需要上传至项目中,则需要使用到将base64编码字符串转换为图
- 1.面对对象的初步认识1.1什么是面向对象用面向对象的思想来涉及程序,更符合人们对事物的认知,对于大型程序的设计、扩展以及维护都非常友好。1
- 本文实例讲述了C#实现文件压缩与解压的方法。分享给大家供大家参考,具体如下:在企业开发过程中经常会遇到文件的压缩与解压,虽然网上很多流行的压
- 本文汇总了高效C#编码常见的优化原则,对于进行C#程序设计来说有很大的参考借鉴作用。具体列出如下:1.foreach VS for 语句Fo
- 使用@Autowired注解有错误提示使用Spring boot +mybatis框架时,在service实现类中使用Mapper类,给Ma
- 在项目中为了友好化,对于错误页面,我们常常会使用自定义的页面。SSM框架组合时代,我们通常通过拦截或者在web.xml中设置对于错误码的错误