RecyclerView+CardView之初体验

RecyclerView是5.0新出的,成为了ListView的替代者,已经使用的越来越广泛了!同时,CardView的出现也提供了一种卡片式的布局美化效果。
而且,RecyclerView+CardView的结合使用现如今也比较常见。于是乎,咱也来体验一把过过瘾!!
首先,不管三七二十几,先添加依赖:

compile 'com.android.support:recyclerview-v7:21.+'
    compile 'com.android.support:cardview-v7:21.+'

然后就可以在xml里使用了。在activity_main里使用v7包下的RecyclerView,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView  android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

接下来就在MainActivity里声明使用:

package com.example.lenovo.recyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
import Adapter.MyRecyclerViewAdapter;

public class MainActivity extends Activity {
    private RecyclerView mRecyclerView;
    private ArrayList<String> mList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
    }

    /** * 模拟网络数据 */
    private void initData() {
        for(int i=0;i<30;i++){
            mList.add("数据"+i);
        }
    }

    /** * 初始化recyclerview */
    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //一句代码就能改变布局的切换,是不是很6!!
// mRecyclerView.setLayoutManager(new GridLayoutManager(this,2));
        mRecyclerView.setAdapter(new MyRecyclerViewAdapter(this,mList));
    }
}

下面的是数据适配器代码:

package Adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.example.lenovo.recyclerview.R;
import java.util.ArrayList;

/** * Created by shan on 2016/7/5. */
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
    private Context context;
    private ArrayList<String> mList;

    public MyRecyclerViewAdapter(Context context, ArrayList<String> list) {
        this.context = context;
        mList = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder,  final int i) {
//绑定数据 myViewHolder.mTextView.setText(mList.get(i).toString());
        //item的点击事件监听
        myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击了第"+i+"项"”, Toast.LENGTH_SHORT).show();
            }
        });
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView mTextView;
        public MyViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.textview);
        }
    }
}

上面用到的item样式布局很简单,就一个卡片式的TextView,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" app:cardCornerRadius="5dp" app:cardUseCompatPadding="true" android:foreground="?android:attr/selectableItemBackground" android:layout_width="match_parent" android:layout_height="wrap_content">
    <TextView  android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="数据" android:gravity="center_horizontal" android:paddingBottom="10dp" android:paddingTop="10dp" android:textSize="16sp"/>
</android.support.v7.widget.CardView>

说明一下:根布局需添加命名空间(就是下面这句代码):

xmlns:app="http://schemas.android.com/apk/res-auto"

关于CardView的属性有很多,根据自己需要添加:这里我只使用了3个属性:

app:cardCornerRadius="5dp"
    app:cardUseCompatPadding="true"
    android:foreground="?android:attr/selectableItemBackground"

运行效果图如下:

RecyclerView+CardView之初体验_第1张图片
关于更多的使用方法,后面的帖子再描述~~

你可能感兴趣的:(RecyclerView+CardView之初体验)