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

所属分类: 软件编程 / Android 阅读数: 77
收藏 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>

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

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

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多