RecycleView简介
RecyclerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,这一点从它的名字Recyclerview可以看出。
使用RecycleView。需要在gradle下引入 compile'com.android.support:recyclerview-v7:25.1.0'。
RecycleView使用
直接贴出代码。重要的部分在代码中有注释。
MainActivity.java
public class MainActivity extends AppCompatActivity {
MyAdapter adapter;
Context context;
public List<String> dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
RecyclerView recyclerView= (RecyclerView) findViewById(R.id.ry);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));//设置展示数据的方式 LinearLayoutManager或 GridLayoutManager
recyclerView.addItemDecoration(new DividerItemDecoration(this,MyItemDecoration.VERTICAL_LIST));//添加自定义分割线
addData();
adapter=new MyAdapter(dataList);
ItemTouchHelper helper=new ItemTouchHelper(new MyItemTouchCallBack(adapter));//触摸事件 拖动排序以及右拖删除
helper.attachToRecyclerView(recyclerView);
recyclerView.setAdapter(adapter);
Click();//RecyclerView不像ListView一样提供item的点击监听,所以我们只能自己实现。
}
private void Click() {
adapter.setmOnItemClickLitener(new MyAdapter.OnItemClickLitener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(context,"点击了"+dataList.get(position),Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
//Toast.makeText(context,"长按了"+dataList.get(position),Toast.LENGTH_SHORT).show();
}
});
}
private void addData() {
dataList=new ArrayList<>();
for (int i=0;i<10;i++){
dataList.add("第"+i+"个虚拟数据");
}
}
}
MyAdapter.java
简单定义一个适配器并实现点击功能
/**
* Created by Ezreal on 2017/2/9.
* 邮箱:taoge5165161@163.com
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private OnItemClickLitener mOnItemClickLitener;
//点击事件接口
public interface OnItemClickLitener
{
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public void setmOnItemClickLitener(OnItemClickLitener OnItemClickLitener) {
this.mOnItemClickLitener=OnItemClickLitener;
}
public MyAdapter(List<String> list){
this.dataList=list;
}
private List<String> dataList=new ArrayList<>();
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
MyViewHolder holder = new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(
R.layout.item, parent, false));
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.tv.setText(dataList.get(position));
//判断是否设置了监听器
if(mOnItemClickLitener != null) {
//为ItemView设置监听器
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemClick(holder.itemView, pos);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int pos = holder.getLayoutPosition();
mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
return false ;
}
});
}
}
@Override
public int getItemCount() {
return dataList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv;
MyViewHolder(View itemView) {
super(itemView);
tv = (TextView) itemView.findViewById(R.id.tv);
}
}
public List<String> getDataList() {
return dataList;
}
}
MyItemDecoration.java
RecycleView本身没有分割线,但是我们可以通过itemDecoration来定义自己的分割线。
public class MyItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public MyItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
RecyclerView v = new RecyclerView(parent.getContext());
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
MyItemTouchCallBakc.java
拖拽排序和右拖删除功能的实现
**
* Created by Ezreal on 2017/2/9.
* 邮箱:taoge5165161@163.com
*/
public class MyItemTouchCallBack extends ItemTouchHelper.Callback {
private final MyAdapter adapter;
public MyItemTouchCallBack(MyAdapter adapter) {
this.adapter = adapter;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlag;
int swipeFlag;
RecyclerView.LayoutManager layoutManager=recyclerView.getLayoutManager();
//两种情况 GridLayout 有四个方向可以拖拽排序 LinearLayout则有上下两个方向
if (layoutManager instanceof GridLayoutManager){
dragFlag=ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT;
swipeFlag=0;
}else {
dragFlag=ItemTouchHelper.DOWN | ItemTouchHelper.UP;
swipeFlag=ItemTouchHelper.END;
}
return makeMovementFlags(dragFlag,swipeFlag);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
int fromPosition=viewHolder.getAdapterPosition();
int toPosition=target.getAdapterPosition();
if (fromPosition<toPosition){
for (int i=fromPosition;i<toPosition;i++){
Collections.swap(adapter.getDataList(),i,i+1);
}
}else {
for (int i=fromPosition;i>toPosition;i--){
Collections.swap(adapter.getDataList(),i,i-1);
}
}
recyclerView.getAdapter().notifyItemMoved(fromPosition,toPosition);
return true;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position=viewHolder.getAdapterPosition();
if (direction==ItemTouchHelper.END){
adapter.getDataList().remove(position);
adapter.notifyItemRemoved(position);
}
}
@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
super.onSelectedChanged(viewHolder, actionState);
if (actionState==ItemTouchHelper.ACTION_STATE_DRAG)
{
viewHolder.itemView.setAlpha(0.6f);
}
}
@Override
public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
super.clearView(recyclerView, viewHolder);
viewHolder.itemView.setAlpha(1f);
}
}