美文网首页高级UI
Android横向滚动插件-----HorizontalView

Android横向滚动插件-----HorizontalView

作者: 青春逗_a345 | 来源:发表于2019-02-14 11:49 被阅读212次

首先我们看下效果图

这是最基本的横向滑动菜单

图1

有人会说应该不会适配,所以我加了AutoLayout做了适配,首先我们在Application的onCreate() 方法中对AutoLayout操作;

public    class   MainAppliction   extends      Application{

@Override

public   void   onCreate() {

super.onCreate();

//autoLayout初始化

AutoLayoutConifg.getInstance().useDeviceSize().init(this);

    }

}

然后在AndroidManifest.xml中引用MainAppliction,并且设置

<!--outoLayout分辨率-->

android:name="design_width"

android:value="750"/>

android:name="design_height"

android:value="1334"/>

最后,我们在Activity里面操作了

horizontalBg.setAdapter(getNewBean(),newOnEditAttributeLstener() {

/**

            *

*@return0、不设置背景、1设置背景颜色、2、设置背景图片

*/

@Override

public  int  getViewType() {

       return2;

            }

@Override

public int  getVewHeight() {

     return  80;//设置菜单栏的高度

            }

@Override

public boolean   isAdjustModel() {

          return    true;//是否自适应

            }

@Override

public  int     getSelectBgRes() {

      return   R.drawable.shape_bg_red;//设置选中item的背景

            }

@Override

public  int    getNotSelectBgRes() {

      return  0;//设置未选中item的背景

            }

@Override

public   int   getSelectTextColor() {

        return    R.color.colorWrite;//设置选中item 字体颜色

            }

@Override

public   int  getNotSelectTextColor() {

        return   R.color.textMain;//设置未选中item 字体颜色

            }

@Override

public  boolean   isShowLine() {

      return   false;//隐藏下划线

            }

        });

另外附上所有item样式方法,这些属性应该够基本的Menu菜单使用

public interface   OnEditAttribute{

/**

    * 是否自适应

*@return

*/

boolean  isAdjustModel();

/*** ======设置View属性      ********/

/**

    *获得View的属性类型

*@return

*/

int    getViewType();

/**

    * 改变view高度

*@return

*/

int    getVewHeight();

/**

    * 改变view高度

*@return

*/

int  getVewWidth();

/**

    * 获取选中的背景颜色

*@return

*/

int   getSelectBgColor();

/**

    * 获取未选中的背景颜色

*@return

*/

int  getNotSelectBgColor();

/**

    * 获取选中的背景颜色

*@return

*/

int  getSelectBgRes();

/**

    * 获取未选中的背景颜色

*@return

*/

int   getNotSelectBgRes();

/*** ======设置Item属性      ********/

/**

    *获取选中的字体颜色

*@return

*/

int  getSelectTextColor();

/**

    *获取未选中的字体颜色

*@return

*/

int  getNotSelectTextColor();

/**

    * 获取字体大小

*@return

*/

int  getTextSize();

/**

    * 获取Item高度

*@return

*/

int  getItemHeight();

/*** ======设置bottom属性      ********/

/**

    *获得底部下划线的属性类型

*@return

*/

int  getBottomViewType();

/**

    * 是否显示下划线

*@return

*/

boolean  isShowLine();

/**

    * 改变bottom下划线高度

*@return

*/

int  getBottomHeight();

/**

    * 改变bottom宽度

*@return

*/

int  getBottomWidth();

/**

    * 设置底部下划线背景颜色

*@return

*/

int  getBottomBackgoundColor();

/**

    * 设置底部图片

*@return

*/

int  getBottomDrawable();

}

也许有人会说UI给了奇葩的menu菜单怎么办,不要慌,这里还可以自定义Item布局

图2

代码也是非常简单的,红色小框框的是你自己写的Item布局,黑色小框框是你的Item数据;红色大框框是你的点击事件

图3

除此之外,你还可以写横向滚动器

图4,


HorizontalView原理是继承RecyclerView,通过重写SnapHelper 来改变Recyclerview显示横向或纵向的位置,然后封装了BaseRecyclerViewAdapter基类.具体的看源码

有问题加群478720016 一起交流学习

HorizontalView

相关文章

网友评论

    本文标题:Android横向滚动插件-----HorizontalView

    本文链接:https://www.haomeiwen.com/subject/qanseqtx.html