RecyclerView的使用之HelloWorld

2019-12-10 18:49:45王冬梅

3:建立RecyclerView Item布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://www.easck.com/apk/res-auto"
xmlns:android="http://www.easck.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:id="@+id/cv_item"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardBackgroundColor="#795548"
card_view:cardElevation="4dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/iv_pic"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/tv_text"
android:padding="20dp"
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v7.widget.CardView>

这里使用了一个叫CardView的控件,继承自FrameLayout,它是Google提供的一个卡片式视图容器,可以很方便的显示出具有阴影和圆角的卡片式布局,像这样

RecyclerView,HelloWorld

CardView跟RecyclerView一样使用前也需要进行导入

compile 'com.android.support:cardview-v7:23.1.1'

4:然后建立RecyclerView的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.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Lijizhou on 2016/2/3.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.NormalViewHolder> {
private LayoutInflater mLayoutInflater;
private Context mContext;
private String [] mTitle;
private int [] mPic;
public RecyclerViewAdapter(Context context,String[]title,int[] pic){
mContext=context;
mTitle=title;
mPic=pic;
mLayoutInflater=LayoutInflater.from(context);
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class NormalViewHolder extends RecyclerView.ViewHolder{
TextView mTextView;
CardView mCardView;
ImageView mImageView;
public NormalViewHolder(View itemView) {
super(itemView);
mTextView=(TextView)itemView.findViewById(R.id.tv_text);
mCardView=(CardView)itemView.findViewById(R.id.cv_item);
mImageView=(ImageView)itemView.findViewById(R.id.iv_pic);
}
}
//在该方法中我们创建一个ViewHolder并返回,ViewHolder必须有一个带有View的构造函数,这个View就是我们Item的根布局,在这里我们使用自定义Item的布局;
@Override
public NormalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NormalViewHolder(mLayoutInflater.inflate(R.layout.item_view,parent,false));
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(NormalViewHolder holder, final int position) {
holder.mTextView.setText(mTitle[position]);
holder.mImageView.setBackgroundResource(mPic[position]);
holder.mCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,mTitle[position],3000).show();
}
});
}
//获取数据的数量
@Override
public int getItemCount() {
return mTitle==null ? 0 : mTitle.length;
}
}