android 5.0+6.0新特性

5.android5.0介绍

1.十大新特性
(1). 全新Material Design设计风格
     * 新的视觉语言,在基本元素的处理上,借鉴了传统的印刷设计,字体版式、网格系统、空间、比例、配色、图像使用等这些基础  的平面设计规范
(2). 支持多种设备
(3). 全新的通知中心设计
(4). 支持64位ART虚拟机
 * 谷歌承诺所有性能都会比原来提升一倍,Android Lollipop支持更大的寄存器,支持新的指令集,提升了内存寻址空间,未来Android智能手机将支持4GB以上的内存。
(5). Project Volta电池续航改进计划
(6). 全新的“最近应用程序”
(7). 改进安全性
(8). 不同数据独立保存
(9). 改进搜索
(10). `新的API支持`,蓝牙4.1、USB Audio、多人分享等其它特性

 

2.Material Design
     设计语言,设计标准,设计规范
(1)官网:http://www.google.com/design/spec/material-design/introduction.html
(2)中文翻译1:http://www.mobileui.cn/material-design/
(3)中文翻译2:http://design.1sters.com/#b

 

6.感受5.0新特性

7.默认的3个主题,修改主题

1.详情见——主题样式.html

2.

3.

  1. android:colorPrimaryDark 应用的主要暗色调,statusBarColor默认使用该颜色
  2. android:statusBarColor 状态栏颜色,默认使用colorPrimaryDark
  3. android:colorPrimary 应用的主要色调,actionBar默认使用该颜色
  4. android:windowBackground 窗口背景颜色
  5. android:navigationBarColor 底部栏颜色
  6. android:colorForeground 应用的前景色,ListView的分割线,switch滑动区默认使用该颜色
  7. android:colorBackground 应用的背景色,popMenu的背景默认使用该颜色
  8. android:colorAccent 一般控件的选种效果默认采用该颜色
  9. android:colorControlNormal 控件的默认色调 
  10. android:colorControlHighlight 控件按压时的色调
  11. android:colorControlActivated 控件选中时的颜色,默认使用colorAccent
  12. android:colorButtonNormal 默认按钮的背景颜色
  13. android:textColor Button,textView的文字颜色
  14. android:textColorPrimaryDisableOnly RadioButton checkbox等控件的文字
  15. android:textColorPrimary 应用的主要文字颜色,actionBar的标题文字默认使用该颜色

4. value21-style.xml

  1. <style name=“AppTheme” parent=“android:Theme.Material.Light.DarkActionBar”>  
  2.     <!–应用的主要暗色调–>  
  3.     <item name=“android:colorPrimaryDark”>#ff3367d6</item>  
  4.     <!–actionBar默认使用该颜色–>  
  5.     <item name=“android:colorPrimary”>#ff4285f4</item>  
  6.     <!–窗口背景颜色,不能直接引用–>  
  7.     <item name=“android:windowBackground”>@color/windowbg</item>  
  8.     <!–底部栏颜色–>  
  9.     <item name=“android:navigationBarColor”>#ff030303</item>  
  10. </style>  
    <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
        <!--应用的主要暗色调-->
        <item name="android:colorPrimaryDark">#ff3367d6</item>
        <!--actionBar默认使用该颜色-->
        <item name="android:colorPrimary">#ff4285f4</item>
        <!--窗口背景颜色,不能直接引用-->
        <item name="android:windowBackground">@color/windowbg</item>
        <!--底部栏颜色-->
        <item name="android:navigationBarColor">#ff030303</item>
    </style>


 

8.主题切换效果

1.color.xml

  1. <resources>  
  2.     <color name=“windowbg”>#ff303030</color>  
  3.     <!–红色–>  
  4.     <color name=“red_colorPrimaryDark”>#ffb0120a</color>  
  5.     <color name=“red_colorPrimary”>#ffe51c23</color>  
  6.     <color name=“red_windowBackground”>#fff36c60</color>  
  7.     <color name=“red_navigationBarColor”>#ffd01716</color>  
  8.     <!–粉色–>  
  9.     <color name=“pink_colorPrimaryDark”>#ff880e4f</color>  
  10.     <color name=“pink_colorPrimary”>#ffe91e63</color>  
  11.     <color name=“pink_windowBackground”>#fff06292</color>  
  12.     <color name=“pink_navigationBarColor”>#ffc2185b</color>  
  13. </resources>  
<resources>
    <color name="windowbg">#ff303030</color>
    <!--红色-->
    <color name="red_colorPrimaryDark">#ffb0120a</color>
    <color name="red_colorPrimary">#ffe51c23</color>
    <color name="red_windowBackground">#fff36c60</color>
    <color name="red_navigationBarColor">#ffd01716</color>
    <!--粉色-->
    <color name="pink_colorPrimaryDark">#ff880e4f</color>
    <color name="pink_colorPrimary">#ffe91e63</color>
    <color name="pink_windowBackground">#fff06292</color>
    <color name="pink_navigationBarColor">#ffc2185b</color>
</resources>


2.value21–styles.xml

  1. <!–红色主题–>  
  2. <style name=“AppTheme_Red” parent=“android:Theme.Material.Light.DarkActionBar”>  
  3.     <!–应用的主要暗色调–>  
  4.     <item name=“android:colorPrimaryDark”>@color/red_colorPrimaryDark</item>  
  5.     <!–actionBar默认使用该颜色–>  
  6.     <item name=“android:colorPrimary”>@color/red_colorPrimary</item>  
  7.     <!–窗口背景颜色–>  
  8.     <item name=“android:windowBackground”>@color/red_windowBackground</item>  
  9.     <!–底部栏颜色–>  
  10.     <item name=“android:navigationBarColor”>@color/red_navigationBarColor</item>  
  11. </style>  
  12. <!–粉色主题–>  
  13.   
  14. <style name=“AppTheme_Pink” parent=“android:Theme.Material.Light.DarkActionBar”>  
  15.     <!–应用的主要暗色调–>  
  16.     <item name=“android:colorPrimaryDark”>@color/pink_colorPrimaryDark</item>  
  17.     <!–actionBar默认使用该颜色–>  
  18.     <item name=“android:colorPrimary”>@color/pink_colorPrimary</item>  
  19.     <!–窗口背景颜色–>  
  20.     <item name=“android:windowBackground”>@color/pink_windowBackground</item>  
  21.     <!–底部栏颜色–>  
  22.     <item name=“android:navigationBarColor”>@color/pink_navigationBarColor</item>  
  23. </style>  
    <!--红色主题-->
    <style name="AppTheme_Red" parent="android:Theme.Material.Light.DarkActionBar">
        <!--应用的主要暗色调-->
        <item name="android:colorPrimaryDark">@color/red_colorPrimaryDark</item>
        <!--actionBar默认使用该颜色-->
        <item name="android:colorPrimary">@color/red_colorPrimary</item>
        <!--窗口背景颜色-->
        <item name="android:windowBackground">@color/red_windowBackground</item>
        <!--底部栏颜色-->
        <item name="android:navigationBarColor">@color/red_navigationBarColor</item>
    </style>
    <!--粉色主题-->

    <style name="AppTheme_Pink" parent="android:Theme.Material.Light.DarkActionBar">
        <!--应用的主要暗色调-->
        <item name="android:colorPrimaryDark">@color/pink_colorPrimaryDark</item>
        <!--actionBar默认使用该颜色-->
        <item name="android:colorPrimary">@color/pink_colorPrimary</item>
        <!--窗口背景颜色-->
        <item name="android:windowBackground">@color/pink_windowBackground</item>
        <!--底部栏颜色-->
        <item name="android:navigationBarColor">@color/pink_navigationBarColor</item>
    </style>


 

3.主题切换-MainActivity

  1. @Override  
  2.    protected void onCreate(Bundle savedInstanceState) {  
  3.        super.onCreate(savedInstanceState);  
  4.     //获取主题ID,设置主题  
  5.        int themeId = getIntent().getIntExtra(“themeId”, -1);  
  6.        if (themeId != -1) {  
  7.            setTheme(themeId);  
  8.        }  
  9.        setContentView(R.layout.activity_main);  
  10.    }  
  11.    //设置主题必须在setContentView之前使用,所以要重启自己  
  12.    public void red(View v) {  
  13.        //结束自己  
  14.        finish();  
  15.     //不用动画  
  16.        overridePendingTransition(00);//紧挨着finish方法或者对应的startActivity方法就可以  
  17.        //重启自己  
  18.        Intent intent = new Intent(MainActivity.this, MainActivity.class);  
  19.        //告知需要切换的主题  
  20.        intent.putExtra(”themeId”, R.style.AppTheme_Red);  
  21.        startActivity(intent);  
  22.        overridePendingTransition(00);//紧挨着finish方法或者对应的startActivity方法就可以  
  23.    }  
  24.   
  25.    public void pink(View v) {  
  26.        finish();  
  27.        overridePendingTransition(00);//紧挨着finish方法或者对应的startActivity方法就可以  
  28.        //重启自己  
  29.        Intent intent = new Intent(MainActivity.this, MainActivity.class);  
  30.        //告知需要切换的主题  
  31.        intent.putExtra(”themeId”, R.style.AppTheme_Pink);  
  32.        startActivity(intent);  
  33.        overridePendingTransition(00);//紧挨着finish方法或者对应的startActivity方法就可以  
  34.    }  
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //获取主题ID,设置主题
        int themeId = getIntent().getIntExtra("themeId", -1);
        if (themeId != -1) {
            setTheme(themeId);
        }
        setContentView(R.layout.activity_main);
    }
    //设置主题必须在setContentView之前使用,所以要重启自己
    public void red(View v) {
        //结束自己
        finish();
        //不用动画
        overridePendingTransition(0, 0);//紧挨着finish方法或者对应的startActivity方法就可以
        //重启自己
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        //告知需要切换的主题
        intent.putExtra("themeId", R.style.AppTheme_Red);
        startActivity(intent);
        overridePendingTransition(0, 0);//紧挨着finish方法或者对应的startActivity方法就可以
    }

    public void pink(View v) {
        finish();
        overridePendingTransition(0, 0);//紧挨着finish方法或者对应的startActivity方法就可以
        //重启自己
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        //告知需要切换的主题
        intent.putExtra("themeId", R.style.AppTheme_Pink);
        startActivity(intent);
        overridePendingTransition(0, 0);//紧挨着finish方法或者对应的startActivity方法就可以
    }


5.主题不仅可以对Application和Activity使用,也可以对某一个控件单使用,或者是在xml布局中给一个根节点控件设置android:theme属性,来修改它和它所有子控件的主题。

 

6.Material Design主题只有在API级别为21以上才可使用,在v7支持库中提供了部分控件的Material Design主题样式,如果想使应用在android的所有版本上都能统一风格,我们可以对控件效果做自定义或者使用一些第三方的兼容包。目前最有效的做法是针对21版本创建value-21资源目录,使用Material Design风格主题,在其他版本使用v7的Theme.AppCompat.Light风格主题。

 

7.项目中使用Material Design风格

1.  设置应用的 `targetSdkVersion` 和 `targetSdkVersion` 为21
2.  在values目录下的style资源文件中创建一个style,让其继承自 `android:Theme.Material`
3.  在AndroidManifest中指定应用的主题或者Activity的主题为我们设定的样式

 

9.toolBar的使用

1.toolBar

Toolbar是应用的内容的标准工具栏,`可以说是Actionbar的升级版`,两者不是独立关系,要使用Toolbar还是得跟ActionBar扯上关系的。相比Actionbar Toolbar最明显的一点就是变得很`自由,可随处放置`,因为它是作为一个`ViewGroup来定义使用的`,所以单纯使用ActionBar已经稍显过时了,它的一些方法已被标注过时。

 

2.引入v7,v4包,修改build.gradle,兼容2.2

(1)引入v7包,去除actionBar        value

  1. <style name=“AppTheme” parent=“Theme.AppCompat.Light.NoActionBar”>  
  2.        <!– Customize your theme here. –>  
  3.    </style>  
  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>


(2)创建ToolBar的布局       include_toolbar.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <android.support.v7.widget.Toolbar  
  3.     xmlns:android=“http://schemas.android.com/apk/res/android”  
  4.     android:layout_width=“match_parent”  
  5.     android:layout_height=“wrap_content”  
  6.     android:background=“#ff0000”>  
  7.   
  8. </android.support.v7.widget.Toolbar>  
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000">

</android.support.v7.widget.Toolbar>


 

(3)使用toolbar

a.布局中

  1. <include layout=“@layout/include_toolbar”></include>  
<include layout="@layout/include_toolbar"></include>


b.代码

  1. /** 
  2.  * 1.需要继承我们的actionBarActivity 
  3.  */  
  4. public class MainActivity extends ActionBarActivity {  
  5.   
  6.     private Toolbar mToolbar;  
  7.     private DrawerLayout mDrawerLayout;  
  8.     private ActionBarDrawerToggle mToggle;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         initView();  
  15.     }  
  16.     private void initView() {  
  17.         mToolbar = (Toolbar) findViewById(R.id.main_toolbar);  
  18.   
  19.         //Set a Toolbar to act as the ActionBar for this Activity window.  
  20.         setSupportActionBar(mToolbar);  
  21.    
  22.     }  
/**
 * 1.需要继承我们的actionBarActivity
 */
public class MainActivity extends ActionBarActivity {

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.main_toolbar);

        //Set a Toolbar to act as the ActionBar for this Activity window.
        setSupportActionBar(mToolbar);

    }


 

10.drawerlayout和actionbardrawertoggle的集成

1.接上

2.布局

  1. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.               xmlns:tools=“http://schemas.android.com/tools”  
  3.               android:layout_width=“match_parent”  
  4.               android:layout_height=“match_parent”  
  5.               android:orientation=“vertical”  
  6.               tools:context=“.MainActivity”>  
  7.   
  8.     <include  
  9.         android:id=“@+id/main_toolbar”  
  10.         layout=“@layout/include_toolbar”></include>  
  11.   
  12.     <android.support.v4.widget.DrawerLayout  
  13.         android:id=“@+id/main_drawerlayout”  
  14.         android:layout_width=“match_parent”  
  15.         android:layout_height=“match_parent”  
  16.   
  17.         >  
  18.         <!–menu–>  
  19.         <FrameLayout  
  20.             android:layout_width=“180dp”  
  21.             android:layout_height=“match_parent”  
  22.             android:layout_gravity=“left”  
  23.             android:background=“#00ff00”></FrameLayout>  
  24.   
  25.         <!–content–>  
  26.         <FrameLayout  
  27.             android:layout_width=“match_parent”  
  28.             android:layout_height=“match_parent”  
  29.             android:background=“#ff7dffd6”></FrameLayout>  
  30.   
  31.     </android.support.v4.widget.DrawerLayout>  
  32.   
  33. </LinearLayout>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context=".MainActivity">

    <include
        android:id="@+id/main_toolbar"
        layout="@layout/include_toolbar"></include>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/main_drawerlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        >
        <!--menu-->
        <FrameLayout
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#00ff00"></FrameLayout>

        <!--content-->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff7dffd6"></FrameLayout>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>


3.MainActivity

  1. /** 
  2.  * 1.需要继承我们的actionBarActivity 
  3.  */  
  4. public class MainActivity extends ActionBarActivity {  
  5.   
  6.     private Toolbar mToolbar;  
  7.     private DrawerLayout mDrawerLayout;  
  8.     private ActionBarDrawerToggle mToggle;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         initView();  
  15.         initDrawerLayout();  
  16.     }  
  17.   
  18.     private void initDrawerLayout() {  
  19.         mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R  
  20.                 .string.open, R.string.close);  
  21.   
  22.         //同步状态  
  23.         mToggle.syncState();  
  24.   
  25.         //设置监听  
  26.         mDrawerLayout.setDrawerListener(mToggle);  
  27.     }  
  28.   
  29.     private void initView() {  
  30.         mToolbar = (Toolbar) findViewById(R.id.main_toolbar);  
  31.   
  32.         //Set a Toolbar to act as the ActionBar for this Activity window.  
  33.         setSupportActionBar(mToolbar);  
  34.   
  35.         mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawerlayout);  
  36.     }  
/**
 * 1.需要继承我们的actionBarActivity
 */
public class MainActivity extends ActionBarActivity {

    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initDrawerLayout();
    }

    private void initDrawerLayout() {
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R
                .string.open, R.string.close);

        //同步状态
        mToggle.syncState();

        //设置监听
        mDrawerLayout.setDrawerListener(mToggle);
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.main_toolbar);

        //Set a Toolbar to act as the ActionBar for this Activity window.
        setSupportActionBar(mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawerlayout);
    }


 

 

11.阴影和裁剪效果

1.布局

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.                 xmlns:tools=“http://schemas.android.com/tools”  
  3.                 android:layout_width=“match_parent”  
  4.                 android:layout_height=“match_parent”  
  5.                 android:background=“#ffffff”  
  6.                 android:paddingBottom=“@dimen/activity_vertical_margin”  
  7.                 android:paddingLeft=“@dimen/activity_horizontal_margin”  
  8.                 android:paddingRight=“@dimen/activity_horizontal_margin”  
  9.                 android:paddingTop=“@dimen/activity_vertical_margin”  
  10.                 tools:context=“.MainActivity”>  
  11.   
  12.     <!–红色–>  
  13.     <View  
  14.         android:layout_width=“100dp”  
  15.         android:layout_height=“100dp”  
  16.         android:layout_margin=“10dp”  
  17.         android:background=“#ff0000”  
  18.         android:elevation=“11dp”/>  
  19.     <!–绿色–>  
  20.     <View  
  21.         android:layout_width=“100dp”  
  22.         android:layout_height=“100dp”  
  23.         android:layout_marginLeft=“60dp”  
  24.         android:layout_marginTop=“60dp”  
  25.         android:background=“#00ff00”  
  26.         android:elevation=“10dp”/>  
  27.     <!–圆形的shape–>  
  28.     <View  
  29.         android:layout_width=“100dp”  
  30.         android:layout_height=“100dp”  
  31.         android:layout_marginLeft=“180dp”  
  32.         android:layout_marginTop=“180dp”  
  33.         android:background=“@drawable/shape_circle”  
  34.         android:elevation=“10dp”/>  
  35.     <!–圆形的图片–>  
  36.     <View  
  37.   
  38.         android:id=“@+id/ivHead”  
  39.         android:layout_width=“100dp”  
  40.         android:layout_height=“100dp”  
  41.         android:layout_marginLeft=“180dp”  
  42.         android:layout_marginTop=“300dp”  
  43.         android:background=“@mipmap/head”  
  44.         android:elevation=“10dp”/>  
  45. </RelativeLayout>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#ffffff"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <!--红色-->
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_margin="10dp"
        android:background="#ff0000"
        android:elevation="11dp"/>
    <!--绿色-->
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="60dp"
        android:background="#00ff00"
        android:elevation="10dp"/>
    <!--圆形的shape-->
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="180dp"
        android:layout_marginTop="180dp"
        android:background="@drawable/shape_circle"
        android:elevation="10dp"/>
    <!--圆形的图片-->
    <View

        android:id="@+id/ivHead"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="180dp"
        android:layout_marginTop="300dp"
        android:background="@mipmap/head"
        android:elevation="10dp"/>
</RelativeLayout>


 

2.在xml布局中,可以通过android:outlineProvider来指定轮廓的判定方式:

(1)none 即使设置了Z属性,也不会显示阴影

(2)background 会按照背景来设置阴影形状

(3)bounds 会按照View的大小来描绘阴影

(4)paddedBounds 和bounds类似,不过阴影会稍微向右偏移一点

3. 在代码中,我们可以通过setOutlineProvider来指定一个View的轮廓:

  1. ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {  
  2.     public void getOutline(View view, Outline outline) {  
  3.         // 可以指定圆形,矩形,圆角矩形,path  
  4.         outline.setOval(0, 0, view.getWidth(), view.getHeight());  
  5.     }  
  6. };  
  7. View.setOutlineProvider(viewOutlineProvider );  
ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
    public void getOutline(View view, Outline outline) {
        // 可以指定圆形,矩形,圆角矩形,path
        outline.setOval(0, 0, view.getWidth(), view.getHeight());
    }
};
View.setOutlineProvider(viewOutlineProvider );


4.注意:如果采用图片作为背景,即使在xml布局中指定android:outlineProvider为background也不会显示阴影,只有通过代码中指定轮廓来显示。

5.MainActivity

  1. private View mIvHead;  
  2.   
  3. @Override  
  4. protected void onCreate(Bundle savedInstanceState) {  
  5.     super.onCreate(savedInstanceState);  
  6.     setContentView(R.layout.activity_main);  
  7.     mIvHead = (View) findViewById(R.id.ivHead);  
  8.     //设置轮廓  
  9.     mIvHead.setOutlineProvider(new ViewOutlineProvider() {  
  10.         @Override  
  11.         public void getOutline(View view, Outline outline) {  
  12.             //设置view对应的轮廓  
  13.             outline.setOval(00, view.getWidth() - 20, view.getHeight() - 20);  
  14.         }  
  15.     });  
  16.     mIvHead.setClipToOutline(true);  
  17. }  
    private View mIvHead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIvHead = (View) findViewById(R.id.ivHead);
        //设置轮廓
        mIvHead.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                //设置view对应的轮廓
                outline.setOval(0, 0, view.getWidth() - 20, view.getHeight() - 20);
            }
        });
        mIvHead.setClipToOutline(true);
    }


 

12.selector定义新方式

1.tint属性

tint属性一个颜色值,可以对图片做颜色渲染,我们可以给view的背景设置tint色值,给ImageView的图片设置tint色值,也可以给任意Drawable或者NinePatchDrawable设置tint色值。

在应用的主题中也可以通过设置 android:tint来给主题设置统一的颜色渲染。

tint的渲染模式有总共有16种,xml文件中可以使用6种,代码中我们可以设置16种,渲染模式决定了渲染颜色和原图颜色的取舍和合成规则:———–两种图片合成的效果

  1. PorterDuff.Mode.CLEAR 所绘制不会提交到画布上。
  2. PorterDuff.Mode.SRC 显示上层绘制图片
  3. PorterDuff.Mode.DST 显示下层绘制图片
  4. PorterDuff.Mode.SRC_OVER 正常绘制显示,上下层绘制叠盖。
  5. PorterDuff.Mode.DST_OVER 上下层都显示。下层居上显示。
  6. PorterDuff.Mode.SRC_IN 取两层绘制交集。显示上层。
  7. PorterDuff.Mode.DST_IN 取两层绘制交集。显示下层。
  8. PorterDuff.Mode.SRC_OUT 取上层绘制非交集部分。
  9. PorterDuff.Mode.DST_OUT 取下层绘制非交集部分。
  10. PorterDuff.Mode.SRC_ATOP 取下层非交集部分与上层交集部分
  11. PorterDuff.Mode.DST_ATOP 取上层非交集部分与下层交集部分
  12. PorterDuff.Mode.XOR 取两层绘制非交集。两层绘制非交集。
  13. PorterDuff.Mode.DARKEN 上下层都显示。变暗
  14. PorterDuff.Mode.LIGHTEN 上下层都显示。变亮
  15. PorterDuff.Mode.MULTIPLY 取两层绘制交集
  16. PorterDuff.Mode.SCREEN 上下层都显示。

2.ic_launcher_f.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <bitmap xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.         android:src=“@mipmap/ic_launcher”  
  4.         android:tint=“#ff58ffd6”  
  5.         android:tintMode=“multiply”  
  6.     >  
  7.     <!–android:tint=”#ff58ffd6” 合成颜色–>  
  8.     <!–android:tintMode=”multiply” 合成规则–>  
  9. </bitmap>  
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@mipmap/ic_launcher"
        android:tint="#ff58ffd6"
        android:tintMode="multiply"
    >
    <!--android:tint="#ff58ffd6" 合成颜色-->
    <!--android:tintMode="multiply" 合成规则-->
</bitmap>


3.selector_bg.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <selector xmlns:android=“http://schemas.android.com/apk/res/android”>  
  3.     <item android:drawable=“@drawable/ic_launcher_f” android:state_pressed=“true”></item>  
  4.     <item android:drawable=“@mipmap/ic_launcher”></item>  
  5. </selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_launcher_f" android:state_pressed="true"></item>
    <item android:drawable="@mipmap/ic_launcher"></item>
</selector>


4.activity_main.xml

  1. <Button  
  2.     android:layout_width=“wrap_content”  
  3.     android:layout_height=“wrap_content”  
  4.     android:background=“@drawable/selector_bg”  
  5.     android:onClick=“click”  
  6.     android:text=“@string/hello_world”  
  7.     />  
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/selector_bg"
        android:onClick="click"
        android:text="@string/hello_world"
        />


 

 13.Palette调色版功能

 1.Android Material Design之Toolbar与Palette实践:http://blog.csdn.net/jdsjlzx/article/details/41441083

 2.需要导入相关的包:v7和

3.

  1. private void initView() {  
  2.     mIv_muted = findViewById(R.id.iv_Muted);  
  3.     mIv_muted_light = findViewById(R.id.iv_Muted_light);  
  4.     mIv_vibrant = findViewById(R.id.iv_Vibrant);  
  5. }  
  6.   
  7. public void click(View v) {  
  8.     Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.h17);  
  9.     //        如果操作本来就属于后台线程,可以使用:  
  10.     //        Palette p = Palette.generate(bitmap);  
  11.     //        如果在主线程中,我们可以使用异步的方式:  
  12.     Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {  
  13.         public void onGenerated(Palette palette) {  
  14.             mIv_muted.setBackgroundColor(palette.getMutedColor(Color.BLACK));  
  15.             mIv_muted_light.setBackgroundColor(palette.getLightMutedColor(Color.BLACK));  
  16.             mIv_vibrant.setBackgroundColor(palette.getVibrantColor(Color.BLACK));  
  17.         }  
  18.     });  
  19. }  
    private void initView() {
        mIv_muted = findViewById(R.id.iv_Muted);
        mIv_muted_light = findViewById(R.id.iv_Muted_light);
        mIv_vibrant = findViewById(R.id.iv_Vibrant);
    }

    public void click(View v) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.h17);
        //        如果操作本来就属于后台线程,可以使用:
        //        Palette p = Palette.generate(bitmap);
        //        如果在主线程中,我们可以使用异步的方式:
        Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
            public void onGenerated(Palette palette) {
                mIv_muted.setBackgroundColor(palette.getMutedColor(Color.BLACK));
                mIv_muted_light.setBackgroundColor(palette.getLightMutedColor(Color.BLACK));
                mIv_vibrant.setBackgroundColor(palette.getVibrantColor(Color.BLACK));
            }
        });
    }


 

4.详情见图片和颜色.html

5.

  1. Vibrant 鲜艳的
  2. Vibrant dark鲜艳的暗色
  3. Vibrant light鲜艳的亮色
  4. Muted 柔和的
  5. Muted dark柔和的暗色
  6. Muted light柔和的亮色

6.对图片取色是一个比较消耗性能的操作,其内部会对图片的像素值进来遍历以分析对比,所以我们要在异步线程中去完成。

7.

p.getVibrantColor(int defaultColor);
p.getDarkVibrantColor(int defaultColor);
p.getLightVibrantColor(int defaultColor);
p.getMutedColor(int defaultColor);
p.getDarkMutedColor(int defaultColor);
p.getLightMutedColor(int defaultColor);

 

 

14.按钮的水波纹反馈

1.见全新的动画.html

(1)触摸反馈

(2)圆形展示

(3)曲线运动

(4)视图状态变化

(5)矢量图动画

(6)转场动画

(7)普通转场动画

(8)共享转场动画

(9)组合转场动画

 

2.布局

  1. <Button  
  2.            android:layout_width=“150dp”  
  3.            android:layout_height=“150dp”  
  4.            android:text=“默认效果”/>  
  5.   
  6.        <Button  
  7.            android:layout_width=“150dp”  
  8.            android:layout_height=“150dp”  
  9.            android:layout_marginTop=“20dp”  
  10.            android:background=“?android:selectableItemBackground”  
  11.            android:text=“selectableItemBackground效果”/>  
  12.   
  13.   
  14.        <Button  
  15.            android:layout_width=“150dp”  
  16.            android:layout_height=“150dp”  
  17.            android:layout_marginTop=“20dp”  
  18.            android:background=“?android:selectableItemBackgroundBorderless”  
  19.            android:text=“selectableItemBackgroundBorderless”/>  
  20.   
  21.        <Button  
  22.            android:id=“@+id/btn_cutstom_animation”  
  23.            android:layout_width=“150dp”  
  24.            android:layout_height=“150dp”  
  25.            android:layout_marginTop=“20dp”  
  26.            android:background=“#ff5cffbf”  
  27.            android:text=“selectableItemBackgroundBorderless”/>  
 <Button
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:text="默认效果"/>

        <Button
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="20dp"
            android:background="?android:selectableItemBackground"
            android:text="selectableItemBackground效果"/>


        <Button
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="20dp"
            android:background="?android:selectableItemBackgroundBorderless"
            android:text="selectableItemBackgroundBorderless"/>

        <Button
            android:id="@+id/btn_cutstom_animation"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_marginTop="20dp"
            android:background="#ff5cffbf"
            android:text="selectableItemBackgroundBorderless"/>


3.

  1. mCustomAnimation = (Button) findViewById(R.id.btn_cutstom_animation);  
  2.        mCustomAnimation.setOnClickListener(new View.OnClickListener() {  
  3.            @Override  
  4.            public void onClick(View v) {  
  5.                 /* 
  6.                    view 操作的视图 
  7.                    centerX 动画开始的中心点X 
  8.                    centerY 动画开始的中心点Y 
  9.                    startRadius 动画开始半径 
  10.                    startRadius 动画结束半径 
  11.                 */  
  12.                int centerX = mCustomAnimation.getWidth() / 2;  
  13.                int centerY = mCustomAnimation.getHeight() / 2;  
  14.                float startRadius = 0;  
  15.                float endRadius = mCustomAnimation.getWidth();  
  16.   
  17.                Animator animator = ViewAnimationUtils.createCircularReveal(mCustomAnimation,  
  18.                        centerX,  
  19.                        centerY, startRadius, endRadius);  
  20.                animator.setDuration(3000);  
  21.                animator.start();  
  22.            }  
  23.        });  
 mCustomAnimation = (Button) findViewById(R.id.btn_cutstom_animation);
        mCustomAnimation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 /*
                    view 操作的视图
                    centerX 动画开始的中心点X
                    centerY 动画开始的中心点Y
                    startRadius 动画开始半径
                    startRadius 动画结束半径
                 */
                int centerX = mCustomAnimation.getWidth() / 2;
                int centerY = mCustomAnimation.getHeight() / 2;
                float startRadius = 0;
                float endRadius = mCustomAnimation.getWidth();

                Animator animator = ViewAnimationUtils.createCircularReveal(mCustomAnimation,
                        centerX,
                        centerY, startRadius, endRadius);
                animator.setDuration(3000);
                animator.start();
            }
        });


 

15.矢量图展示,矢量图动画

1.详情见全新的动画.html

2.矢量图展示

(1)布局

  1. <View  
  2.            android:layout_width=“100dp”  
  3.            android:layout_height=“100dp”  
  4.            android:background=“@drawable/vector_xin”  
  5.            ></View>  
 <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/vector_xin"
            ></View>


(2)vector_xin.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <vector xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.         android:width=“600px”  
  4.         android:height=“600px”  
  5.         android:viewportHeight=“600”  
  6.         android:viewportWidth=“600”  
  7.   
  8.     >  
  9.     <path  
  10.         android:fillColor=“#ff0000”  
  11.         android:pathData=“m272.36969,90.10716c14.5986,0 26.44617,-10.93784 26.44617,-24.43066c0,-13.50066 -11.84756,-24.44545 -26.44617,-24.44545c-14.61017,0 -26.46236,10.94479 -26.46236,24.44545c0,13.49283 11.84758,24.43066 26.46236,24.43066zm74.16827,105.36101l-23.73206,-81.31308c-0.22348,-0.73847 -0.53455,-1.44301 -0.9101,-2.09345c-2.69809,-7.61726 -10.43979,-13.13331 -19.58011,-13.13331l-60.06725,0c-9.53197,0 -17.53633,5.98904 -19.8911,14.11715c-0.16129,0.35566 -0.29031,0.73074 -0.41013,1.11668l-23.31035,81.30994c-1.55295,5.33005 1.87091,10.80814 7.65184,12.24348c5.77171,1.42679 11.6978,-1.73773 13.25305,-7.06851l18.30589,-63.87151l7.62418,0l-33.22247,115.56409l31.3055,0l0,79.7836c0,6.13647 5.38002,11.10779 12.0296,11.10779c6.63574,0 12.02499,-4.9675 12.02499,-11.10779l0,-79.7836l9.56192,0l0,79.7836c0,6.13647 5.38693,11.10779 12.03876,11.10779c6.63342,0 12.02264,-4.9675 12.02264,-11.10779l0,-79.7836l31.29181,0l-33.34705,-115.56409l7.82477,0l18.64224,63.87151c1.55295,5.33078 7.4837,8.4953 13.25308,7.06851c5.75784,-1.43535 9.18631,-6.91733 7.64032,-12.24742l0,0z”></path>  
  12. </vector>  
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="600px"
        android:height="600px"
        android:viewportHeight="600"
        android:viewportWidth="600"

    >
    <path
        android:fillColor="#ff0000"
        android:pathData="m272.36969,90.10716c14.5986,0 26.44617,-10.93784 26.44617,-24.43066c0,-13.50066 -11.84756,-24.44545 -26.44617,-24.44545c-14.61017,0 -26.46236,10.94479 -26.46236,24.44545c0,13.49283 11.84758,24.43066 26.46236,24.43066zm74.16827,105.36101l-23.73206,-81.31308c-0.22348,-0.73847 -0.53455,-1.44301 -0.9101,-2.09345c-2.69809,-7.61726 -10.43979,-13.13331 -19.58011,-13.13331l-60.06725,0c-9.53197,0 -17.53633,5.98904 -19.8911,14.11715c-0.16129,0.35566 -0.29031,0.73074 -0.41013,1.11668l-23.31035,81.30994c-1.55295,5.33005 1.87091,10.80814 7.65184,12.24348c5.77171,1.42679 11.6978,-1.73773 13.25305,-7.06851l18.30589,-63.87151l7.62418,0l-33.22247,115.56409l31.3055,0l0,79.7836c0,6.13647 5.38002,11.10779 12.0296,11.10779c6.63574,0 12.02499,-4.9675 12.02499,-11.10779l0,-79.7836l9.56192,0l0,79.7836c0,6.13647 5.38693,11.10779 12.03876,11.10779c6.63342,0 12.02264,-4.9675 12.02264,-11.10779l0,-79.7836l31.29181,0l-33.34705,-115.56409l7.82477,0l18.64224,63.87151c1.55295,5.33078 7.4837,8.4953 13.25308,7.06851c5.75784,-1.43535 9.18631,-6.91733 7.64032,-12.24742l0,0z"></path>
</vector>


(3)pathData  矢量图制作

http://editor.method.ac

 

3.矢量动画

(1)例子:http://blog.csdn.net/ljx19900116/article/details/41806875

(2)详情见 全新的动画.html

(3)

   <vector>元素的矢量资源,在res/drawable/(文件夹)
   <animated-vector>元素的矢量资源动画,在res/drawable/(文件夹)
   < objectAnimator>元素的一个或多个对象动画器,在res/anim/(文件夹)
 

(4)布局

  1. <!–矢量图动画–>  
  2. <View  
  3.     android:id=“@+id/triangle”  
  4.     android:layout_width=“50dp”  
  5.     android:layout_height=“50dp”  
  6.     android:background=“@drawable/animated_vector_triangle”  
  7.     ></View>  
        <!--矢量图动画-->
        <View
            android:id="@+id/triangle"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/animated_vector_triangle"
            ></View>


(5)drawable—-vector_triangle.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <vector xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.         android:width=“64dp”  
  4.         android:height=“64dp”  
  5.         android:viewportHeight=“600”  
  6.         android:viewportWidth=“600”  
  7.     >  
  8.     <path  
  9.         android:name=“v”  
  10.         android:fillColor=“#000000”  
  11.         android:pathData=“M300,70 l 0,-70 70,70 0,0 -70,70z”/>  
  12.   
  13. </vector>  
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="64dp"
        android:height="64dp"
        android:viewportHeight="600"
        android:viewportWidth="600"
    >
    <path
        android:name="v"
        android:fillColor="#000000"
        android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z"/>

</vector>


(6)drawable—animated_vector_triangle.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <animated-vector  
  3.     xmlns:android=“http://schemas.android.com/apk/res/android”  
  4.     android:drawable=“@drawable/vector_triangle”  
  5.     >  
  6.     <target  
  7.         android:name=“v”  
  8.         android:animation=“@anim/path_morph”/>  
  9.     <!–<target–>  
  10.     <!–android:name=”v”  指定名字.这个是后面需要–>  
  11.     <!–android:animation=”@anim/path_morph”  指定一个动画/>–>  
  12.   
  13. </animated-vector>  
<?xml version="1.0" encoding="utf-8"?>
<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/vector_triangle"
    >
    <target
        android:name="v"
        android:animation="@anim/path_morph"/>
    <!--<target-->
    <!--android:name="v"  指定名字.这个是后面需要-->
    <!--android:animation="@anim/path_morph"  指定一个动画/>-->

</animated-vector>


(7)res/anim/path_morph.xml

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <objectAnimator  
  3.     xmlns:android=“http://schemas.android.com/apk/res/android”  
  4.     android:duration=“5000”  
  5.     android:propertyName=“pathData”  
  6.     android:valueFrom=“M300,70 l 0,-70 70,70 0,0 -70,70z”  
  7.     android:valueTo=“M300,70 l 0,-70 70,0 0,140 -70,0 z”  
  8.     android:valueType=“pathType”>  
  9. </objectAnimator>  
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:propertyName="pathData"
    android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z"
    android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z"
    android:valueType="pathType">
</objectAnimator>


(8)

  1. View triangle = findViewById(R.id.triangle);  
  2. Drawable drawable = triangle.getBackground();  
  3.   
  4. if (drawable instanceof Animatable) {  
  5.     ((Animatable) drawable).start();  
  6. }  
        View triangle = findViewById(R.id.triangle);
        Drawable drawable = triangle.getBackground();

        if (drawable instanceof Animatable) {
            ((Animatable) drawable).start();
        }


 

 

16.recycleView_列表和网格

1.单独的jar包:com.android.support:recyclerview-v7:22.2.1

2.

3.RecycleView———————详情见例子源码

(1)主布局

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.                 xmlns:tools=“http://schemas.android.com/tools”  
  3.                 android:layout_width=“match_parent”  
  4.                 android:layout_height=“match_parent”  
  5.                 tools:context=“.MainActivity”>  
  6.         <android.support.v7.widget.RecyclerView  
  7.             android:id=“@+id/recyclerView”  
  8.             android:layout_width=“match_parent”  
  9.             android:layout_height=“match_parent”  
  10.             />  
  11.     </RelativeLayout>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">
        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </RelativeLayout>


(2)ListAdapter

  1. public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyHolder> {  
  2.     private Context context;  
  3.     private List<DataBean> datas;  
  4.   
  5.     public ListAdapter(Context context, List<DataBean> datas) {  
  6.         this.context = context;  
  7.         this.datas = datas;  
  8.     }  
  9.   
  10.     @Override  
  11.     public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {//决定根布局  
  12.         //        TextView tv = new TextView(context);//根布局  
  13.         View itemView = View.inflate(context, R.layout.item_list, null);  
  14.         return new MyHolder(itemView);  
  15.     }  
  16.   
  17.     @Override  
  18.     public void onBindViewHolder(MyHolder myHolder, int position) {//填充数据  
  19.         myHolder.setDataAndRefreshUI(datas.get(position));  
  20.     }  
  21.   
  22.     @Override  
  23.     public int getItemCount() {//条目总数  
  24.         if (datas != null) {  
  25.             return datas.size();  
  26.         }  
  27.         return 0;  
  28.     }  
  29.   
  30.     public class MyHolder extends RecyclerView.ViewHolder {  
  31.         //孩子对象  
  32.         private TextView mTvName;  
  33.         private ImageView mIvIcon;  
  34.   
  35.         public MyHolder(View itemView) {  
  36.             super(itemView);  
  37.             //初始化孩子对象  
  38.             mTvName = (TextView) itemView.findViewById(R.id.tv_name);  
  39.             mIvIcon = (ImageView) itemView.findViewById(R.id.iv_icon);  
  40.         }  
  41.   
  42.         /** 
  43.          * 设置itemView的数据展示 
  44.          * 
  45.          * @param dataBean 
  46.          */  
  47.         public void setDataAndRefreshUI(DataBean dataBean) {  
  48.             mTvName.setText(dataBean.text);  
  49.             mIvIcon.setImageResource(dataBean.iconId);  
  50.         }  
  51.     }  
  52. }  
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyHolder> {
    private Context context;
    private List<DataBean> datas;

    public ListAdapter(Context context, List<DataBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {//决定根布局
        //        TextView tv = new TextView(context);//根布局
        View itemView = View.inflate(context, R.layout.item_list, null);
        return new MyHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyHolder myHolder, int position) {//填充数据
        myHolder.setDataAndRefreshUI(datas.get(position));
    }

    @Override
    public int getItemCount() {//条目总数
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        //孩子对象
        private TextView mTvName;
        private ImageView mIvIcon;

        public MyHolder(View itemView) {
            super(itemView);
            //初始化孩子对象
            mTvName = (TextView) itemView.findViewById(R.id.tv_name);
            mIvIcon = (ImageView) itemView.findViewById(R.id.iv_icon);
        }

        /**
         * 设置itemView的数据展示
         *
         * @param dataBean
         */
        public void setDataAndRefreshUI(DataBean dataBean) {
            mTvName.setText(dataBean.text);
            mIvIcon.setImageResource(dataBean.iconId);
        }
    }
}


(3)DataBean

  1. public class DataBean {  
  2.     public String text;  
  3.     public int iconId;  
  4. }  
public class DataBean {
    public String text;
    public int iconId;
}


(4)MainActivity

  1. public class MainActivity extends Activity {  
  2.   
  3.     private RecyclerView mRecyclerView;  
  4.     private List<DataBean> mDatas = new ArrayList<DataBean>();  
  5.     private List<DataBean> mStraggerDatas = new ArrayList<DataBean>();  
  6.   
  7.   
  8.     private int[] mListIcons = new int[]{R.mipmap.g1, R.mipmap.g2, R.mipmap.g3, R.mipmap.g4,  
  9.             R.mipmap.g5, R.mipmap.g6, R.mipmap.g7, R.mipmap.g8, R.mipmap.g9, R.mipmap.g10, R  
  10.             .mipmap.g11, R.mipmap.g12, R.mipmap.g13, R.mipmap.g14, R.mipmap.g15, R.mipmap  
  11.             .g16, R.mipmap.g17, R.mipmap.g18, R.mipmap.g19, R.mipmap.g20, R.mipmap.g21, R  
  12.             .mipmap.g22, R.mipmap.g23, R.mipmap.g24, R.mipmap.g25, R.mipmap.g26, R.mipmap  
  13.             .g27, R.mipmap.g28, R.mipmap.g29};  
  14.   
  15.     private int[] mStraggeredIcons = new int[]{R.mipmap.p1, R.mipmap.p2, R.mipmap.p3, R  
  16.             .mipmap.p4, R.mipmap.p5, R.mipmap.p6, R.mipmap.p7, R.mipmap.p8, R.mipmap.p9, R  
  17.             .mipmap.p10, R.mipmap.p11, R.mipmap.p12, R.mipmap.p13, R.mipmap.p14, R.mipmap  
  18.             .p15, R.mipmap.p16, R.mipmap.p17, R.mipmap.p18, R.mipmap.p19, R.mipmap.p20, R  
  19.             .mipmap.p21, R.mipmap.p22, R.mipmap.p23, R.mipmap.p24, R.mipmap.p25, R.mipmap  
  20.             .p26, R.mipmap.p27, R.mipmap.p28, R.mipmap.p29, R.mipmap.p30, R.mipmap.p31, R  
  21.             .mipmap.p32, R.mipmap.p33, R.mipmap.p34, R.mipmap.p35, R.mipmap.p36, R.mipmap  
  22.             .p37, R.mipmap.p38, R.mipmap.p39, R.mipmap.p40, R.mipmap.p41, R.mipmap.p42, R  
  23.             .mipmap.p43, R.mipmap.p44};  
  24.     private SwipeRefreshLayout mSwipeRefreshLayout;  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.   
  31.   
  32.         //找到recyclerView  
  33.         mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);  
  34.   
  35.         mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);  
  36.   
  37.   
  38.         initData();  
  39.         initListAdapterV();  
  40.     }  
  41.   
  42.     private void initListAdapterV() {  
  43.         //设置layoutManager  
  44.         LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this);  
  45.         mRecyclerView.setLayoutManager(layoutManger);  
  46.   
  47.         //设置adapter  
  48.         ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  49.         mRecyclerView.setAdapter(adapter);  
  50.     }  
  51.     private void initData() {  
  52.         for (int i = 0; i < mListIcons.length; i++) {  
  53.             int iconId = mListIcons[i];  
  54.             DataBean dataBean = new DataBean();  
  55.             dataBean.iconId = iconId;  
  56.             dataBean.text = ”我是item” + i;  
  57.             mDatas.add(dataBean);  
  58.         }  
  59.    
  60.     }  
  61.   
  62.     @Override  
  63.     public boolean onCreateOptionsMenu(Menu menu) {  
  64.         // Inflate the menu; this adds items to the action bar if it is present.    
  65.         return true;  
  66.     }  
  67.   
  68.     @Override  
  69.     public boolean onOptionsItemSelected(MenuItem item) {  
  70.         return super.onOptionsItemSelected(item);  
  71.     }  
  72. }  
public class MainActivity extends Activity {

    private RecyclerView mRecyclerView;
    private List<DataBean> mDatas = new ArrayList<DataBean>();
    private List<DataBean> mStraggerDatas = new ArrayList<DataBean>();


    private int[] mListIcons = new int[]{R.mipmap.g1, R.mipmap.g2, R.mipmap.g3, R.mipmap.g4,
            R.mipmap.g5, R.mipmap.g6, R.mipmap.g7, R.mipmap.g8, R.mipmap.g9, R.mipmap.g10, R
            .mipmap.g11, R.mipmap.g12, R.mipmap.g13, R.mipmap.g14, R.mipmap.g15, R.mipmap
            .g16, R.mipmap.g17, R.mipmap.g18, R.mipmap.g19, R.mipmap.g20, R.mipmap.g21, R
            .mipmap.g22, R.mipmap.g23, R.mipmap.g24, R.mipmap.g25, R.mipmap.g26, R.mipmap
            .g27, R.mipmap.g28, R.mipmap.g29};

    private int[] mStraggeredIcons = new int[]{R.mipmap.p1, R.mipmap.p2, R.mipmap.p3, R
            .mipmap.p4, R.mipmap.p5, R.mipmap.p6, R.mipmap.p7, R.mipmap.p8, R.mipmap.p9, R
            .mipmap.p10, R.mipmap.p11, R.mipmap.p12, R.mipmap.p13, R.mipmap.p14, R.mipmap
            .p15, R.mipmap.p16, R.mipmap.p17, R.mipmap.p18, R.mipmap.p19, R.mipmap.p20, R
            .mipmap.p21, R.mipmap.p22, R.mipmap.p23, R.mipmap.p24, R.mipmap.p25, R.mipmap
            .p26, R.mipmap.p27, R.mipmap.p28, R.mipmap.p29, R.mipmap.p30, R.mipmap.p31, R
            .mipmap.p32, R.mipmap.p33, R.mipmap.p34, R.mipmap.p35, R.mipmap.p36, R.mipmap
            .p37, R.mipmap.p38, R.mipmap.p39, R.mipmap.p40, R.mipmap.p41, R.mipmap.p42, R
            .mipmap.p43, R.mipmap.p44};
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //找到recyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);


        initData();
        initListAdapterV();
    }

    private void initListAdapterV() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }
    private void initData() {
        for (int i = 0; i < mListIcons.length; i++) {
            int iconId = mListIcons[i];
            DataBean dataBean = new DataBean();
            dataBean.iconId = iconId;
            dataBean.text = "我是item" + i;
            mDatas.add(dataBean);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.  
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
}


(5)item_list.xml

  1. <RelativeLayout  
  2.            android:layout_width=“match_parent”  
  3.            android:layout_height=“wrap_content”  
  4.            android:paddingBottom=“8dp”>  
  5.   
  6.            <ImageView  
  7.                android:id=“@+id/iv_icon”  
  8.                android:layout_width=“wrap_content”  
  9.                android:layout_height=“wrap_content”  
  10.                android:layout_alignParentLeft=“true”  
  11.                android:layout_alignParentTop=“true”  
  12.                android:layout_centerVertical=“true”  
  13.                android:layout_margin=“10dp”  
  14.                android:src=“@mipmap/ic_launcher”/>  
  15.   
  16.            <TextView  
  17.                android:id=“@+id/tv_name”  
  18.                android:layout_width=“match_parent”  
  19.                android:layout_height=“wrap_content”  
  20.                android:layout_centerVertical=“true”  
  21.                android:layout_toRightOf=“@+id/iv_icon”  
  22.                android:text=“Name”/>  
  23.   
  24.        </RelativeLayout>  
 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="8dp">

            <ImageView
                android:id="@+id/iv_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher"/>

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/iv_icon"
                android:text="Name"/>

        </RelativeLayout>


 

 

4.在上基础上点击右上角菜单栏切换各种显示状态

(1)meau_main.xml

  1. <menu xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.       xmlns:app=“http://schemas.android.com/apk/res-auto”  
  3.       xmlns:tools=“http://schemas.android.com/tools”  
  4.       tools:context=“.MainActivity”>  
  5.     <item  
  6.         android:id=“@+id/action_list_v”  
  7.         android:orderInCategory=“100”  
  8.         android:title=“纵向的列表”  
  9.         app:showAsAction=“never”/>  
  10.     <item  
  11.         android:id=“@+id/action_list_h”  
  12.         android:orderInCategory=“100”  
  13.         android:title=“横向的列表”  
  14.         app:showAsAction=“never”/>  
  15.     <item  
  16.         android:id=“@+id/action_grid_v”  
  17.         android:orderInCategory=“100”  
  18.         android:title=“纵向的网格”  
  19.         app:showAsAction=“never”/>  
  20.     <item  
  21.         android:id=“@+id/action_grid_h”  
  22.         android:orderInCategory=“100”  
  23.         android:title=“横向的网格”  
  24.         app:showAsAction=“never”/>  
  25.     <item  
  26.         android:id=“@+id/action_stragger_v”  
  27.         android:orderInCategory=“100”  
  28.         android:title=“纵向的瀑布流”  
  29.         app:showAsAction=“never”/>  
  30.     <item  
  31.         android:id=“@+id/action_stragger_h”  
  32.         android:orderInCategory=“100”  
  33.         android:title=“横向的瀑布流”  
  34.         app:showAsAction=“never”/>  
  35. </menu>  
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".MainActivity">
    <item
        android:id="@+id/action_list_v"
        android:orderInCategory="100"
        android:title="纵向的列表"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_list_h"
        android:orderInCategory="100"
        android:title="横向的列表"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_grid_v"
        android:orderInCategory="100"
        android:title="纵向的网格"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_grid_h"
        android:orderInCategory="100"
        android:title="横向的网格"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_stragger_v"
        android:orderInCategory="100"
        android:title="纵向的瀑布流"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_stragger_h"
        android:orderInCategory="100"
        android:title="横向的瀑布流"
        app:showAsAction="never"/>
</menu>


(2)菜单点击事件  MainActivity

  1. @Override  
  2.     public boolean onOptionsItemSelected(MenuItem item) {  
  3.         switch (item.getItemId()) {  
  4.             case R.id.action_list_v:  
  5.                 initListAdapterV();  
  6.                 break;  
  7.             case R.id.action_list_h:  
  8.                 initListAdapterH();  
  9.                 break;  
  10.             case R.id.action_grid_v:  
  11.                 initGridAdapterV();  
  12.                 break;  
  13.             case R.id.action_grid_h:  
  14.                 initGridAdapterH();  
  15.                 break;  
  16.             case R.id.action_stragger_v:  
  17.                 initStaggeredGridAdapterV();  
  18.                 break;  
  19.             case R.id.action_stragger_h:  
  20.                 initStaggeredGridAdapterH();  
  21.                 break;  
  22.   
  23.             default:  
  24.                 break;  
  25.         }  
  26.         return super.onOptionsItemSelected(item);  
  27.     }  
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_list_v:
                initListAdapterV();
                break;
            case R.id.action_list_h:
                initListAdapterH();
                break;
            case R.id.action_grid_v:
                initGridAdapterV();
                break;
            case R.id.action_grid_h:
                initGridAdapterH();
                break;
            case R.id.action_stragger_v:
                initStaggeredGridAdapterV();
                break;
            case R.id.action_stragger_h:
                initStaggeredGridAdapterH();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


(3)设置layoutManager和对应的adapter

  1. private void initListAdapterV() {  
  2.        //设置layoutManager  
  3.        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this);  
  4.        mRecyclerView.setLayoutManager(layoutManger);  
  5.   
  6.        //设置adapter  
  7.        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  8.        mRecyclerView.setAdapter(adapter);  
  9.    }  
  10.   
  11.    private void initListAdapterH() {  
  12.        //设置layoutManager  
  13.        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this,  
  14.                LinearLayoutManager.HORIZONTAL, false);  
  15.        mRecyclerView.setLayoutManager(layoutManger);  
  16.   
  17.        //设置adapter  
  18.        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  19.        mRecyclerView.setAdapter(adapter);  
  20.    }  
  21.   
  22.    private void initGridAdapterV() {  
  23.        //设置layoutManager  
  24.        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this2);  
  25.        mRecyclerView.setLayoutManager(layoutManger);  
  26.   
  27.        //设置adapter  
  28.        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  29.        mRecyclerView.setAdapter(adapter);  
  30.    }  
  31.   
  32.    private void initGridAdapterH() {  
  33.        //设置layoutManager  
  34.        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this2,  
  35.                LinearLayoutManager.HORIZONTAL, false);  
  36.        mRecyclerView.setLayoutManager(layoutManger);  
  37.   
  38.        //设置adapter  
  39.        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  40.        mRecyclerView.setAdapter(adapter);  
  41.    }  
  42.   
  43.    private void initStaggeredGridAdapterV() {  
  44.        //设置layoutManager  
  45.        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,  
  46.                StaggeredGridLayoutManager.VERTICAL);  
  47.        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);  
  48.   
  49.        //设置adapter  
  50.        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);  
  51.        mRecyclerView.setAdapter(adapter);  
  52.    }  
  53.   
  54.    private void initStaggeredGridAdapterH() {  
  55.        //设置layoutManager  
  56.        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,  
  57.                StaggeredGridLayoutManager.HORIZONTAL);  
  58.        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);  
  59.   
  60.        //设置adapter  
  61.        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);  
  62.        mRecyclerView.setAdapter(adapter);  
  63.    }  
 private void initListAdapterV() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initListAdapterH() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this,
                LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initGridAdapterV() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this, 2);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initGridAdapterH() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this, 2,
                LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initStaggeredGridAdapterV() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initStaggeredGridAdapterH() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }


 

(4)完整MainActivity

  1. public class MainActivity extends Activity {  
  2.   
  3.     private RecyclerView mRecyclerView;  
  4.     private List<DataBean> mDatas = new ArrayList<DataBean>();  
  5.     private List<DataBean> mStraggerDatas = new ArrayList<DataBean>();  
  6.   
  7.   
  8.     private int[] mListIcons = new int[]{R.mipmap.g1, R.mipmap.g2, R.mipmap.g3, R.mipmap.g4,  
  9.             R.mipmap.g5, R.mipmap.g6, R.mipmap.g7, R.mipmap.g8, R.mipmap.g9, R.mipmap.g10, R  
  10.             .mipmap.g11, R.mipmap.g12, R.mipmap.g13, R.mipmap.g14, R.mipmap.g15, R.mipmap  
  11.             .g16, R.mipmap.g17, R.mipmap.g18, R.mipmap.g19, R.mipmap.g20, R.mipmap.g21, R  
  12.             .mipmap.g22, R.mipmap.g23, R.mipmap.g24, R.mipmap.g25, R.mipmap.g26, R.mipmap  
  13.             .g27, R.mipmap.g28, R.mipmap.g29};  
  14.   
  15.     private int[] mStraggeredIcons = new int[]{R.mipmap.p1, R.mipmap.p2, R.mipmap.p3, R  
  16.             .mipmap.p4, R.mipmap.p5, R.mipmap.p6, R.mipmap.p7, R.mipmap.p8, R.mipmap.p9, R  
  17.             .mipmap.p10, R.mipmap.p11, R.mipmap.p12, R.mipmap.p13, R.mipmap.p14, R.mipmap  
  18.             .p15, R.mipmap.p16, R.mipmap.p17, R.mipmap.p18, R.mipmap.p19, R.mipmap.p20, R  
  19.             .mipmap.p21, R.mipmap.p22, R.mipmap.p23, R.mipmap.p24, R.mipmap.p25, R.mipmap  
  20.             .p26, R.mipmap.p27, R.mipmap.p28, R.mipmap.p29, R.mipmap.p30, R.mipmap.p31, R  
  21.             .mipmap.p32, R.mipmap.p33, R.mipmap.p34, R.mipmap.p35, R.mipmap.p36, R.mipmap  
  22.             .p37, R.mipmap.p38, R.mipmap.p39, R.mipmap.p40, R.mipmap.p41, R.mipmap.p42, R  
  23.             .mipmap.p43, R.mipmap.p44};  
  24.     private SwipeRefreshLayout mSwipeRefreshLayout;  
  25.   
  26.     @Override  
  27.     protected void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.activity_main);  
  30.   
  31.   
  32.         //找到recyclerView  
  33.         mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);  
  34.   
  35.         mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);  
  36.   
  37.   
  38.         initData();  
  39.         initListAdapterV();  
  40.   
  41.         //initListener();  
  42.   
  43.     }  
  44.   
  45.       
  46.   
  47.     private void initListAdapterV() {  
  48.         //设置layoutManager  
  49.         LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this);  
  50.         mRecyclerView.setLayoutManager(layoutManger);  
  51.   
  52.         //设置adapter  
  53.         ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  54.         mRecyclerView.setAdapter(adapter);  
  55.     }  
  56.   
  57.     private void initListAdapterH() {  
  58.         //设置layoutManager  
  59.         LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this,  
  60.                 LinearLayoutManager.HORIZONTAL, false);  
  61.         mRecyclerView.setLayoutManager(layoutManger);  
  62.   
  63.         //设置adapter  
  64.         ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  65.         mRecyclerView.setAdapter(adapter);  
  66.     }  
  67.   
  68.     private void initGridAdapterV() {  
  69.         //设置layoutManager  
  70.         LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this2);  
  71.         mRecyclerView.setLayoutManager(layoutManger);  
  72.   
  73.         //设置adapter  
  74.         ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  75.         mRecyclerView.setAdapter(adapter);  
  76.     }  
  77.   
  78.     private void initGridAdapterH() {  
  79.         //设置layoutManager  
  80.         LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this2,  
  81.                 LinearLayoutManager.HORIZONTAL, false);  
  82.         mRecyclerView.setLayoutManager(layoutManger);  
  83.   
  84.         //设置adapter  
  85.         ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);  
  86.         mRecyclerView.setAdapter(adapter);  
  87.     }  
  88.     //瀑布流—–在下节  
  89.     private void initStaggeredGridAdapterV() {  
  90.         //设置layoutManager  
  91.         StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,  
  92.                 StaggeredGridLayoutManager.VERTICAL);  
  93.         mRecyclerView.setLayoutManager(staggeredGridLayoutManager);  
  94.   
  95.         //设置adapter  
  96.         StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);  
  97.         mRecyclerView.setAdapter(adapter);  
  98.     }  
  99.    //瀑布流—–在下节  
  100.     private void initStaggeredGridAdapterH() {  
  101.         //设置layoutManager  
  102.         StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,  
  103.                 StaggeredGridLayoutManager.HORIZONTAL);  
  104.         mRecyclerView.setLayoutManager(staggeredGridLayoutManager);  
  105.   
  106.         //设置adapter  
  107.         StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);  
  108.         mRecyclerView.setAdapter(adapter);  
  109.     }  
  110.   
  111.     private void initData() {  
  112.         for (int i = 0; i < mListIcons.length; i++) {  
  113.             int iconId = mListIcons[i];  
  114.             DataBean dataBean = new DataBean();  
  115.             dataBean.iconId = iconId;  
  116.             dataBean.text = ”我是item” + i;  
  117.             mDatas.add(dataBean);  
  118.         }  
  119.         for (int i = 0; i < mStraggeredIcons.length; i++) {  
  120.             int iconId = mStraggeredIcons[i];  
  121.             DataBean dataBean = new DataBean();  
  122.             dataBean.iconId = iconId;  
  123.             dataBean.text = ”我是item” + i;  
  124.             mStraggerDatas.add(dataBean);  
  125.   
  126.         }  
  127.     }  
  128.   
  129.     @Override  
  130.     public boolean onCreateOptionsMenu(Menu menu) {  
  131.         // Inflate the menu; this adds items to the action bar if it is present.  
  132.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  133.         return true;  
  134.     }  
  135.   
  136.     @Override  
  137.     public boolean onOptionsItemSelected(MenuItem item) {  
  138.         switch (item.getItemId()) {  
  139.             case R.id.action_list_v:  
  140.                 initListAdapterV();  
  141.                 break;  
  142.             case R.id.action_list_h:  
  143.                 initListAdapterH();  
  144.                 break;  
  145.             case R.id.action_grid_v:  
  146.                 initGridAdapterV();  
  147.                 break;  
  148.             case R.id.action_grid_h:  
  149.                 initGridAdapterH();  
  150.                 break;  
  151.             case R.id.action_stragger_v:  
  152.                 initStaggeredGridAdapterV();  
  153.                 break;  
  154.             case R.id.action_stragger_h:  
  155.                 initStaggeredGridAdapterH();  
  156.                 break;  
  157.   
  158.             default:  
  159.                 break;  
  160.         }  
  161.         return super.onOptionsItemSelected(item);  
  162.     }  
  163. }  
public class MainActivity extends Activity {

    private RecyclerView mRecyclerView;
    private List<DataBean> mDatas = new ArrayList<DataBean>();
    private List<DataBean> mStraggerDatas = new ArrayList<DataBean>();


    private int[] mListIcons = new int[]{R.mipmap.g1, R.mipmap.g2, R.mipmap.g3, R.mipmap.g4,
            R.mipmap.g5, R.mipmap.g6, R.mipmap.g7, R.mipmap.g8, R.mipmap.g9, R.mipmap.g10, R
            .mipmap.g11, R.mipmap.g12, R.mipmap.g13, R.mipmap.g14, R.mipmap.g15, R.mipmap
            .g16, R.mipmap.g17, R.mipmap.g18, R.mipmap.g19, R.mipmap.g20, R.mipmap.g21, R
            .mipmap.g22, R.mipmap.g23, R.mipmap.g24, R.mipmap.g25, R.mipmap.g26, R.mipmap
            .g27, R.mipmap.g28, R.mipmap.g29};

    private int[] mStraggeredIcons = new int[]{R.mipmap.p1, R.mipmap.p2, R.mipmap.p3, R
            .mipmap.p4, R.mipmap.p5, R.mipmap.p6, R.mipmap.p7, R.mipmap.p8, R.mipmap.p9, R
            .mipmap.p10, R.mipmap.p11, R.mipmap.p12, R.mipmap.p13, R.mipmap.p14, R.mipmap
            .p15, R.mipmap.p16, R.mipmap.p17, R.mipmap.p18, R.mipmap.p19, R.mipmap.p20, R
            .mipmap.p21, R.mipmap.p22, R.mipmap.p23, R.mipmap.p24, R.mipmap.p25, R.mipmap
            .p26, R.mipmap.p27, R.mipmap.p28, R.mipmap.p29, R.mipmap.p30, R.mipmap.p31, R
            .mipmap.p32, R.mipmap.p33, R.mipmap.p34, R.mipmap.p35, R.mipmap.p36, R.mipmap
            .p37, R.mipmap.p38, R.mipmap.p39, R.mipmap.p40, R.mipmap.p41, R.mipmap.p42, R
            .mipmap.p43, R.mipmap.p44};
    private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //找到recyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);


        initData();
        initListAdapterV();

        //initListener();

    }



    private void initListAdapterV() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initListAdapterH() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new LinearLayoutManager(MainActivity.this,
                LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initGridAdapterV() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this, 2);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initGridAdapterH() {
        //设置layoutManager
        LinearLayoutManager layoutManger = new GridLayoutManager(MainActivity.this, 2,
                LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(layoutManger);

        //设置adapter
        ListAdapter adapter = new ListAdapter(MainActivity.this, mDatas);
        mRecyclerView.setAdapter(adapter);
    }
    //瀑布流-----在下节
    private void initStaggeredGridAdapterV() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }
   //瀑布流-----在下节
    private void initStaggeredGridAdapterH() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }

    private void initData() {
        for (int i = 0; i < mListIcons.length; i++) {
            int iconId = mListIcons[i];
            DataBean dataBean = new DataBean();
            dataBean.iconId = iconId;
            dataBean.text = "我是item" + i;
            mDatas.add(dataBean);
        }
        for (int i = 0; i < mStraggeredIcons.length; i++) {
            int iconId = mStraggeredIcons[i];
            DataBean dataBean = new DataBean();
            dataBean.iconId = iconId;
            dataBean.text = "我是item" + i;
            mStraggerDatas.add(dataBean);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_list_v:
                initListAdapterV();
                break;
            case R.id.action_list_h:
                initListAdapterH();
                break;
            case R.id.action_grid_v:
                initGridAdapterV();
                break;
            case R.id.action_grid_h:
                initGridAdapterH();
                break;
            case R.id.action_stragger_v:
                initStaggeredGridAdapterV();
                break;
            case R.id.action_stragger_h:
                initStaggeredGridAdapterH();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}


 

 

17.瀑布流的实现

1.接上

2.item布局

  1. <RelativeLayout  
  2.             android:layout_width=“match_parent”  
  3.             android:layout_height=“wrap_content”  
  4.             android:padding=“5dp”>  
  5.   
  6.             <ImageView  
  7.                 android:id=“@+id/item_straggered_iv”  
  8.                 android:layout_width=“match_parent”  
  9.                 android:layout_height=“wrap_content”  
  10.                 android:scaleType=“centerCrop”  
  11.                 android:src=“@mipmap/ic_launcher”/>  
  12.   
  13.             <TextView  
  14.                 android:id=“@+id/item_straggered_tv”  
  15.                 android:layout_width=“wrap_content”  
  16.                 android:layout_height=“wrap_content”  
  17.                 android:layout_below=“@id/item_straggered_iv”  
  18.                 android:layout_centerHorizontal=“true”  
  19.                 android:gravity=“center”  
  20.                 android:text=“gagaga”  
  21.                 android:textColor=“#212121”  
  22.                 android:textSize=“16sp”/>  
  23.         </RelativeLayout>  
<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp">

            <ImageView
                android:id="@+id/item_straggered_iv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop"
                android:src="@mipmap/ic_launcher"/>

            <TextView
                android:id="@+id/item_straggered_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/item_straggered_iv"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="gagaga"
                android:textColor="#212121"
                android:textSize="16sp"/>
        </RelativeLayout>


3.StraggerAdapter.Java

  1. public class StraggerAdapter extends RecyclerView.Adapter<StraggerAdapter.MyHolder> {  
  2.     private Context context;  
  3.     private List<DataBean> datas;  
  4.   
  5.     public StraggerAdapter(Context context, List<DataBean> datas) {  
  6.         this.context = context;  
  7.         this.datas = datas;  
  8.     }  
  9.   
  10.     @Override  
  11.     public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {//决定根布局  
  12.         //        TextView tv = new TextView(context);//根布局  
  13.         View itemView = View.inflate(context, R.layout.item_stragger, null);  
  14.         return new MyHolder(itemView);  
  15.     }  
  16.   
  17.     @Override  
  18.     public void onBindViewHolder(MyHolder myHolder, int position) {//填充数据  
  19.         myHolder.setDataAndRefreshUI(datas.get(position));  
  20.     }  
  21.   
  22.     @Override  
  23.     public int getItemCount() {//条目总数  
  24.         if (datas != null) {  
  25.             return datas.size();  
  26.         }  
  27.         return 0;  
  28.     }  
  29.   
  30.     public class MyHolder extends RecyclerView.ViewHolder {  
  31.         //孩子对象  
  32.         private TextView mTvName;  
  33.         private ImageView mIvIcon;  
  34.   
  35.         public MyHolder(View itemView) {  
  36.             super(itemView);  
  37.             //初始化孩子对象  
  38.             mTvName = (TextView) itemView.findViewById(R.id.item_straggered_tv);  
  39.             mIvIcon = (ImageView) itemView.findViewById(R.id.item_straggered_iv);  
  40.         }  
  41.   
  42.         /** 
  43.          * 设置itemView的数据展示 
  44.          * 
  45.          * @param dataBean 
  46.          */  
  47.         public void setDataAndRefreshUI(DataBean dataBean) {  
  48.             mTvName.setText(dataBean.text);  
  49.             mIvIcon.setImageResource(dataBean.iconId);  
  50.         }  
  51.     }  
  52. }  
public class StraggerAdapter extends RecyclerView.Adapter<StraggerAdapter.MyHolder> {
    private Context context;
    private List<DataBean> datas;

    public StraggerAdapter(Context context, List<DataBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup viewGroup, int i) {//决定根布局
        //        TextView tv = new TextView(context);//根布局
        View itemView = View.inflate(context, R.layout.item_stragger, null);
        return new MyHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyHolder myHolder, int position) {//填充数据
        myHolder.setDataAndRefreshUI(datas.get(position));
    }

    @Override
    public int getItemCount() {//条目总数
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        //孩子对象
        private TextView mTvName;
        private ImageView mIvIcon;

        public MyHolder(View itemView) {
            super(itemView);
            //初始化孩子对象
            mTvName = (TextView) itemView.findViewById(R.id.item_straggered_tv);
            mIvIcon = (ImageView) itemView.findViewById(R.id.item_straggered_iv);
        }

        /**
         * 设置itemView的数据展示
         *
         * @param dataBean
         */
        public void setDataAndRefreshUI(DataBean dataBean) {
            mTvName.setText(dataBean.text);
            mIvIcon.setImageResource(dataBean.iconId);
        }
    }
}


 

4.MainActivity中

  1. private void initStaggeredGridAdapterV() {  
  2.     //设置layoutManager  
  3.     StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,  
  4.             StaggeredGridLayoutManager.VERTICAL);  
  5.     mRecyclerView.setLayoutManager(staggeredGridLayoutManager);  
  6.   
  7.     //设置adapter  
  8.     StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);  
  9.     mRecyclerView.setAdapter(adapter);  
  10. }  
  11. 瀑布流    上面是纵向瀑布流  
  12. private void initStaggeredGridAdapterH() {  
  13.     //设置layoutManager  
  14.     StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,  
  15.             StaggeredGridLayoutManager.HORIZONTAL);  
  16.     mRecyclerView.setLayoutManager(staggeredGridLayoutManager);  
  17.   
  18.     //设置adapter  
  19.     StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);  
  20.     mRecyclerView.setAdapter(adapter);  
  21. }  
    private void initStaggeredGridAdapterV() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }
//横向瀑布流    上面是纵向瀑布流
    private void initStaggeredGridAdapterH() {
        //设置layoutManager
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2,
                StaggeredGridLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(staggeredGridLayoutManager);

        //设置adapter
        StraggerAdapter adapter = new StraggerAdapter(MainActivity.this, mStraggerDatas);
        mRecyclerView.setAdapter(adapter);
    }


 

 

18.cardView引入

1.在cardview独立的包中

2.直接套在布局中

  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <FrameLayout  
  3.     xmlns:android=“http://schemas.android.com/apk/res/android”  
  4.     xmlns:app=“http://schemas.android.com/apk/res-auto”  
  5.     android:layout_width=“match_parent”  
  6.     android:layout_height=“wrap_content”  
  7.     >  
  8.   
  9.     <android.support.v7.widget.CardView  
  10.         android:layout_width=“match_parent”  
  11.         android:layout_height=“wrap_content”  
  12.         android:layout_margin=“5dp”  
  13.         >  
  14.   
  15.         <RelativeLayout  
  16.             android:layout_width=“match_parent”  
  17.             android:layout_height=“wrap_content”  
  18.             android:paddingBottom=“8dp”>  
  19.   
  20.             <ImageView  
  21.                 android:id=“@+id/iv_icon”  
  22.                 android:layout_width=“wrap_content”  
  23.                 android:layout_height=“wrap_content”  
  24.                 android:layout_alignParentLeft=“true”  
  25.                 android:layout_alignParentTop=“true”  
  26.                 android:layout_centerVertical=“true”  
  27.                 android:layout_margin=“10dp”  
  28.                 android:src=“@mipmap/ic_launcher”/>  
  29.   
  30.             <TextView  
  31.                 android:id=“@+id/tv_name”  
  32.                 android:layout_width=“match_parent”  
  33.                 android:layout_height=“wrap_content”  
  34.                 android:layout_centerVertical=“true”  
  35.                 android:layout_toRightOf=“@+id/iv_icon”  
  36.                 android:text=“Name”/>  
  37.   
  38.         </RelativeLayout>  
  39.     </android.support.v7.widget.CardView>  
  40. </FrameLayout>  
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="8dp">

            <ImageView
                android:id="@+id/iv_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher"/>

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/iv_icon"
                android:text="Name"/>

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>


3.可以设置各种属性,圆角,z轴大小…..

 

 

19.swiperefreshLayout的使用

1.下拉刷新

2.使用

(1)

  1. <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  2.                 xmlns:tools=“http://schemas.android.com/tools”  
  3.                 android:layout_width=“match_parent”  
  4.                 android:layout_height=“match_parent”  
  5.                 tools:context=“.MainActivity”>  
  6.   
  7.     <android.support.v4.widget.SwipeRefreshLayout  
  8.         android:id=“@+id/swipeRefreshLayout”  
  9.         android:layout_width=“match_parent”  
  10.         android:layout_height=“wrap_content”  
  11.         >  
  12.   
  13.         <android.support.v7.widget.RecyclerView  
  14.             android:id=“@+id/recyclerView”  
  15.             android:layout_width=“match_parent”  
  16.             android:layout_height=“match_parent”  
  17.             />  
  18.     </android.support.v4.widget.SwipeRefreshLayout>  
  19. </RelativeLayout>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>


 

(2)

  1. private SwipeRefreshLayout mSwipeRefreshLayout;  
  2.   
  3.   @Override  
  4.   protected void onCreate(Bundle savedInstanceState) {  
  5.       super.onCreate(savedInstanceState);  
  6.       setContentView(R.layout.activity_main);  
  7.   
  8.   
  9.       //找到recyclerView  
  10.       mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);  
  11.       //下拉刷新控件  
  12.       mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);  
  13.   
  14.   
  15.      // initData();  
  16.       //initListAdapterV();  
  17.   
  18.       initListener();  
  19.   
  20.   }  
  21.   
  22.   private void initListener() {  
  23.       mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {  
  24.           @Override  
  25.           public void onRefresh() {  
  26.               //模拟加载数据  
  27.               new Thread(new Runnable() {  
  28.                   @Override  
  29.                   public void run() {  
  30.                       SystemClock.sleep(2000);  
  31.                       MainActivity.this.runOnUiThread(new Runnable() {  
  32.                           @Override  
  33.                           public void run() {  
  34.                               //停止刷新操作  
  35.                               mSwipeRefreshLayout.setRefreshing(false);  
  36.                               //得到adapter.然后刷新  
  37.                               mRecyclerView.getAdapter().notifyDataSetChanged();  
  38.                           }  
  39.                       });  
  40.   
  41.                   }  
  42.               }).start();  
  43.               ;  
  44.           }  
  45.       });  
  46.   }  
  private SwipeRefreshLayout mSwipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //找到recyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //下拉刷新控件
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);


       // initData();
        //initListAdapterV();

        initListener();

    }

    private void initListener() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //模拟加载数据
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(2000);
                        MainActivity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                //停止刷新操作
                                mSwipeRefreshLayout.setRefreshing(false);
                                //得到adapter.然后刷新
                                mRecyclerView.getAdapter().notifyDataSetChanged();
                            }
                        });

                    }
                }).start();
                ;
            }
        });
    }


 

 

 

20.兼容性

1.虽然Material Design新增了许多新特性,但是并不是所有新内容对对下保持了兼容。

2.使用v7包————v7 support libraries r21 及更高版本包含了以下Material Design特性:

(1).  使用Theme.AppCompat主题包含调色板主体属性,可以对应用的主题做统一的配色,但是不包括状态栏和底部操作栏
(2).  RecyclerView和CardView被独立出来,只要引入jar包,即可适配7以上的所有版本。
(3).  Palette类用于从图片提取主色调

 

3.系统组件————–Theme.AppCompat主题中提供了这些组件的Material Design style:

(1).  EditText
(2).  Spinner
(3).  CheckBox
(4).  RadioButton
(5).  SwitchCompat
(6).  CheckedTextView
(7).  Color Palette

 

4.以下特性只在Android 5.0 (API level 21) 及以上版本中可用:

(1).  转场动画
(2).  触摸反馈
(3).  圆形展示动画
(4).  路径动画
(5).  矢量图
(6).  tint染色

所以在代码中遇上使用这些api的地方需要进行版本判断

  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {  
  2.     // 使用新特性  
  3. else {  
  4.     // 用其他替代方式  
  5. }  
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // 使用新特性
    } else {
        // 用其他替代方式
    }


5. RippleDrawable 提供触摸反馈特效,即5.0的button按压下的水波纹效果。

(1).  [https://github.com/03uk/RippleDrawable](https://github.com/03uk/RippleDrawable)
(2).  [https://github.com/siriscac/RippleView](https://github.com/siriscac/RippleView)
(3).  [https://github.com/balysv/material-ripple](https://github.com/balysv/material-ripple)

 

6.状态动画

[https://github.com/NghiaTranUIT/Responsive-Interaction-Control](https://github.com/NghiaTranUIT/Responsive-Interaction-Control)

 

7.Material Design风格的对话框

[https://github.com/lewisjdeane/L-Dialogs](https://github.com/lewisjdeane/L-Dialogs)

 

8.Material Design风格兼容包

[https://github.com/navasmdc/MaterialDesignLibrary](https://github.com/navasmdc/MaterialDesignLibrary)

 

9.支持修改状态栏和底部操作栏

https://github.com/jgilfelt/SystemBarTint

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值