Android实现九宫格横向左右滑动

所属分类: 软件编程 / Android 阅读数: 46
收藏 0 赞 0 分享

项目中大多都会有很多的分类,且左右滑动,如美团首页(下图):

不难发现包含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>

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多