SwipeDelMenuLayout
SwipeDelMenuLayout copied to clipboard
在RecyclerView中,侧滑删除时有问题。
删除第一个和第二个会出现,留白情况,就是剩下的item会一直往页面中心靠拢,而不是正常的顶替被删除的item的位置,但是删除第三个后面的item就不会出现这个问题,请问是怎么回事?是我使用哪里不对吗?请帮助我。 这是frgament代码 ` public class UploadingFragment extends BaseFragment implements MainActivity.PassingDataToUploadingFragment, UploadingAdapter.onSwipeListener {
@BindView(R.id.uploading_tv)
TextView mTextView;
@BindView(R.id.uploading_recyclerview)
RecyclerView mRecyclerView;
private UploadingAdapter adapter;
//实时传递过来的数据集合
List<PhotoBean> photoBeanList = new ArrayList<>();
public UploadingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_uploading, container, false);
ButterKnife.bind(this, view);
initData();
initView();
return view;
}
@Override
protected void initData() {
super.initData();
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.setPassingDataToUploadingFragment(this);
}
@Override
protected void initView() {
super.initView();
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
manager.setOrientation(VERTICAL);
mRecyclerView.setLayoutManager(manager);
adapter = new UploadingAdapter(getActivity(), photoBeanList);
if (photoBeanList.size() != 0) {
mTextView.setVisibility(View.GONE);
mRecyclerView.setVisibility(View.VISIBLE);
adapter.setOnDelListener(this);
mRecyclerView.setAdapter(adapter);
startUploadingService(photoBeanList);
} else {
mRecyclerView.setVisibility(View.GONE);
mTextView.setVisibility(View.VISIBLE);
}
}
private void startUploadingService(List<PhotoBean> photoBeanList) {
}
@Override
public void getListData(Bundle bundle) {
if (bundle != null) {
bundle.getParcelableArrayList("data");
photoBeanList = bundle.getParcelableArrayList("data");
initView();
}
}
@Override
public void onDel(int pos) {
if (pos >= 0 && pos < photoBeanList.size()) {
Toast.makeText(getActivity(), "删除:" + pos, Toast.LENGTH_SHORT).show();
photoBeanList.remove(pos);
//删除动画效果
adapter.notifyItemRemoved(pos);
//如果删除时,不使用mAdapter.notifyItemRemoved(pos),则删除没有动画效果,
//且如果想让侧滑菜单同时关闭,需要同时调用 ((SwipeMenuLayout) holder.itemView).quickClose();
//mAdapter.notifyDataSetChanged();
}
}
}
这是Adapte代码
public class UploadingAdapter extends RecyclerView.Adapter<UploadingAdapter.ViewHolder> {
private Context context;
private LayoutInflater mInflater;
private List<PhotoBean> list;
/**
* 和Activity通信的接口
*/
public interface onSwipeListener {
void onDel(int pos);
}
private onSwipeListener mOnSwipeListener;
public onSwipeListener getOnDelListener() {
return mOnSwipeListener;
}
public void setOnDelListener(onSwipeListener mOnDelListener) {
this.mOnSwipeListener = mOnDelListener;
}
public UploadingAdapter(Context context, List<PhotoBean> list) {
this.context = context;
this.list = list;
mInflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_uploading, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
if (list.size() != 0) {
Glide.with(context).load(list.get(position).getPath()).into(holder.mImageView);
holder.mTextName.setText(list.get(position).getName());
holder.mTextSize.setText(list.get(position).getImageSize());
}
//验证长按
holder.mLinearLayout.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(context, "longclig", Toast.LENGTH_SHORT).show();
Log.e("TAG", "onLongClick() called with: v = [" + v + "]");
return false;
}
});
holder.mButtonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mOnSwipeListener) {
//如果删除时,不使用mAdapter.notifyItemRemoved(pos),则删除没有动画效果,
//且如果想让侧滑菜单同时关闭,需要同时调用 ((CstSwipeDelMenu) holder.itemView).quickClose();
//((CstSwipeDelMenu) holder.itemView).quickClose();
mOnSwipeListener.onDel(holder.getAdapterPosition());
}
}
});
//注意事项,设置item点击,不能对整个holder.itemView设置咯,只能对第一个子View,即原来的content设置,这算是局限性吧。
(holder.mLinearLayout).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "onClick:" + list.get(holder.getAdapterPosition()).getName(), Toast.LENGTH_SHORT).show();
Log.d("TAG", "onClick() called with: v = [" + v + "]");
}
});
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.item_uploading_linear)
LinearLayout mLinearLayout;
@BindView(R.id.item_uploading_iv)
ImageView mImageView;
@BindView(R.id.item_uploading_state)
ImageView mImageState;
@BindView(R.id.item_uploading_name)
TextView mTextName;
@BindView(R.id.item_uploading_speed)
TextView mTextSpeed;
@BindView(R.id.item_uploading_schedule)
TextView mTextSchedule;
@BindView(R.id.item_uploading_size)
TextView mTextSize;
@BindView(R.id.item_uploading_delete)
Button mButtonDelete;
ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
这是item
<com.mcxtzhang.swipemenulib.SwipeMenuLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="80dp"
android:clickable="true"
android:paddingBottom="1dp"
app:ios="false"
app:leftSwipe="true"
app:swipeEnable="true">
<LinearLayout
android:id="@+id/item_uploading_linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp">
<FrameLayout
android:layout_width="65dp"
android:layout_height="60dp">
<ImageView
android:id="@+id/item_uploading_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/item_uploading_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_weight="2"
android:gravity="bottom">
<TextView
android:id="@+id/item_uploading_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
<TextView
android:id="@+id/item_uploading_speed"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/item_uploading_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
<TextView
android:id="@+id/item_uploading_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="8dp"
android:background="@color/grey" />
</LinearLayout>
<Button
android:id="@+id/item_uploading_delete"
android:layout_width="60dp"
android:layout_height="match_parent"
android:background="@color/red"
android:text="删除"
android:textColor="@android:color/white"/>
</com.mcxtzhang.swipemenulib.SwipeMenuLayout> `
你的RecyclerView的父布局是ScrollView吗?我的代码中,这样写的就存在这种现象,因为ScrollView会自动收缩