【发布时间】:2017-04-06 05:45:26
【问题描述】:
在适配器上调用 notifyItemRemoved(position) 时遇到问题。每当我提供的位置不是最后一个元素,并且当删除动画发生时,最后一个元素总是会在向上移动之前先“闪烁/闪烁”,如下面的屏幕截图(链接)所示。
这是我的代码的一个小sn-p:
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
RecylerView recyclerView = (RecyclerView) findViewById(R.id.list_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(new ItemAdapter());
}
ItemAdapter.java
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// mItems has a list of hard-coded elements
mItems.remove(holder.getAdapterPosition());
notifyItemRemoved(holder.getAdapterPosition());
}
});
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.myapp.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_click"
android:text="Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
有没有人经历过这个问题以及如何解决这个问题?
提前致谢。
【问题讨论】:
-
可以发一下布局文件或者
RecyclerView吗? -
@azizbekian 抱歉,我很忙。是的,已添加 xml。我没有添加activity_main.xml,因为它是在Android Studio中创建新项目时的默认xml
标签: android android-layout listview