码迷,mamicode.com
首页 > 其他好文 > 详细

Material Design UI Widgets

时间:2015-12-23 19:50:37      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:


    Android L 开发人员预览支持库提供两个新的Widgets,RecyclerView和CardView。

使用这两个Widgets能够显示复杂的Listview和卡片布局。这两个Widgets默认使用Material design。



RecyclerView

    RecyclerView是一个更高级柔性版本号的Listview。RecyclerView是一个能包括非常多视图的容器。它能完美的处理循环和滚动。在item动态变化的Listview使用RecyclerView。


RecyclerView使用非常easy。由于它提供了:

1、定位item的布局管理器

2、常见的item操作默认动画


你能够灵活的为RecyclerView自己定义布局管理器和动画。

使用RecyclerView。必须使用指定一个adapter、定义一个布局管理器。创建adapter必须继承自RecyclerView.Adapter。实施的细节须要看数据类型和须要的视图。

技术分享

   RecyclerView widget



    RecyclerView 提供了 LayoutManager。RecylerView 不负责子 View 的布局,我们能够自己定义 LayoutManager 来实现不同的布局效果,眼下仅仅提供了LinearLayoutManager。

LinearLayoutManager 能够指定方向。默认是垂直, 能够指定水平, 这样就轻松实现了水平的 ListView。


RecyclerView Demo:

1、布局文件

<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2、Activity文件

public class MyActivity extends Activity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // improve performance if you know that changes in content
        // do not change the size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }
    ...
}
To create a simple adapter:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the type of views that you are using
    // (custom viewholder)
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_text_view, null);
        // set the view‘s size, margins, paddings and layout parameters
        ...
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

3、Recycler adapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the type of views that you are using
    // (custom viewholder)
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_text_view, null);
        // set the view‘s size, margins, paddings and layout parameters
        ...
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

RecyclerView 的标准化了 ViewHolder, 编写 Adapter 面向的是 ViewHoder 而不在是View 了, 复用的逻辑被封装了, 写起来更加简单。

CardView


CardView继承自FrameLayout类,能够在一个卡片布局中一致性的显示内容。卡片能够包括圆角和阴影。


能够使用android:elevation属性,创建一个阴影的卡片。


如何指定CardView的属性:

1、使用android:cardCornerRadius属性指定圆角半径

2、使用CardView.setRadius 设置圆角半径。

3、使用 android:cardBackgroundColor属性设置卡片颜色


在创建布局文件里创建CardView:

<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="200dp"
    android:layout_height="200dp"
    card_view:cardCornerRadius="4dp">

    <TextView
        android:id="@+id/info_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.v7.widget.CardView>



/**
* @author 张兴业
*  我的新浪微博:@张兴业TBOW
*/


參考:

http://developer.android.com/preview/material/ui-widgets.html


Material Design UI Widgets

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/5070993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!