最近项目不是特别紧张,所以闲下来看了一下安卓的Recyclerview和CradView这俩东东,Recyclerview是安卓新出的一个组件,用来取代我们用了很久的ListView,直接上代码吧
AS中如果想使用这个组件
1,首先需要在gradle 文件里面加上
compile 'com.android.support:recyclerview-v7:21.+'
compile 'com.android.support:cardview-v7:21.+'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-appindexing:8.4.0'
配置上这些后咱们就可以接着写了
2,我们在xml文件中可以把这个组件写进去
<android.support.v7.widget.RecyclerView
android:id="@+id/id_view"
android:divider="#ffff0000"
android:dividerHeight="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
app:cardCornerRadius="5dp"
app:elevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/itemView"
android:padding="5dp" >
<ImageView
android:id="@+id/pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:scaleType="centerCrop" />
<TextView
android:clickable="true"
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:gravity="right|bottom"
android:textColor="@android:color/white"
android:textSize="24sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
3,在我们要写的activity里面设置上相应的一些属性
mRecyclerView = (RecyclerView) findViewById(R.id.id_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mAdapter = new HomeAdapter(this, actors));
//设置固定大小
mRecyclerView.setHasFixedSize(true);
//创建线性布局
mLayoutManager = new LinearLayoutManager(this);
//垂直方向
mLayoutManager.setOrientation(OrientationHelper.VERTICAL);
//给RecyclerView设置布局管理器
mRecyclerView.setLayoutManager(mLayoutManager);
4,主要是适配器 下面咱们看适配的代码
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder>{
private List<Actor> actors;
private Context mContext;
public interface OnItemClickLitener {
void onItemClick(View view, int position);
void onItemLongClick(View view , int position);
}
private OnItemClickLitener mOnItemClickLitener;
public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
public HomeAdapter(Context context , List<Actor> actors) {
this.mContext = context;
this.actors = actors;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i ) {
// 给ViewHolder设置布局文件
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.toolbar, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder( final ViewHolder viewHolder, int i ) {
// 给ViewHolder设置元素
Actor p = actors.get(i);
viewHolder.mTextView.setText(p.name);
viewHolder.mImageView.setImageDrawable(mContext.getDrawable(p.getImageResourceId(mContext)));
// // 如果设置了回调,则设置点击事件
if (mOnItemClickLitener != null) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = viewHolder.getPosition();
// int pos = viewHolder.getLayoutPosition();
mOnItemClickLitener.onItemClick(viewHolder.itemView, pos);
}
});
}
viewHolder.linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// int pos = viewHolder.getLayoutPosition();
int pos = viewHolder.getPosition();
mOnItemClickLitener.onItemLongClick(viewHolder.itemView, pos);
return false;
}
});
}
@Override
public int getItemCount() {
// 返回数据总数
return actors == null ? 0 : actors.size();
}
// 重写的自定义ViewHolder
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public LinearLayout linearLayout;
public ImageView mImageView;
public ViewHolder( View v ) {
super(v);
linearLayout = (LinearLayout)v.findViewById(R.id.itemView);
mTextView = (TextView) v.findViewById(R.id.name);
mImageView = (ImageView) v.findViewById(R.id.pic);
}
}
}
5,相信你们一定会问item的点击事件怎么写,上面的适配器代码,我已经写了接口,所以我们只要在activity里面实现就行了
mAdapter.setOnItemClickLitener(new HomeAdapter.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(MainActivity.this, position + " click", Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(MainActivity.this, position + " long click", Toast.LENGTH_SHORT).show();
}
});
OK了,就讲到这里吧,有问题的童鞋请留言~希望和大家多多交流
工程下载地址:http://download.youkuaiyun.com/detail/liwei123liwei123/9576727