我们来学习一下item的点击添加和删除
源代码可以上这里获取Github:https://github.com/307572384/recycleitemnew
我们先来看一下如何新增item和删除
首先我们需要在Adapter中新增这一段代码
/**
* 添加新的Item
*/
public void addNewItem() {
if (mData == null) {
mData = new ArrayList<>();
}
mData.add(0, "new item");
notifyItemInserted(0);
}
//删除item
public void deleteItem() {
if (mData == null || mData.isEmpty()) {
return;
}
mData.remove(0);
notifyItemRemoved(0);
}
然后我们再看看activity里面需要做些什么
我们先在布局管理器下面设置一下添加和移除的动画
//设置item添加和移除的动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
然后再给添加和删除的item的那个按钮新增一个点击事件
这里新增点击事件以后后面你可以根据自己想要的进行修改
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.rv_add_item_btn) {
mAdapter.addNewItem();
mLayoutManager.scrollToPosition(0);
} else if (id == R.id.rv_del_item_btn) {
mAdapter.deleteItem();
mLayoutManager.scrollToPosition(0);
}
}
然后我给主的Recyclerview做分支的item跳转到其他的activity也是recyclerview布局的时候发现
Adapter需要放在oncreate里面,我写了initview放在里面好像无法调用和识别到。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rv_linear);
initView();
initData();
initAction();
mRecyclerView.setAdapter(mAdapter);//设置adapter
}
我们来看看完整的代码
DLActivity
**
* 添加和删除item的Recyclerdemo
*/
public class DLActivity extends MDBaseActivity implements View.OnClickListener {
private TextView mTextViewadd;
private TextView mTextViewdel;
private RecyclerView mRecyclerView;
private MDRvAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rv_linear);
initView();
initData();
initAction();
mRecyclerView.setAdapter(mAdapter);//设置adpter
}
private void initAction() {
mTextViewadd.setOnClickListener(this);
mTextViewdel.setOnClickListener(this);
}
private void initData() {
mAdapter = new MDRvAdapter(getData());
mAdapter.setOnItemClickListener(new MDRvAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Toast.makeText(DLActivity.this, "click " + position + " item", Toast.LENGTH_SHORT).show();//单点击事件
}
@Override
public void onItemLongClick(View view, int position) {
Toast.makeText(DLActivity.this, "long click " + position + " item", Toast.LENGTH_SHORT).show();//长按点击事件
}
});
}
private void initView() {
mTextViewadd = (TextView) findViewById(R.id.rv_add_item_btn);
mTextViewdel = (TextView) findViewById(R.id.rv_del_item_btn);
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//设置布局管理器
mRecyclerView.setLayoutManager(mLayoutManager);
//设置item添加和移除的动画
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
// 设置Item之间间隔样式
mRecyclerView.addItemDecoration(new MDLinearRvDividerDecoration(this, LinearLayoutManager.VERTICAL));
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.rv_add_item_btn) {
mAdapter.addNewItem();
mLayoutManager.scrollToPosition(0);
} else if (id == R.id.rv_del_item_btn) {
mAdapter.deleteItem();
mLayoutManager.scrollToPosition(0);
}
}
private ArrayList<String> getData() {
ArrayList<String> data = new ArrayList<>();
String temp = " item";
for (int i = 0; i < 20; i++) {
data.add(i + temp);
}
return data;
}
}
MDRvAdapter
public class MDRvAdapter extends RecyclerView.Adapter<MDRvAdapter.ViewHolder> {
//展示数据
private ArrayList<String> mData;
//事件回调监听
private MDRvAdapter.OnItemClickListener onItemClickListener;
public MDRvAdapter(ArrayList<String> data) {
this.mData = data;
}
public void updateData(ArrayList<String> data) {
this.mData = data;
notifyDataSetChanged();
}
/**
* 添加新的Item
*/
public void addNewItem() {
if (mData == null) {
mData = new ArrayList<>();
}
mData.add(0, "new item");
notifyItemInserted(0);
}
//删除item
public void deleteItem() {
if (mData == null || mData.isEmpty()) {
return;
}
mData.remove(0);
notifyItemRemoved(0);
}
/**
* 设置回调监听
*
* @param listener
*/
public void setOnItemClickListener(MDRvAdapter.OnItemClickListener listener) {
this.onItemClickListener = listener;
}
@Override
public MDRvAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// 实例化展示的view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_rv_item, parent, false);
// 实例化viewholder
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final MDRvAdapter.ViewHolder holder, int position) {
//绑定数据
holder.mTv.setText(mData.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onItemClickListener != null) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemClick(holder.itemView, pos);
}
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (onItemClickListener != null) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemLongClick(holder.itemView, pos);
}
return true;
}
});
}
@Override
public int getItemCount() {
return mData.size();
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTv;
public ViewHolder(View itemView) {
super(itemView);
mTv = (TextView) itemView.findViewById(R.id.item_tv1);
}
}
}
MDLinearRvDividerDecoration
这个是分隔线
public class MDLinearRvDividerDecoration extends RecyclerView.ItemDecoration {
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
/**
* 用于绘制间隔样式
*/
private Drawable mDivider;
/**
* 列表的方向,水平/竖直
*/
private int mOrientation;
public MDLinearRvDividerDecoration(Context context, int orientation) {
// 获取默认主题的属性
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(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 setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
/**
* 绘制间隔
*/
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);
}
}
}
下面是layout布局
activity_rv_linear
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/rv_add_item_btn"
android:layout_width="match_parent"
android:layout_height="@dimen/md_common_btn_height"
android:gravity="center"
android:text="@string/md_add_item"/>
<TextView
android:id="@+id/rv_del_item_btn"
android:layout_width="match_parent"
android:layout_height="@dimen/md_common_btn_height"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="@string/md_del_item"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true" />
</LinearLayout>
view_rv_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/item_tv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="item"/>
</LinearLayout>