网上都说recycleview是listview和gridview的结合体,是加强版.能自定义很多样式,而且类也比较开放,可以定义一些功能体
实用recycleview也比较简单:
1.创建布局管理器
2.创建间隔样式
3.创建默认动画样式
4.创建可拖动助手
5.创建适配器
//布局管理器
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//间隔样式
mDivider = new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL);
//动画
animator = new DefaultItemAnimator();
//适配器
mAdapter = new RecyclerAdapterCopy(getData());
//拖动助手
mItemTouchHelper = new ItemTouchHelper(new ItemTouchHelperCallback(mAdapter));
//---------------------设置上述5步到recycleview--------------
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addItemDecoration(mDivider);
mRecyclerView.setItemAnimator(animator);
mItemTouchHelper.attachToRecyclerView(mRecyclerView);
mRecyclerView.setAdapter(mAdapter);
MyDividerItemDecoration
public class MyDividerItemDecoration 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 MyDividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
/**
* 设置方向
* @param orientation
*/
private 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, RecyclerView.State state) {
// 绘制间隔
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
/**
* 绘制垂直间隔
*/
private 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);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin +
Math.round(ViewCompat.getTranslationY(child));
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
/**
* 绘制水平间隔
*/
private 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 +
Math.round(ViewCompat.getTranslationX(child));
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
}
ItemTouchHelper
public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
private RecyclerAdapterCopy mRecyclerAdapter;
public ItemTouchHelperCallback(RecyclerAdapterCopy adapterCopy){
this.mRecyclerAdapter = adapterCopy;
}
/**
* 指定可以支持的拖放和滑动的方向
* @param recyclerView
* @param viewHolder
* @return
*/
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
mRecyclerAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mRecyclerAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
public interface IItemHelper {
void onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);
}
}
RecyclerAdapterCopy
public class RecyclerAdapterCopy extends RecyclerView.Adapter<RecyclerAdapterCopy.ViewHolder> implements ItemTouchHelperCallback.IItemHelper{
private ArrayList<String> mData;
private OnItemClickListener mOnItemClickListener;
public RecyclerAdapterCopy(ArrayList<String> mData) {
this.mData = mData;
}
public void updateData(ArrayList<String> data) {
this.mData = data;
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
/**
* 创建复用
* @param parent
* @param viewType
* @return
*/
@Override
public RecyclerAdapterCopy.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View inflate = View.inflate(parent.getContext(), R.layout.recycler_layout_2, null);
ViewHolder holder = new ViewHolder(inflate);
return holder;
}
/**
* 绑定数据
* @param holder
* @param position
*/
@Override
public void onBindViewHolder(final RecyclerAdapterCopy.ViewHolder holder, final int position) {
holder.mTv.setText(mData.get(position));
ViewHolder mItems = holder;
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mOnItemClickListener != null) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemClick(holder.itemView, pos);
}
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if(mOnItemClickListener != null) {
int pos = holder.getLayoutPosition();
mOnItemClickListener.onItemLongClick(holder.itemView, pos);
}
return true;
}
});
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTv;
public ViewHolder(View itemView) {
super(itemView);
mTv = (TextView) itemView.findViewById(R.id.tv_info);
}
}
public void addNewItem() {
if(mData == null) {
mData = new ArrayList<>();
}
mData.add(0, "new Item");
notifyItemInserted(0);
}
public void deleteItem() {
if(mData == null || mData.isEmpty()) {
return;
}
mData.remove(0);
notifyItemRemoved(0);
}
/**
* 公共接口
*/
public void setOnItemClickListener(OnItemClickListener OnItemClickListener){
this.mOnItemClickListener = OnItemClickListener;
}
public interface OnItemClickListener{
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
@Override
public void onItemMove(int fromPosition, int toPosition) {
String prev = mData.remove(fromPosition);
mData.add(toPosition > fromPosition ? toPosition - 1 : toPosition, prev);
notifyItemMoved(fromPosition, toPosition);
}
@Override
public void onItemDismiss(int position) {
mData.remove(position);
notifyItemRemoved(position);
}
}
recycle子布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="sssss"
android:gravity="center"
android:textSize="36sp"/>
</RelativeLayout>
让adapter实现IItemHelper是因为让recycle不光只是可以拖动,而且拖动后会把当前的item放到手指的位置,而且itemview左右移动会让当前item消失.
gif不会做,而且在手机上实现的,所以就不截图了.
网友评论