RecyclerView和CardView使用

预备知识

RecyclerView是Android5.0中support library的新控件,可以说是ListView的重新设计,但使用上更“麻烦”一点。
ListView使用中为了效率,使用了ViewHolder,RecyclerView将ViewHolder集成进来,必须实现。
使用RecyclerView, 需要实现一个Adapter和一个layout manager(用来维护item view)。 Adapter继承RecyclerView.Adapter,来完成itemView的绑定。
如下图:
RecyclerView

RecyclerView provides these built-in layout managers:
LinearLayoutManager shows items in a vertical or horizontal scrolling list.
GridLayoutManager shows items in a grid.
StaggeredGridLayoutManager shows items in a staggered grid.

添加依赖

dependencies {
        compile 'com.android.support:cardview-v7:22.2.1'
        compile 'com.android.support:recyclerview-v7:22.2.1'
 }

ViewHolder

ViewHolder就是itemView的布局缓存机制,只需要一次findViewById()。

        public static class ItemViewHolder extends RecyclerView.ViewHolder {
            public final ImageView mImageView;
            public final TextView mTextView;

            public ItemViewHolder(View view) {
                super(view);
                mImageView = (ImageView) view.findViewById(R.id.avatar);
                mTextView = (TextView) view.findViewById(R.id.tv_content);
            }

            @Override
            public String toString() {
                return super.toString() + " '" + mTextView.getText();
            }
        }

RecyclerViewAdapter

    //创建ViewHolder,inflate(R.layout.list_item)生产view,再new ViewHolder()完成
    onCreateViewHolder()
    //有了viewHolder和数据之后,进行绑定咯。
    onBindViewHolder()
    public static class SimpleStringRecyclerViewAdapter
            extends RecyclerView.Adapter<ItemViewHolder> {
        private List<String> mValues;


        public String getValueAt(int position) {
            return mValues.get(position);
        }

        public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
            mValues = items;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item, parent, false);
            return new ItemViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
            ((ItemViewHolder)holder).mTextView.setText(mValues.get(position));
            ((ItemViewHolder)holder).mImageView.setImageResource(R.mipmap.ic_launcher);         
        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }
    }

布局文件

RecyclerView的布局

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main_swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"/>

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

CardView就是一个类似卡片的样子

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/tools"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    card_view:cardCornerRadius="4dp">

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

        <ImageView
            android:id="@+id/avatar"
            android:layout_width="@dimen/list_item_avatar_size"
            android:layout_height="@dimen/list_item_avatar_size"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="16dp" />

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/avatar" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

展示list

        mRecyclerView.setLayoutManager(mLinearLayoutManager = new LinearLayoutManager(getActivity()));
        mAdapter = new SimpleStringRecyclerViewAdapter(getActivity(), mDataList);
        mRecyclerView.setAdapter(mAdapter);

rv

Animations

用默认的,还不错。

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int itemPosition = SwipeRefreshFragmentList.mRecyclerView.getChildAdapterPosition(v);
            mValues.remove(itemPosition);
            notifyItemRemoved(itemPosition);
        }
    });

对item定义动画,以后再表啊。

搞定收工

GitHub地址:https://github.com/captain-miao/AndroidStartupDemo
apk下载地址:http://fir.im/yan

RecyclerView和CardView使用》有1个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注