RecyclerView是Android5.0中新推出的一种全新的适配器控件,用以取代ListView和GridView,要使用此控件,推荐使用Android Studio作为开发工具,在Eclipse中导入jar和在Studio中还是有些微的差距的
RecyclerView所代表的意义是:
我只管Recycler View ,也就是说RecyclerView只管回收与复用View,其他的可以自己去设置,可以看出其高度的解耦,给与你充分的定制自由,(所以你可以轻松的通过这个控件实现ListView,GridView,瀑布流的效果)
自定义RecyclerView,实现其滚动监听
package longyue.com.pritaceone;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
//自定义RecyclerView
public class MyRecyclerView extends RecyclerView {
public MyRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyRecyclerView(Context context) {
super(context);
}
public interface OnRecyclerScrollChangeListener {
// position:这个条目在整个集合中的位置
public void onRecyclerChanged(View view, int position);
}
private OnRecyclerScrollChangeListener mListener;
public void setOnRecyclerScrollChangeListener(
OnRecyclerScrollChangeListener listener) {
this.mListener = listener;
}
@Override
public void onScrollStateChanged(int state) {
}
// volicity:滚动的速度.
// firstItemIndex:第一个可见条目的索引.始终是0.
@Override
public void onScrolled(int volicity, int firstItemIndex) {
// 获取ViewGroup中某个位置处的子View.
// 根据视图的位置,获取该视图
View newView = getChildAt(firstItemIndex);
// getChildPosition(newView):根据视图,获取该视图在容器中的位置
if (mListener != null) {
mListener.onRecyclerChanged(newView, getChildPosition(newView));
}
}
}
RecyclerView适配器填充并实现其单击事件,同样的可以定义他的长按事件(使用接口回调实现)
package longyue.com.pritaceone;
import java.util.List;
import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
//适配器必须有ViewHolder泛型
public class MyAdapter extends Adapter<MyAdapter.MyViewHolder> {
private LayoutInflater mInflater;
private List<Integer> mList;
// 必须有ViewHolder
public class MyViewHolder extends ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
ImageView iv;
}
// 点击事件的接口
public interface OnRecyclerItemClickListener {
public void onItemClick(int position);
}
private OnRecyclerItemClickListener mListener;
// 设置点击监听
public void setOnRecyclerItemClickListener(
OnRecyclerItemClickListener listener) {
this.mListener = listener;
}
public MyAdapter(Context context, List<Integer> list) {
mInflater = LayoutInflater.from(context);
this.mList = list;
}
// 显示条目的数量
@Override
public int getItemCount() {
return mList != null ? mList.size() : 0;
}
// 创建ViewHolder:做getView()中if(convertView==null){...}做的事情.
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int position) {
View itemView = mInflater
.inflate(R.layout.recycler_item, parent, false);
MyViewHolder holder = new MyViewHolder(itemView);
holder.iv = (ImageView) itemView.findViewById(R.id.iv_item);
return holder;
}
// 绑定数据
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.iv.setImageResource(mList.get(position));
// ImageView点击事件
holder.iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
// 回传每个被点击的条目的位置.
mListener.onItemClick(position);
}
}
});
}
}
MainActivity调用
package longyue.com.pritaceone;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends Activity {
private MyRecyclerView myRecyclerView;
private ImageView ivShow;
private List<Integer> asList;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
// 实现RecyclerView的按钮点击事件
adapter.setOnRecyclerItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
@Override
public void onItemClick(int position) {
ivShow.setImageResource(asList.get(position));
}
});
// 处理滚动监听
myRecyclerView.setOnRecyclerScrollChangeListener(new MyRecyclerView.OnRecyclerScrollChangeListener() {
@Override
public void onRecyclerChanged(View view, int position) {
ivShow.setImageResource(asList.get(position));
}
});
}
private void initView(){
myRecyclerView =(MyRecyclerView) findViewById(R.id.rv);
ivShow = (ImageView) findViewById(R.id.iv_show);
// 数据源
asList = Arrays.asList(R.mipmap.a, R.mipmap.b, R.mipmap.c,
R.mipmap.d, R.mipmap.e, R.mipmap.f, R.mipmap.g,
R.mipmap.h, R.mipmap.l);
adapter = new MyAdapter(this, asList);
// 必须有布局管理器:
LinearLayoutManager manager = new LinearLayoutManager(this);
// 设置布局方向为水平
manager.setOrientation(LinearLayoutManager.HORIZONTAL);
myRecyclerView.setLayoutManager(manager);
myRecyclerView.setAdapter(adapter);
}
}