RecyclerView基本使用方法

RecyclerView是Android Lollipop新增的一个类似于ListView的ViewGroup。并作为一个单独的jar放在support-v7包中。
在Android Studio中想要使用RecyclerView,需在build.gradle的dependencies中添加下面这句代码:
   
   
compile 'com.android.support:recyclerview-v7:23.0.1'
在布局文件中以全类名的方式引用RecyclerView:
   
   
<android.support.v7.widget.RecyclerView
android:background="@color/colorText"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:id="@+id/rv"
/>
RecyclerView的adapter和ListView的稍有不同,它没有已经实现好的像ArrayAdapter的adapter,它的adapter必须由开发者自己实现。官方API中提供了一个RecyclerView.Adapter类,用户自定义的adapter继承它,然后实现其未实现的方法即可。另,RecyclerView.Adapter用维护ViewHolder的方式取代了以前ListView的adapte中的getView方法。在ListView的adapter中,官方只是建议使用ViewHolder,而RecyclerView.Adapter强制开发者使用ViewHolder。为此,官方API中提供了RecyclerView.ViewHolder给开发者用于自定义ViewHolder。

RecyclerView.Adapter要实现的方法主要有以下三个:
getItemCount:和ListView的adapter没有区别
   
   
@Override
public int getItemCount() {
return itemList == null ? 0 : itemList.size();
}
onCreateViewHolder :创建Item布局,并以ViewHolder的形式返回
   
   
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_view,parent,false);
return new MyViewHolder(view);
}
onBindViewHolder:绑定数据
   
   
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final String str = itemList.get(position);
holder.tv.setText(str);
}

同时,RecyclerView.Adapter中没有OnItemClickListener。所以开发者要自定义监听回调方法实现此功能。具体的实现方式就是先自定义一个公共接口,让需要监听RecyclerView的类去实现此接口:
   
   
public interface OnItemClickListener{
void OnItemClick(int position,Object object);
}
同时,在自定义的Adapter中,提供一个设置监听的公共方法:
   
   
public void setOnItemClickListener(OnItemClickListener listener)
{
this.listener = listener;
}
最后,在绑定数据的同时,添加一个监听,调用接口里的监听方法:
   
   
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final String str = itemList.get(position);
holder.tv.setText(str);
 
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null != listener)
listener.OnItemClick(position,str);
}
});
}

具体的自定义RecyclerView.Adapter实现如下:
   
   
package com.egotrip.wcc.adapter;
 
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
import com.egotrip.wcc.R;
 
import java.util.List;
 
/**
* Created by wcc on 2015/11/27.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
 
public List<String> itemList;
private Context context;
private OnItemClickListener listener;
 
 
public RecyclerViewAdapter(Context context,List<String> itemList)
{
this.context = context;
this.itemList = itemList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_recycler_view,parent,false);
return new MyViewHolder(view);
}
 
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
final String str = itemList.get(position);
holder.tv.setText(str);
 
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(null != listener)
listener.OnItemClick(position,str);
}
});
}
 
public interface OnItemClickListener{
void OnItemClick(int position,Object object);
}
 
public void setOnItemClickListener(OnItemClickListener listener)
{
this.listener = listener;
}
 
@Override
public int getItemCount() {
return itemList == null ? 0 : itemList.size();
}
 
public class MyViewHolder extends RecyclerView.ViewHolder{
 
private TextView tv;
 
public MyViewHolder(View itemView) {
super(itemView);
 
tv = (TextView) itemView.findViewById(R.id.tv);
}
}
}
对应的Activity:
   
   
package com.egotrip.wcc.ui;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
 
import com.egotrip.wcc.R;
import com.egotrip.wcc.adapter.RecyclerViewAdapter;
import com.egotrip.wcc.utils.ToastUtils;
 
import java.util.ArrayList;
import java.util.List;
 
public class RecyclerViewActivity extends AppCompatActivity implements RecyclerViewAdapter.OnItemClickListener{
 
private String[] list = {"纸杯蛋糕(Cupcake)" ,
"甜甜圈(Donut)" ,
"闪电泡芙(Éclair)" ,
"冻酸奶(Froyo)" ,
"姜饼(Gingerbread)" ,
"蜂巢(Honeycomb)" ,
"冰淇淋三明治(Ice Cream Sandwich)" ,
"果冻豆(Jelly Bean)" ,
"奇巧(KitKat)" ,
"棒棒糖(Lollipop)" ,
"棉花糖(Marshmallow)。"};
 
private List<String> itemList;
private RecyclerViewAdapter adapter;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
initview();
initData();
}
 
private void initData() {
itemList = new ArrayList<String>();
for(String str : list)
{
itemList.add(str);
}
 
adapter = new RecyclerViewAdapter(this,itemList);
adapter.setOnItemClickListener(this);
 
rv.setAdapter(adapter);
}
 
RecyclerView rv;
private void initview() {
rv = (RecyclerView) findViewById(R.id.rv);
 
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, false));
 
rv.setItemAnimator(new DefaultItemAnimator());
}
 
@Override
public void OnItemClick(int position, Object object) {
// Toast.makeText(this,(String) object,Toast.LENGTH_SHORT).show();
ToastUtils.showToast((String) object, this);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值